The journey of a manTime
limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 20267 Accepted Submission (s): 7081
Problem description Although grass son is a road crazy (is in Hangzhou electric stay for more than a year, incredibly still will be lost in campus, sweat ~), but grass son still very much like travel, because in the journey will meet a lot of people (Prince Charming, ^0^), many things, but also enrich their experience, You can also see the beautiful scenery ... Grass wants to go to many places, she wants to go to the Tokyo Tower to see the night scene, to Venice to see movies, to see the Taro on Yangmingshan, go to New York pure snow, go to Paris to drink coffee letter, to Beijing to visit Meng Jiangnu ... See the winter vacation is coming, so a large period of time, can not be wasted ah, must give yourself good put a fake, but also can not waste training ah, so grass son decided in the shortest time to a place where they want to go! Because the house of grass is in a small town, there is no train passing by, so she can only go to the neighboring city by train (poor Ah ~).
Input data has more than one group, the first line of each group is three integers t,s and D, indicating that there is a T road, and the grass home adjacent to the city of S, the grass wants to go where there are D;
Then there is a T-line, each line has three integer a,b,time, indicating that the drive between A and B cities is time hours; (1=< (A, B) there may be multiple paths between <=1000;a,b)
The next line of T+1 is the number of S, which indicates the city connected with the grass family;
The next line of T+2 has a number D, indicating that the grass wants to go to the place.
Output outputs the shortest time a grass can go to a favorite city.
Sample Input
6 2 31 3 51 4 72 8 123 8 44 9 129 10 21 28 9 10
Sample Output
9
Authorgrass
SOURCERPG Practice Competition
/* First use Dijkstra to solve the shortest path problem. The feeling is very good, obviously is the Tao basic problem. Hangzhou electric OJ with C can be AC, with g++ is not too. */#include <stdio.h> #include <string.h> #define M 1000000int dij[1010],s[1010];int road[1010][1010];int Max (int a,int b) {return a>b?a:b;} int Min (int a,int b) {return a<b?a:b;} void Dij (int start,int n) {int Path,i,j,k,v;memset (s,0,sizeof (s)); for (i=1;i<=n;i++) dij[i]=road[start][i];d ij[ Start]=0;s[start]=1;for (i=1;i<=n;i++) {path=m;for (j=1;j<=n;j++)//Find the point with the smallest distance from the remaining unreachable points. {if (!s[j]&&dij[j]<path) {path=dij[j];v=j;}} S[v]=1;for (k=1;k<=n;k++)//update the shortest distance from start to all remaining points. {if (!s[k]) dij[k]=min (Dij[k],dij[v]+road[v][k]);}} int main () {int T,s,d,i,j,x,y,z,n,min;int city[1010],aim[1010];while (scanf ("%d%d%d", &t,&s,&d)!=EOF) { for (i=0;i<1001;i++) {for (j=0;j<1001;j++) {if (i==j) road[i][j]=road[j][i]=0; else road[i][j]=m;}} for (i=0,n=0;i<t;i++) {scanf ("%d%d%d", &x,&y,&z), if (road[x][y]>z) road[x][y]=road[y][x]=z;if (Max (x, y) >n) N=max (x, y);} for (i=0;i<s;i++) scanf ("%d", &citY[i]); for (i=0;i<d;i++) scanf ("%d", &aim[i]); for (i=0,min=m;i<s;i++) {dij (city[i],n); for (j=0;j<d;j++) { Min=min (Min,dij[aim[j]);}} printf ("%d\n", Min);} return 0;}
A person's travel (Hangzhou Electric oj2066) (Dijkstra algorithm)