HDU Today
Time limit:15000/5000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 19487 Accepted Submission (s): 4578
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-
Well, the main idea is to ask for the shortest path between two points, but the input is not easy to handle, here I define a two-dimensional array to put in, the others are the same
Dijkstra#include <iostream> #include <cstdio> #include <cstring> #define INF 0x3f3f3f3fusing namespace Std;char s[150][30];int cnt,map[150][150],rec[150],vis[150];void Dijkstra (int s,int e) {memset (vis,0,sizeof (VIS)); Vis[s]=1; for (int i=0;i<cnt;++i) rec[i]=map[s][i]; while (1) {int minw=inf,mark=-1; for (int i=0;i<cnt;++i) {if (!VIS[I]&&REC[I]<MINW) {minw=rec[i]; Mark=i; }} if (Mark==-1) break; Vis[mark]=1; for (int i=0;i<cnt;++i) {if (!vis[i]&&rec[i]>rec[mark]+map[mark][i]) rec[i]= Rec[mark]+map[mark][i]; }} if (Rec[e]==inf) printf (" -1\n"); else printf ("%d\n", Rec[e]);} int main () {int n,u,v,f; Char a[30],b[30]; while (scanf ("%d", &n) && (n!=-1)) {memset (map,inf,sizeof (map)); scanf ("%s%s", s[0],s[1]); cnt=2; while (n--) {scanf ("%s%s%d", a,b,&f); for (int i=0;i<cnt;++i) {if (strcmp (A,s[i]) ==0) {u=i; Break } else if (i==cnt-1) {u=cnt; strcpy (S[cnt++],a); Break }} for (int i=0;i<cnt;++i) {if (strcmp (B,s[i]) ==0) { V=i; Break } else if (i==cnt-1) {v=cnt; strcpy (S[CNT++],B); }} map[u][v]=map[v][u]=f; } if (strcmp (s[0],s[1]) ==0) {printf ("0\n"); Continue } Dijkstra (0,1); } return 0;}
Floyed#include <iostream> #include <cstdio> #include <cstring> #define INF 0x3f3f3f3fusing namespace Std;char s[150][30];int cnt,map[150][150];void floyed () {for (int. k=0;k<cnt;++k) {for (int i=0;i& Lt;cnt;++i) {for (int j=0;j<cnt;++j) {if (Map[i][j]>map[i][k]+map[k][j]) MAP[I][J]=MAP[I][K]+MAP[K][J]; }}}}int Main () {int n,u,v,f; Char a[30],b[30]; while (scanf ("%d", &n) && (n!=-1)) {memset (map,inf,sizeof (map)); scanf ("%s%s", s[0],s[1]); cnt=2; while (n--) {scanf ("%s%s%d", a,b,&f); for (int i=0;i<cnt;++i) {if (strcmp (A,s[i]) ==0) {u=i; Break } else if (i==cnt-1) {u=cnt; strcpy (S[cnt++],a); Break }} for (int i=0;i<cnt;++i) {if (strcmp (B,s[i]) ==0) { V=i; Break } else if (i==cnt-1) {v=cnt; strcpy (S[CNT++],B); }} map[u][v]=map[v][u]=f; } if (strcmp (s[0],s[1]) ==0) {printf ("0\n"); Continue } floyed (); if (map[0][1]==inf) printf (" -1\n"); else printf ("%d\n", map[0][1]); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 2112 HDU Today "Shortest path Dijkstra & floyed"