HDU Today
Time limit:15000/5000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 19475 Accepted Submission (s): 4574
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 INPUT6 Xiasha Westlake Xiasha Station-Xiasha Shoppingcenterofhangzhou-station Westlake Shoppingcenterofhan Gzhou supermarket Xiasha supermarket supermarket Westlake 10-1
Sample Output50 hint:the Best route is:xiasha->shoppingcenterofhangzhou->supermarket->westlake though occasionally lost, But because of your help * * and * * Still live a happy life. -The whole ending-the puzzle: WA to explode ... A description is attached to the code:
1#include <stdio.h>2#include <string.h>3 #defineMIN (x, y) (x<y?x:y)4#include <map>5#include <string>6 using namespacestd;7 Const intmaxn= the;8 Const intinf=0x3f3f3f3f;9 intN,top;Tenmap<string,int>MP; One intBUS[MAXN][MAXN],VIS[MAXN],D[MAXN]; A voidInitial () { -memset (Bus,inf,sizeof(bus)); -memset (Vis,0,sizeof(Vis)); thememset (D,inf,sizeof(d)); - mp.clear (); -top=0; - } + voidDijskra (ints) { -d[s]=0; + intK; A while(true){ atk=-1; - for(intI=0; i<top;i++) - if(!vis[i]&& (k==-1|| D[I]<D[K]) k=i; - if(k==-1) Break; -vis[k]=1; - for(intI=0; i<top;i++) inD[i]=min (d[i],d[k]+bus[k][i]); - } to } + intMain () { - Charbeg[ to],en[ to],a[ to],b[ to]; the intC; * while(~SCANF ("%d", &n), n!=-1){ $ initial ();Panax Notoginsengscanf"%s%s", beg,en); - while(n--){ thescanf"%s%s%d",a,b,&c); + if(!mp.count (a)) mp[a]=top++; A if(!mp.count (b)) mp[b]=top++; the if(c<Bus[mp[a]][mp[b]]) +Bus[mp[a]][mp[b]]=bus[mp[b]][mp[a]]=c;//no-show diagram .... - } $ if(!STRCMP (Beg,en)) {//The thinking is really comprehensive ... When the position is the same as 0. $Puts"0"); - Continue; - } the if(!mp.count (Beg) | |! Mp.count (en)) {//When there is no car to start at the end of the -1 -Puts"-1");Wuyi Continue; the } - Dijskra (Mp[beg]); Wu //if (!strcmp (Beg,en)) puts ("1"); - if(D[mp[en]]==inf) puts ("-1"); About Elseprintf"%d\n", D[mp[en]]); $ } - return 0; -}
HDU Today (Dijskra)