POJ 3268 Silver Cow Party (Dijkstra)

                        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 }

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

时间: 2024-10-18 01:49:17

POJ 3268 Silver Cow Party (Dijkstra)的相关文章

POJ 3268 Silver Cow Party ( Dijkstra )

题目大意: 有N个农场每个农场要有一头牛去参加一个聚会,连接每个农场有m条路, 聚会地点是X,并且路是单向的.要求的是所有牛赶到聚会地点并且回到自己原先的农场所需要的最短时间. 题目分析: 其实就是以X为终点,求出X到其他每个点的距离, 再将图反存一下,在做一次最短路, 两次距离相加求出最长的时间. 这里是用Dijkstra写的,我们第一次用邻接矩阵写,第二次用邻接表,并且有优先队列优化 1 #include <iostream> 2 #include <cmath> 3 #inc

POJ 3268 Silver Cow Party(dijkstra+矩阵转置)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15460   Accepted: 7004 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 

POJ - 3268 Silver Cow Party(dijkstra技巧)

题目链接:http://poj.org/problem?id=3268 题意:n只奶牛(分别在1-n奶牛舍)分别从各自的奶牛舍出发到X奶牛舍,然后回到自己的奶舍(都以最短路),求出哪一只奶牛花费的距离最远. 题解:n到达1000,想直接Floyd肯定不行. 从X奶牛舍回家,就直接以X为源点最短路就可以了:从各自的奶牛舍去X奶牛舍可以以X为源点反方向进行求最短路. 1 #include <iostream> 2 #include <algorithm> 3 #include <

poj 3268 Silver Cow Party(dijkstra最短路)

题目链接:http://poj.org/problem?id=3268 题目大意:给你N个农场,在X农场要举办一个party,其它农场需要到X农场去,然后还要回来,问N个农场中距离最远的那个至少为多少?,给出的边为单向边... 思路:用dijkstra最初X农场到其它几个农场的最短距离,然后在把边反向,继续求出X到其它几个农场的最短距离,算出最大的那一个... code: #include<cstdio> #include<iostream> #include<cstring

POJ 3268 Silver Cow Party(Dijkstra算法求解来回最短路问题)

题目链接: https://vjudge.net/problem/POJ-3268 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 conn

poj 3268 Silver Cow Party , spfa , dijkstra

点击打开链接 两次求最短路(第二次把边反向求) 1.spfa //poj 3268 Silver Cow Party //SPFA #include <cstdio> #include <cstring> #include <queue> using namespace std; const int M = 100000 + 100; const int N = 1000 + 100; const int inf = 1<<25; struct Graph

poj 3268 Silver Cow Party(最短路)

poj 3268 Silver Cow Party 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

POJ 3268 Silver Cow Party(最短路dijkstra)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15451   Accepted: 6997 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

poj 3268 Silver Cow Party (最短路)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12913   Accepted: 5778 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