码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3268 Silver Cow Party (Dijkstra)

时间:2018-07-30 21:28:42      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:ems   div   eof   integer   iostream   body   cst   center   tab   

                        Silver Cow Party
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions:28457   Accepted: 12928

Description

One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.

Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow‘s return route might be different from her original route to the party since roads are one-way.

Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?

Input

Line 1: Three space-separated integers, respectively: NM, and X 
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: AiBi, and Ti. The described road runs from farm Ai to farm Bi, requiring Titime units to traverse.

Output

Line 1: One integer: the maximum of time any one cow must walk.

Sample Input

4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3

Sample Output

10

Hint

Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.
 
题意:
单向图,每个农场都有一头牛,牛去开party,每个牛都要耗费一定的时间在路上,问花费最多的牛,要耗费多少时间
思路:
建立反向图,DIjkstra。
我还没有vector的Dijkstra的板子,就把这个当板子吧。
代码
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<vector>
 5 #include<queue>
 6 using namespace std;
 7 int n,m,x;
 8 const int inf = 1e9;
 9 struct node
10 {
11     int num;
12     int dis;
13 
14     bool operator < (const node x)const
15     {
16         return x.dis<dis;
17     }
18 };
19 vector<int>u[1024],w[1024];
20 vector<int>ux[1024],wx[1024];
21 int dis[1024],dis2[1024];
22 bool book[1024];
23 void dijkstra1()
24 {
25     fill(dis,dis+n+5,inf);
26     priority_queue<node>q;
27     memset(book,0,sizeof(book));
28     q.push(node{x,0});
29     dis[x]=0;
30     node exa;
31     while(!q.empty()){
32         exa=q.top();q.pop();
33         int t=exa.num;
34         if(book[t]){continue;}
35         book[t]=true;
36         int siz=u[exa.num].size();
37         for(int i=0;i<siz;i++){
38             if(!book[u[t][i]]&&dis[u[t][i]]>dis[t]+w[t][i]){
39                 dis[u[t][i]]=dis[t]+w[t][i];
40                 q.push(node{u[t][i],dis[u[t][i]]});
41             }
42         }
43     }
44 }
45 
46 void dijkstra2()
47 {
48     fill(dis2,dis2+n+5,inf);
49     priority_queue<node>q;
50     memset(book,0,sizeof(book));
51     q.push(node{x,0});
52     dis2[x]=0;
53     node exa;
54     while(!q.empty()){
55         exa=q.top();q.pop();
56         int t=exa.num;
57         if(book[t]){continue;}
58         book[t]=true;
59         int siz=ux[exa.num].size();
60         for(int i=0;i<siz;i++){
61             if(!book[ux[t][i]]&&dis2[ux[t][i]]>dis2[t]+wx[t][i]){
62                 dis2[ux[t][i]]=dis2[t]+wx[t][i];
63                 q.push(node{ux[t][i],dis2[ux[t][i]]});
64             }
65         }
66     }
67 }
68 
69 int main()
70 {
71     scanf("%d%d%d",&n,&m,&x);
72     int q,l,e;
73     for(int i=1;i<=m;i++){
74         scanf("%d%d%d",&q,&l,&e);
75         u[q].push_back(l);
76         w[q].push_back(e);
77         ux[l].push_back(q);
78         wx[l].push_back(e);
79     }
80     int ans=0;
81     dijkstra1();
82     dijkstra2();
83     for(int i=1;i<=n;i++){
84         ans=max(ans,dis[i]+dis2[i]);
85     }
86     printf("%d\n",ans);
87 }

 

POJ 3268 Silver Cow Party (Dijkstra)

标签:ems   div   eof   integer   iostream   body   cst   center   tab   

原文地址:https://www.cnblogs.com/ZGQblogs/p/9392456.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!