您的位置:首页 > 其它

POJ1237 The Postal Worker Rings Once

2012-08-04 16:33 288 查看
The Postal Worker Rings Once

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 515Accepted: 331
Description

Graph algorithms form a very important part of computer science and have a lineage that goes back at least to Euler and the famous Seven Bridges of Konigsberg problem. Many optimization problems involve determining efficient methods for reasoning about graphs.

This problem involves determining a route for a postal worker so
that all mail is delivered while the postal worker walks a minimal
distance, so as to rest weary legs.

Given a sequence of streets (connecting given intersections) you are
to write a program that determines the minimal cost tour that traverses
every street at least once. The tour must begin and end at the same
intersection.

The ``real-life'' analogy concerns a postal worker who parks a truck
at an intersection and then walks all streets on the postal delivery
route (delivering mail) and returns to the truck to continue with the
next route.

The cost of traversing a street is a function of the length of the
street (there is a cost associated with delivering mail to houses and
with walking even if no delivery occurs).

In this problem the number of streets that meet at a given
intersection is called the degree of the intersection. There will be at
most two intersections with odd degree. All other intersections will
have even degree, i.e., an even number of streets meeting at that
intersection.

Input

The
input consists of a sequence of one or more postal routes. A route is
composed of a sequence of street names (strings), one per line, and is
terminated by the string ``deadend'' which is NOT part of the route. The
first and last letters of each street name specify the two
intersections for that street, the length of the street name indicates
the cost of traversing the street. All street names will consist of
lowercase alphabetic characters.

For example, the name foo indicates a street with intersections f
and o of length 3, and the name computer indicates a street with
intersections c and r of length 8. No street name will have the same
first and last letter and there will be at most one street directly
connecting any two intersections. As specified, the number of
intersections with odd degree in a postal route will be at most two. In
each postal route there will be a path between all intersections, i.e.,
the intersections are connected.

Output

For
each postal route the output should consist of the cost of the minimal
tour that visits all streets at least once. The minimal tour costs
should be output in the order corresponding to the input postal routes.
Sample Input

one
two
three
deadend
mit
dartmouth
linkoping
tasmania
york
emory
cornell
duke
kaunas
hildesheim
concord
arkansas
williams
glasgow
deadend

Sample Output

11
114

Source

Duke Internet Programming Contest 1992,UVA 117

思路:每个字符串表示两个点以及这两个点之间的距离,字符串的都是小写字母,字符串的第一个字母和最后一个字母表示点,字符串的长度表示两点之间的距离(两点之间的距离没有方向),没有重边。同时,这些点里面,最多只有两个点的度为奇数,其余全部为偶数。求遍历所有点所经过的最少路径。在体重只有两种情况:1.所有点的度数都是偶数。2.所有点中度数为奇数的度数恰好两个(因为每条边产生2度,所以所有度的总和一定是偶数,所以不会出现一个奇度点)。在第1中情况中,这个图刚好构成欧拉图,欧拉图经过所有边一次恰好能够遍历所有点,所以结果为所有边的和。在第2种情况中,这个图构成一个欧拉通路,如果将两个奇度点之间再人为的加上一条边,则构成了欧拉图,而这条边的长度就是这两个点之间的最短距离。

#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cctype>
#include <cmath>
#include <queue>
#include <vector>

#define MAXINT 99999999

using namespace std;

int data[100][100];
int vis[100];
int degree[100];

int lowcost[100];

int main(int argc, char *argv[])
{
char tmpStr[1000];

int i,j,k;
int sum=0;

while(scanf("%s",tmpStr)!=EOF)
{
if(strcmp(tmpStr,"deadend")==0)
continue;

getchar();

sum=0;

for(i=0;i<26;i++)
for(j=0;j<26;j++)
data[i][j]=MAXINT;

for(i=0;i<26;i++)
degree[i]=vis[i]=0;

int len=strlen(tmpStr);

data[tmpStr[0]-'a'][tmpStr[len-1]-'a']=len;
data[tmpStr[len-1]-'a'][tmpStr[0]-'a']=len;
degree[tmpStr[0]-'a']++;
degree[tmpStr[len-1]-'a']++;

sum+=len;

while(scanf("%s",tmpStr)!=EOF)
{if(strcmp(tmpStr,"deadend")==0)
break;

getchar();

len=strlen(tmpStr);

data[tmpStr[0]-'a'][tmpStr[len-1]-'a']=len;
data[tmpStr[len-1]-'a'][tmpStr[0]-'a']=len;
degree[tmpStr[0]-'a']++;
degree[tmpStr[len-1]-'a']++;

sum+=len;

}

for(i=0;i<26;i++)
if(degree[i]%2==1)
break;

if(i>=26)
{
printf("%d\n",sum);
continue;
}

int beginv=i;

for(i++;i<26;i++)
if(degree[i]%2==1)
break;

int endv=i;

for(j=0;j<26;j++)
{vis[j]=0;lowcost[j]=data[beginv][j];}

int mincost=MAXINT;

vis[beginv]=1;

for(i=1;i<26;i++)
{
mincost=MAXINT;
k=-1;

for(j=0;j<26;j++)
{if((vis[j]==0)&&(mincost>lowcost[j]))
{k=j;mincost=lowcost[j];}
}

vis[k]=1;

if(k==endv)
break;

for(j=0;j<26;j++)
{
if((vis[j]==0)&&(lowcost[k]+data[k][j]<lowcost[j]))
lowcost[j]=lowcost[k]+data[k][j];
}

}

sum+=lowcost[endv];
printf("%d\n",sum);
}

//system("PAUSE");
return EXIT_SUCCESS;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: