POJ
来源:互联网 发布:秋天 知乎 编辑:程序博客网 时间:2024/06/06 03:04
问题描述
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
分析:
神奇的一题。
代码如下:
#include<cstdio>#include<algorithm>#include<queue>#include<vector>#include<cstring>const int maxn = 1000+10;const int maxe = 100000+10;const int INF = 10000000;using namespace std;struct edge{ int to,cost; edge(int to, int cost):to(to), cost(cost){}};typedef pair<int, int> P;int N, M, X;vector<vector<edge> > G(maxn);vector<vector<edge> > BG(maxn);int d[maxn];//d[i]为点i到party点并返回的距离和最小int b_d[maxn];void dijkstra(int s){ priority_queue<P,vector<P>,greater<P> > q; fill(d, d + N, INF); d[s] = 0; q.push(P(0,s)); while(!q.empty()) { P p = q.top(); q.pop(); int v = p.second; if(d[v] < p.first) continue; for(int i=0; i<G[v].size(); i++) { edge e = G[v][i]; if(d[e.to] > d[v] + e.cost) { d[e.to] = d[v] + e.cost; q.push(P(d[e.to],e.to)); } } }} int main(){ scanf("%d%d%d",&N, &M, &X); int a,b,t; --X; for(int i=0; i<M; i++) { scanf("%d%d%d",&a,&b,&t); --a; --b; G[a].push_back(edge(b, t)); BG[b].push_back(edge(a, t)); } dijkstra(X); G = BG; memcpy(b_d, d, sizeof(d)); dijkstra(X); for(int i=0; i<N; i++) { d[i] += b_d[i]; } int m = 0; for(int i=0; i<N; i++) { m = max(m, d[i]); } printf("%d\n",m); return 0;}
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- PopupWindow 设置动画无效
- 项目部署的一些思考
- 算法--美团--给出m*n个格子,每次只能右走一步或下走一步,打印出所有的路径
- Java web学习总结
- 理解css中的float
- POJ
- Android Studio 2.1 和 Unity3D 5.3.4 交互(二)
- STL 中的map与multimap
- python不足之处总结
- 402. Remove K Digits
- [C++][基础概念]main函数中的参数argc和argv的含义和用法
- [Swift]WKWebView用法介绍
- 中南大学第十一届大学生程序设计竞赛-COJ1898-复盘拉火车
- Hmz 的女装 详细题解