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

poj3268

时间:2020-01-26 19:04:18      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:cin   maximum   direct   short   color   max   cow   span   time   

原题:
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: N, M, and X
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time 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.
题意即给定一个单向图,要求从某个顶点出发,经过特定点x的最小闭环,并输出所有闭环中权值和的最大值;要求两个最短路,即从起点到x的最短路以及从x到起点的最短路;dijkstra算法所求的是"单源最短路",即从某个起点出发到所有的顶点的最短路;在单项图中,如果把所有的道路的起点和终点都颠倒,然后再用dijkstra算法处理的话,就能得到"从任一点出发到x的最短路".
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<algorithm>
 5 #include<vector>
 6 #include<queue>
 7 #include<cmath>
 8 
 9 using namespace std;
10 
11 const int MAX_V=1001;
12 const int MAX_E=100001;
13 const int INF=99999999;
14 
15 typedef pair<int,int> P;
16 
17 struct edge{
18     int from,to,cost;
19 };
20 
21 int d1[MAX_V];//从起点出发到任意一点的最短路
22 int d2[MAX_V];//从任一点出发到起点的最短路
23 
24 vector<edge>es1[MAX_E];
25 vector<edge>es2[MAX_E];
26 
27 //就是求单源最短路的
28 void dijkstra(int s,int* d,vector<edge> es[]){
29     fill(d,d+MAX_V,INF);
30     d[s]=0;
31     priority_queue<P,vector<P>,greater<P> > que;
32     que.push(P(0,s));
33     while(!que.empty()){
34         P p=que.top();que.pop();
35         int v=p.second;
36         if(d[v]<p.first)continue;
37         for(int i=0;i<es[v].size();i++){
38             edge e=es[v][i];
39             if(d[e.to]>d[e.from]+e.cost){
40                 d[e.to]=d[e.from]+e.cost;
41                 que.push(P(d[e.to],e.to));
42             }
43         }
44     }
45     
46 }
47 
48 int main() {
49     int n,m,x;
50     cin>>n>>m>>x;
51     while(m--){
52         edge temp;
53         scanf("%d%d%d",&temp.from,&temp.to,&temp.cost);
54         es1[temp.from].push_back(temp);
55         swap(temp.from,temp.to);
56         es2[temp.from].push_back(temp);
57     }
58     int ans=-1;
59     for(int i=1;i<=n;i++){
60         dijkstra(i,d1,es1);//从起点出发到任意一点的最短路
61         dijkstra(i,d2,es2);//从任一点出发到起点的最短路
62         ans=max(ans,d1[x]+d2[x]);
63     }
64     cout<<ans<<endl;
65     return 0;
66 }

 

poj3268

标签:cin   maximum   direct   short   color   max   cow   span   time   

原文地址:https://www.cnblogs.com/OldAtaraxi/p/12234490.html

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