Link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2112
HDU today
Time Limit: 15000/5000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 14515 accepted submission (s): 3405
With the help of Problem description, Haidong Group has finally passed the crisis. Since then, HDU has been developing smoothly. By 2050, the Group has already reached a considerable scale, it is said that it entered the Qianjiang meat silk Economic Development Zone's top 500. At this time, the xhd couple also retired to the second line, and they bought a house in taoyao village, Xupu town, zhushu city, Fengjing, and began to settle down in their later years.
After staying for a while, Xu still does not know much about the local traffic. Sometimes it is very depressing. If you want to go to a certain place, you don't know what bus to take, where to get a taxi, or where to get off. (In fact, Xu has his own car, but he must be happy with the people, this is Xu's character ).
Mr. Xu often asked me in poor English: "Can you help me ?". Looking at his confused and helpless eyes, can you help him with enthusiasm?
Please help him reach his destination in the shortest time (assuming that each bus stops at the start station and end station, and will start at any time ).
There are multiple groups of input data. The first line of each group is the total number of buses N (0 <=n <= 10000 );
In the second line, there is Xu's location start, his destination end;
Next there are n rows, each row has the station name s, station name E, and the time integer t from S to E (0 <t <100) (Each place name is a string of no more than 30 characters ).
Note: The number of physical names in a group of data cannot exceed 150.
If n =-1, the input ends.
Output: If Xu can always reach the destination, the output time is the shortest; otherwise, "-1" is output ".
Sample input6xiasha westlakexiasha Station 60 Xiasha shoppingcenterofhangzhou 30 station Westlake 20 shoppingcenterofhangzhou supermarket 10 Xiasha supermarket 50 supermarket Westlake 10-1
Sample output50 hint: the best route is: Xiasha-> shoppingcenterofhangzhou-> supermarket-> although Westlake occasionally gets lost, but with your help ** and **, we have lived a happy life. -The end of the entire drama ――
Authorlgx
//////////////////////////////////////// //////////////////////////////////////// /////////////////////////
String processing is a bit pitfall. Using the map container, we found that map is really powerful.
Here map is good: http://www.cppblog.com/vontroy/archive/2010/05/16/115501.html
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>#include <map>#define MAXX 155#define INF 1000000000using namespace std;int d[MAXX],mp[MAXX][MAXX],visit[MAXX];void Dijkstra(int n){ memset(visit,0,sizeof(visit)); int i,y; for(i=1;i<=n;i++) { d[i] = mp[1][i]; } d[1] = 0; visit[1]=1; for(i=1; i<=n; i++) { int m=INF,x; for(y=1; y<=n; y++) { if(!visit[y] && d[y]<=m) { m = d[x = y]; } } if(m == INF)break; visit[x]=1; for(y=1; y<=n; y++) { if(!visit[y] && d[y]>d[x]+mp[x][y]) { d[y]=d[x]+mp[x][y]; } } }}int main(){ int n,m,i,j; char tmp[32],tmp1[32]; map<string,int>car; while(scanf("%d",&n)!=EOF&&n!=-1) { bool flag=false; for(i=0; i<MAXX; i++) for(j=0; j<MAXX; j++) mp[i][j]=INF; car.clear(); scanf("%s%s",tmp,tmp1); if(strcmp(tmp,tmp1)==0) { flag=true; } car[tmp]=1; car[tmp1]=2; int count = 3 ; int di=0; for(i=0;i<n;i++) { scanf("%s%s%d",tmp,tmp1,&di); if(!car[tmp]) { car[tmp]=count++; } if(!car[tmp1]) { car[tmp1]=count++; } if(mp[car[tmp]][car[tmp1]] > di) { mp[car[tmp1]][car[tmp]]=mp[car[tmp]][car[tmp1]]=di; } } if(flag) { printf("0\n"); continue; } Dijkstra(count-1); if(d[2]==INF)printf("-1\n"); else printf("%d\n",d[2]); } return 0;}
View code