HDU TodayTime
limit:15000/5000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 19697 Accepted Submission (s): 4635
Problem description through the brocade SAC Help, Sea East group finally passed the crisis, from now on, HDU development has been downwind, to 2050 years, the group has been quite scale, said to enter the Qianjiang pork economic Development Zone 500 strong. At this time, Xhd couple also retreated to the second line, and in the beautiful scenery of Zhuji knots Pu Zhen Tao Yao bought a house, began to comfort old age.
This lived for a period of time, Xu always to the local traffic still do not know very much. Sometimes very depressed, want to go to a place and do not know what bus to take, where to change, where to get off (in fact, Xu always own a car, but must have fun with the people, this is Xu General character).
Xu always asks the lame English question: Can you help me? Look at his confused and helpless eyes, enthusiastic can you help him?
Please help him to arrive at his destination in the shortest possible time (assuming that each bus stops at starting point and terminus and is open at all times).
Input data has multiple groups, the first row of each group is the total number of buses N (0<=n<=10000);
The second line has the location of the total Xu, start, his destination end;
Then there are n rows, each with the station name S, the station name E, and the time integer T (0<t<100) from S to E (each place name is a string with a length of not more than 30).
Note: No more than 150 place names in a set of data.
If N==-1, indicates the end of the input.
Output if Xu always arrives at the destination, outputs the shortest time; otherwise, output "-1".
Sample Input
6xiasha Westlakexiasha Station 60xiasha Shoppingcenterofhangzhou 30station Westlake 20ShoppingCenterofHangZhou Supermarket 10xiasha supermarket 50supermarket Westlake 10-1
Sample Output
50hint:the Best Route Is:xiasha->shoppingcenterofhangzhou->supermarket->westlake Although occasionally lost, but because of your help * * and * * Since then, I have lived a happy life. -All the ending-
Authorlgx
SOURCEACM Program Design _ Final Exam (time has been set!!) Idea: Actually this problem is the bare Dijkstra algorithm, but because the map function is not used, so the map function is also a difficult point. This problem has a pit, is the road no direction, but the topic described as if there is a direction, this will lead to WA, to pay special attention! the first time with the map function, here a little summary of it! The header file for the map function has: #include <iostream> # Include <algorithm> #include <map> using namespace std; define the Map function:map<string,int>mp; Empty function: mp.clear (); use map: map[a]=b (a[10]= "ABCDs", b=1); code:
This time is the first time with the map function, feel the map function is very powerful! #include <stdio.h> #include <string.h> #include <iostream>//This function name must also have #include <map> #include <algorithm>using namespace std; #define INF 0x3f3f3f3fint d[155][155];int dis[155];int vis[155];int M;char x[35],y [35];int t;map<string,int>mp;//because this container is also used inside the main function, so define void init () {mp.clear () outside the function;// It's OK to clear 0 here (remove all the elements!) GetChar ();//Pay attention to the character type before adding GetChar (); scanf ("%s%s", x, y) mp[x]=1;//the table below starts from 1! T=2;while (!mp[y])//Just start without considering the starting point and the end of the coincidence, be sure to remember that there may be coincident {mp[y]=t++;//So this instinct is written mp[y]=2;} Memset (d,inf,sizeof (d)); Memset (vis,0,sizeof (Vis)); Char a[35],b[35];int c;for (int i=1;i<=m;i++) {getchar (); scanf ("%s%s%d", a,b,&c), if (!mp[a])//If the string is not the same as the previous string, it is necessary to number {mp[a]=t++;//Otherwise it will not be numbered! }if (!mp[b]) {mp[b]=t++;} if (d[mp[a]][mp[b]]>c)//The string is numbered, it can be manipulated like a normal number! {d[mp[b]][mp[a]]=d[mp[a]][mp[b]]=c;}} for (int i=1;i<t;i++) {dis[i]=d[1][i];} Dis[1]=0;vis[1]=1;} void Dijkstra () {int min,k;for (int i=1;i<t;i++) {min=inf;for (int j=1;j<t;j++) {if (!vis[j]&&d is[j]<min) {min=dis[j];k=j;}} if (min==inf) break;vis[k]=1;for (int j=1;j<t;j++) {if (!vis[j]&&dis[j]>dis[k]+d[k][j]) {dis[j]=dis[k]+ D[K][J];}}} int main () {while (scanf ("%d", &m) && (m!=-1)) {init ();d Ijkstra (); if (Dis[mp[y]]==inf)//start without considering the beginning and end coincident, Be sure to remember that it is possible to overlap printf (" -1\n"); elseprintf ("%d\n", Dis[mp[y]]);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 2112 hdu Today < Dijkstra algorithm +map functions >