HDU 2112 HDU today (Dijkstra algorithm)

Source: Internet
Author: User

HDU TodayTime limit:15000/5000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 13952 Accepted Submission (s): 3264



Problem description through the brocade SAC Help, Sea East group finally through the crisis, since, HDU development has been downwind, to 2050 years, the group has been considerable scale. Reportedly entered 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 (if each bus stops at starting point and terminus only, and will open at any time).



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 Xu General start. The end of his destination;
Then there are n rows, each line has a station name S. Station name E, and the time integer T (0<t<100) from S to E (each place name is a string of no more than 30 length).


Note: No more than 150 place names in a set of data.


Suppose N==-1, which indicates the end of the input.

Output assumes that Xu always arrives at the destination and 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 * * Still live a happy life. -All the ending-

Authorlgx
SOURCEACM Program Design _ Final Exam (time has been set!!)


Recommendlcy | We have carefully selected several similar problems for you:1874 1217 2680 1142 1385

is mainly the early processing difficulties AH. I'm just trying to fix it.
Code: 859MS (Code not optimized)

#include <iostream> #include <string>using namespaceStd;#define MIN (A, b) (a<b?)

A:B) #define MAX (A, B) (A&GT;B?A:B) #define M 160#define INF 0xfffff;intDis[M],Map[M][M],N; Char A[M][M]; int Find (char S []) { int I; for (I=1;I<=N;I++) if (strcmp(S,A[I])==0)//Assuming that it has occurred, use subscript directly.

returnI;strcpy(A[++N],S); Did not appear, will increase the point, the number of terminal +1, the site subscript for n+1. return N; Returns the number of existing sites. }void Dijkstra (int X )//template. { bool V[M]; int I,J,K,Minx;Memset(V,0 ,sizeof( V)); for (I=1;I<=N;I++)Dis[I]=Map[X][I]; V[X]=1; for (I=1;I<N;I++) {Minx=Inf; for (J=1;J<=N;J++) //Find the point that is currently able to determine the shortest path.

the core of Dijkstra (greed). If(!V[J] &&Dis[J]<Minx) {K=J;Minx=Dis[J]; }V[K]=1;Minx=Inf; for (J=1;J<=N;J+ +)//update weights, which is the current shortest-path update.

if(! V[J] &&Dis[K]<Minx) {Dis[J]=Min(Dis[J],Dis[K]+Map[K][J]); } }} int main() { int N; while (scanf("%d",&N),N!=-1 ) { char S[M]; int R=Inf; int Start,End,A,B,Time,I,J;N=0;Cin>>S;Start =Find(S);Cin>>S;End =Find(S); for (I=0;I<M;I++) for (J=0;J<M;J++)Map[I][J] = (I==J?

0:R); for (I=0;I<N;I++) {Cin>>S;A=Find(S);Cin>>S;B=Find(S);Cin>>Time;Map[A][B]=Map[B][A]=Min(Map[A][B],Time); }Dijkstra(Start); if (Dis[End]!=R)cout<<Dis[End]<<Endl; else if (Start==End)cout<<"0"<<Endl; Else cout<<"-1"<<Endl; } return 0;}

This problem can also be handled in the input. However, there is no optimization algorithm after the foundation of the New school discipline.

Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

HDU 2112 HDU today (Dijkstra algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.