Travel by one (Dijkstra)

Problem Description Although Caoer is a road crazy (i.e. he has been in Hangzhou Electric Power for more than a year, and even lost his way in the ca...


Problem Description

Although Caoer is a road crazy (i.e. he has been in Hangzhou Electric Power for more than a year, and even lost his way in the campus, Khan ~), Caoer still likes to travel, because he will meet many people (prince charming, ^ 0 ^), many things, rich experience and beautiful scenery during the journey Cao'er wants to go to many places. She wants to go to the Tokyo Tower to see the night scene, to Venice to see the movie, to Yangmingshan to see the taro, to New York to see the snow purely, to Paris to drink coffee and write letters, to Beijing to visit Meng Jiangnu Seeing that winter vacation is coming, we can't waste such a large period of time. We must give ourselves a good holiday, but we can't waste the training. So cao'er decided to go where he wanted to go in the shortest time! Because cao'er's home is in a small town, there is no train passing by, so she can only go to the neighboring city to take the train.

Input

There are multiple groups of input data. The first row of each group is three integers T, S and D, indicating that there are t roads, S in cities adjacent to Caoer'S home, and D in places where Caoer wants to go;
Then there are T lines, each line has three integers a,b, time, which means that the distance between a and B cities is time hour; (1 = < a,b) < 1000; there may be multiple paths between a and b)
There are S numbers in the next T+1 line, indicating the city connected with Caoer'S home;
The next T+2 line has D numbers, indicating that the grass wants to go to places.

Output

The shortest time that grass can go to a favorite city.

Sample Input

6 2 3 1 3 5 1 4 7 2 8 12 3 8 4 4 9 12 9 10 2 1 2 8 9 10

Sample Output

9

At first glance, it is the shortest circuit with multiple sources. floyd, the result. TLE,tle,tle,tle......... Well, Dijkstra is better, but I guess it has to time out. As a result, AC is still not familiar with the shortest path algorithm.

AC Code

#include<iostream> #include<iomanip> //#include<bits/stdc++.h> #include<cstdio> #include<cmath> #include<string> #include<algorithm> #include<cstring> #include<vector> #include<sstream> #include<queue> #include<set> #define PI 3.14159265358979 #define LL long long const int INF=0x3f3f3f3f; using namespace std; int w[1010][1010]; int ans[1001]; int num[1001]; int d[1001]; bool vis[1001]; void init() { memset(w,INF,sizeof(w)); memset(d,INF,sizeof(d)); for(int i=1;i<=1001;++i) w[i][i]=0; } void Dijkstra(int u,int n){//Shortest path template memset(vis,0,sizeof(vis)); for(int i=1;i<=n;++i){ d[i]=w[u][i]; } vis[u]=1; for(int r=1;r<=n;++r) { int p=0; for(int i=1;i<=n;++i) { if(!vis[i]&&d[p]>d[i]){ p=i; } } if(p==0) return ; vis[p]=1; for(int i=1;i<=n;++i){ if(d[i]>d[p]+w[p][i]){ d[i]=d[p]+w[p][i]; } } } } int main() { //freopen("input.txt","r",stdin); int T,n,m,S,D,f,t,dis; while(scanf("%d%d%d",&T,&n,&m)==3) { init();int MAX=-INF; for(int i=1;i<=T;++i) { scanf("%d%d%d",&f,&t,&dis); if(w[f][t]>dis) w[f][t]=w[t][f]=dis;//Take care of multiple groups, take the minimum MAX=max(MAX,max(f,t));//The position of this topic is not fixed, take the largest } for(int i=1;i<=n;++i) scanf("%d",&ans[i]); for(int i=1;i<=m;++i) scanf("%d",&num[i]); int MIN=INF; for(int i=1;i<=n;++i) { int H=ans[i]; for(int j=1;j<=m;++j) { int G=num[j]; Dijkstra(H,MAX);//shortest path MIN=min(MIN,d[G]); } } printf("%d\n",MIN); } }

5 November 2019, 14:27 | Views: 7164

Add new comment

For adding a comment, please log in
or create account

0 comments