
Sahand

Moderator: Board moderators
{$S-,R-,Q-,B-}
const hValue = maxlongint div 2;
var i, j, q, nod, nod2: char;
grad: array['a'..'z'] of longint;
d: array['a'..'z', 'a'..'z'] of longint;
s: string;
t: longint;
found: boolean;
procedure read_data;
begin
// assign(input, '117.in'); reset(input);
// assign(output, '117.out'); rewrite(output);
while not eof(input) do begin
readln(s);
t := 0; found := false;
for i := 'a' to 'z' do
for j := 'a' to 'z' do d[i, j] := hValue;
while (s <> 'deadend') do begin
if s <> '' then begin
found := true;
inc(grad[s[1]]); inc(grad[s[length(s)]]);
inc(t, length(s));
d[s[1], s[length(s)]] := length(s);
d[s[length(s)], s[1]] := length(s);
end;
readln(s);
end;
nod := 'A'; nod2 := 'A';
for i := 'a' to 'z' do
if odd(grad) then
if nod = 'A' then nod := i else
if nod2 = 'A' then nod2 := i;
if nod <> 'A' then begin
for q := 'a' to 'z' do
for i := 'a' to 'z' do
for j := 'a' to 'z' do
if d[i, q] + d[q, j] < d[i, j] then d[i, j] := d[i, q] + d[q, j];
t := t + d[nod, nod2];
end;
if found then writeln(t);
end;
// close(input); close(output);
end;
begin
read_data;
end.
Code: Select all
//117 The Postal Worker rings once
#include <iostream>
#include <string>
#include <cassert>
using namespace std;
const char dead[]="deadend";
#define N 40
#define LEN 1000
#define inf 99999999
int edge[N][N],degree[N];
int main(){
//freopen("a.in","r",stdin);
char st[LEN];
int len;
int _wer=0,sum=0,odd1,odd2,x,y;
while(1){
_wer=0;
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
edge[i][j]=inf;
for(int i=0;i<N;i++) degree[i]=0;
while(cin>>st){
_wer=1;
if(strcmp(st,dead)==0) break;
len=strlen(st);
x=st[0]-'a';y=st[len-1]-'a';
if (edge[x][y]>len){
edge[x][y]=len;
edge[y][x]=len;
}
degree[x]++;
degree[y]++;
sum+=len;
}
if(_wer==0) break;
int q;odd1=-1;odd2=-1;
for(q=0;q<N;q++) if (degree[q]%2!=0){odd1=q;q++;break;}
for(;q<N;q++) if(degree[q]%2!=0){odd2=q;break;}
if(odd1!=-1 && odd2!=-1)
{
assert(odd1!=odd2);
for(int i=0;i<N;i++) edge[i][i]=0;
for(int k=0;k<N;k++){
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if (edge[i][k]+edge[k][j]<edge[i][j])
edge[i][j]=edge[i][k]+edge[k][j];
}
}
}
cout<<sum+edge[odd1][odd2]<<endl;
}
else cout<<sum<<endl;
}
return 0;
}
it means that we have to find out euler tour .and euler tour is possible only whenThe tour must begin and end at the same intersection.
but problem statement also states thatAn undirected graph contains an Eulerian cycle iff (1) it is connected and (2) each vertex is of even degree.
in this situtation only euler tour is possible not cycle ..There will be at most two intersections with odd degree.
...now plz explain me the problem wheather we have to find euler cycle or euler tour ..becoz i think it is asking for cycle which is not possible when there will be odd virtex...An undirected graph contains an Eulerian path iff (1) it is connected and (2) all but two vertices are of even degree. These two vertices will be the start and end points of the path.
Neither of them..mukeshtiwari wrote:...now plz explain me the problem wheather we have to find euler cycle or euler tour ..becoz i think it is asking for cycle which is not possible when there will be odd virtex...
Code: Select all
[#include <iostream>
#include <string>
#include <fstream>
using namespace std;
const int Max=1000000;
const int mx=10000;
const int bound=27;
int cost[bound][bound]={Max};
int sw[mx]={-1};
int i,j,k,count=0;
int dijkstra(int first,int last){
sw[first]=1;
int cur;
int min=Max;
for(j=1;j<bound;j++){
min=Max;
for(k=1;k<bound;k++){
if(!sw[k]){
if(cost[first][k]<min){ min=cost[first][k]; cur=k;}
}
}
sw[cur]=1;
for(i=0;i<bound;i++){
if(!sw[i] && i!=first && i!=cur){
if(cost[first][i]>cost[first][cur]+cost[cur][i]){
cost[first][i]=cost[first][cur]+cost[cur][i];
}
}
}
if(cur==last) break;
}
return cost[first][last];
}
void main(){
for(i=0;i<bound;i++){
for(j=0;j<bound;j++){
cost[i][j]=Max;
}
}
// ifstream cin("117.in");
char temp[1000];
int firstNode,lastNode;
int w=0;
int l=0;
while(1){
cin>>temp;
if(!strcmp(temp,"deadend")){
w=0;
firstNode=-1;
lastNode=-1;
int sumEdges=0;
for(i=0;i<bound;i++){
count=0;
for(j=0;j<bound;j++){
if(cost[i][j]!=Max){
sumEdges+=cost[i][j];
count++;
}
}
if((count%2)){
if(!w){
firstNode=i;
w=1;
}
else lastNode=i;
}
}
sumEdge/=2;
if(firstNode!=-1) sumEdges+=dijkstra(firstNode,lastNode);
cout<<sumEdges<<endl;
for(i=0;i<bound;i++){
sw[i]=0;
for(j=0;j<bound;j++){
cost[i][j]=Max;
}
}
continue;
}
else if(cin.eof()) break;
int length=strlen(temp);
l+=length;
cost[temp[0]-'a'][temp[length-1]-'a']=length;
cost[temp[length-1]-'a'][temp[0]-'a']=length;
sw[temp[0]-'a']=0;
sw[temp[length-1]-'a']=0;
}
}