HDU 2680 choose the best route

Source: Internet
Author: User
Choose the best routetime limit: 1000 msmemory limit: 32768 kbthis 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. inputthere 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.
Outputthe 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
Source2009 Zhejiang university computer research review (machine test part) -- simulate Dijkstra algorithm to solve the problem: this is a directed graph... Reverse Solving... The shortest path from the end point to the start point.
 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 Algorithm

 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

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.