Poj 3268(Dijkstra)
来源:互联网 发布:阿里设置一键上传淘宝 编辑:程序博客网 时间:2024/05/19 03:24
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; roadi 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
Lines 2..M+1: Line i+1 describes road i with three space-separated integers:Ai, Bi, and Ti. The described road runs from farmAi to farm Bi, requiring Ti time units to traverse.
Output
Sample Input
4 8 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 2 3
Sample Output
10
Hint
Source
先在原图上求一次单源最短路,再把所有边反向,再求一次,把距离累加即可。
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<map>#include<cmath>#include<queue>using namespace std;const int maxn = 100000 + 5;const int INF = 1000000000;typedef long long LL;typedef pair<int,int> P;int n,m,x;struct Edge{ int from,to; LL dis;}e[maxn];LL ans[maxn];LL d[maxn];int vis[maxn];vector<Edge> G[maxn];void Dij(int x){ priority_queue<P,vector<P>,greater<P> > Q; memset(vis,0,sizeof(vis)); for(int i = 0;i <= n;i++) d[i] = INF; Q.push(P(0,x)); while(!Q.empty()){ P p = Q.top();Q.pop(); int id = p.second; LL dis = p.first; if(vis[id] == 1) continue; vis[id] = 1; d[id] = dis; for(int i = 0;i < G[id].size();i++){ Edge edgs = G[id][i]; int to = edgs.to; LL der = edgs.dis; if(d[to] > d[id] + der){ d[to] = d[id] + der; Q.push(P(d[to],to)); } } }}int main(){ while(scanf("%d%d%d",&n,&m,&x) != EOF){ for(int i = 0;i <= n;i++) G[i].clear(); for(int i = 0;i < m;i++){ scanf("%d%d%I64d",&e[i].from,&e[i].to,&e[i].dis); G[e[i].from].push_back(e[i]); } Dij(x); for(int i = 0;i <= n;i++) ans[i] = d[i]; for(int i = 0;i <= n;i++) G[i].clear(); for(int i = 0;i < m;i++){ swap(e[i].to,e[i].from); G[e[i].from].push_back(e[i]); } Dij(x); LL Max = 0; for(int i = 1;i <= n;i++){ ans[i] += d[i]; if(i != x){ Max = max(Max,ans[i]); } } printf("%d\n",Max); } return 0;}
- poj 3268 dijkstra
- POJ 3268 dijkstra算法
- poj 3268 dijkstra
- Poj 3268(Dijkstra)
- poj 3268 dijkstra
- POJ 3268 双向Dijkstra
- poj 3268 dijkstra
- poj 3268 Dijkstra运用
- poj 3268 (dijkstra)
- POJ 3268 基本dijkstra
- POJ 3268 (dijkstra变形)
- poj 3268 双向dijkstra
- poj 3268 dijkstra
- POJ 3268 Dijkstra算法
- Dijkstra POJ
- POJ 3268 Silver Cow Party(Dijkstra算法)
- POJ 3268 dijkstra()正反矩阵
- POJ 3268 - Silver Cow Party(dijkstra)
- 过滤器与拦截器的区别
- php连接SQLServer
- 重写、覆盖、重载、隐藏、多态几个概念的区别分析
- Java基础正则表达
- 函数式编程
- Poj 3268(Dijkstra)
- 转载_LINUX内核中的xx_initcall初始化标号
- 初识压缩感知Compressive Sensing
- 如何保护你的多个网络账号
- Android之Animation<2>
- sys-config使用
- 针对自定义组件上拉刷新下拉加载更多PullToRefreshView的分析(二)
- WPF/MVVM 快速入门教程
- c++编程错误vs2010