首页 > 代码库 > HDU 2680 Choose the best route

HDU 2680 Choose the best route

Choose the best route

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 2680
64-bit integer IO format: %I64d      Java class name: Main
 
 
One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend’s home as soon as possible . Now give you a map of the city’s traffic route, and the stations which are near Kiki’s home so that she can take. You may suppose Kiki can change the bus at any station. Please find out the least time Kiki needs to spend. To make it easy, if the city have n bus stations ,the stations will been expressed as an integer 1,2,3…n.
 

Input

There are several test cases. 
Each case begins with three integers n, m and s,(n<1000,m<20000,1=<s<=n) n stands for the number of bus stations in this city and m stands for the number of directed ways between bus stations .(Maybe there are several ways between two bus stations .) s stands for the bus station that near Kiki’s friend’s home.
Then follow m lines ,each line contains three integers p , q , t (0<t<=1000). means from station p to station q there is a way and it will costs t minutes .
Then a line with an integer w(0<w<n), means the number of stations Kiki can take at the beginning. Then follows w integers stands for these stations.
 

Output

The output contains one line for each data set : the least time Kiki needs to spend ,if it’s impossible to find such a route ,just output “-1”.
 

Sample Input

5 8 51 2 21 5 31 3 42 4 72 5 62 3 53 5 14 5 122 34 3 41 2 31 3 42 3 211

Sample Output

1-1

Source

2009浙江大学计算机研考复试(机试部分)——全真模拟
 
Dijkstra算法
解题:这题是有向图。。。逆向求解。。。从终点求到起点的最短路。
 
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 200100;18 struct arc{19     int to,w,next;20     arc(int x = 0,int y = 0,int z = 0){21         to = x;22         w = y;23         next = z;24     }25 };26 arc e[maxn];27 int head[10100],d[10100],station[10100],tot,n,m,s;28 bool done[10100];29 void add(int u,int v,int w){30     e[tot] = arc(v,w,head[u]);31     head[u] = tot++;32 }33 priority_queue< pii,vector< pii >,greater< pii > >q;34 void dijkstra(int s){35     while(!q.empty()) q.pop();36     d[s] = 0;37     q.push(make_pair(d[s],s));38     while(!q.empty()){39         int u = q.top().second;40         q.pop();41         if(done[u]) continue;42         done[u] = true;43         for(int i = head[u]; i != -1; i = e[i].next){44             if(d[e[i].to] > d[u]+e[i].w) {45                 d[e[i].to] = d[u]+e[i].w;46                 q.push(make_pair(d[e[i].to],e[i].to));47             }48         }49     }50 }51 int main() {52     int i,j,k,u,v,w;53     while(~scanf("%d %d %d",&n,&m,&s)){54         for(i = 1; i <= n; i++){55             done[i] = false;56             head[i] = -1;57             d[i] = INF;58         }59         for(tot = i = 0; i < m; i++){60             scanf("%d %d %d",&u,&v,&w);61             add(v,u,w);62         }63         dijkstra(s);64         scanf("%d",&k);65         int ans = INF;66         for(i = 0; i < k; i++){67             scanf("%d",&w);68             ans = min(ans,d[w]);69         }70         ans == INF?puts("-1"):printf("%d\n",ans);71     }72     return 0;73 }
View Code

 

spfa算法

 1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #define INF 0x3f3f3f3f 5 using namespace std; 6 int mp[1010][1010],n,m,s,d[1010]; 7 bool in[1010]; 8 queue<int>q; 9 void spfa() {10     for(int i = 0; i <= n; i++) {11         d[i] = INF;12         in[i] = false;13     }14     while(!q.empty()) q.pop();15     d[s] = 0;16     in[s] = true;17     q.push(s);18     while(!q.empty()) {19         int u = q.front();20         q.pop();21         in[u] = false;22         for(int i = 1; i <= n; i++) {23             if(mp[u][i] < INF && d[i] > d[u]+mp[u][i]) {24                 d[i] = d[u]+mp[u][i];25                 if(!in[i]) {26                     in[i] = true;27                     q.push(i);28                 }29             }30         }31     }32 }33 int main() {34     int i,j,k,u,v,w;35     while(~scanf("%d %d %d",&n,&m,&s)) {36         for(i = 0; i <= n; i++)37             for(j = 0; j <= n; j++)38                 mp[i][j] = INF;39         for(i = 0; i < m; i++) {40             scanf("%d %d %d",&u,&v,&w);41             if(mp[v][u] > w) mp[v][u]  = w;42         }43         spfa();44         scanf("%d",&k);45         int ans = INF;46         for(i = 0; i < k; i++) {47             scanf("%d",&w);48             ans = min(ans,d[w]);49         }50         ans == INF?puts("-1"):printf("%d\n",ans);51     }52     return 0;53 }
View Code

 

HDU 2680 Choose the best route