POJ 3268 Silver Cow Party(最短路dijkstra)
来源:互联网 发布:精通python网络爬虫pdf 编辑:程序博客网 时间:2024/05/29 15:12
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
解题思路:题意抽象出来意思是给一个有向图,然后求所有点经过v点,再回到原点的所有最短路径中最长的一条。解题方法就是用两个dijkstra,分别求i到v的最短距离和v到i的最短距离,然后找出最大的即可。
<code class="hljs handlebars has-numbering"><span class="xml"><span class="hljs-tag">求最短路径步骤算法步骤如下:<span class="hljs-attribute">G</span>=<span class="hljs-value">{V,E}</span><span class="hljs-attribute">1.</span> 初始时令 <span class="hljs-attribute">S</span>=<span class="hljs-value">{V0},T=V-S={其余顶点},T中顶点对应的距离值</span>若存在<<span class="hljs-attribute">V0</span>,<span class="hljs-attribute">Vi</span>></span>,d(V0,Vi)为<span class="hljs-tag"><<span class="hljs-title">V0,Vi</span>></span>弧上的权值若不存在<span class="hljs-tag"><<span class="hljs-title">V0,Vi</span>></span>,d(V0,Vi)为∞2. 从T中选取一个与S中顶点有关联边且权值最小的顶点W,加入到S中3. 对其余T中顶点的距离值进行修改:若加进W作中间顶点,从V0到Vi的距离值缩短,则修改此距离值重复上述步骤2、3,直到S中包含所有顶点,即W=Vi为止</span></code>
#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>using namespace std;const int maxn=1010;const int INF=0x7ffffff;int load[maxn][maxn];int v[maxn];int d1[maxn],d2[maxn];int n,m,x;int main(){ while(scanf("%d %d %d",&n,&m,&x) != EOF) { int a,b,c; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(i!=j) load[i][j]=INF; else if (i==j) load[i][i]=0; for(int i=0;i<m;i++){ scanf("%d %d %d",&a,&b,&c); load[a][b]=min(load[a][b],c); } memset(v,0,sizeof(v)); for(int i=1;i<=n;i++) d1[i]=INF; d1[x]=0; for(int i=0;i<n;i++) { int x,ans=INF; for(int y=1;y<=n;y++) if(!v[y] && d1[y]<=ans) {ans=d1[y]; x=y;} v[x]=1; for(int y=1;y<=n;y++) if(d1[y]>d1[x]+load[x][y]) d1[y]=d1[x]+load[x][y]; } memset(v,0,sizeof(v)); for(int i=1;i<=n;i++) d2[i]=INF; d2[x]=0; for(int i=0;i<n;i++) { int x,ans=INF; for(int y=1;y<=n;y++) if(!v[y] && d2[y]<=ans) {ans=d2[y];x=y;} v[x]=1; for(int y=1;y<=n;y++) if(d2[y]>d2[x]+load[y][x]) d2[y]=d2[x]+load[y][x]; } int ans=0; for(int i=1;i<=n;i++) { if(ans<d1[i]+d2[i]) ans=d1[i]+d2[i]; } cout<<ans << endl; } return 0;}
- poj 3268 Silver Cow Party(dijkstra最短路)
- POJ 3268 - Silver Cow Party(最短路dijkstra)
- POJ 3268 Silver Cow Party(最短路dijkstra)
- POJ 3268 Silver Cow Party (dijkstra来回最短路)
- POJ 3268 Silver Cow Party(最短路dijkstra)
- poj 3268 Silver Cow Party (dijkstra 求最短路)
- POJ 3268Silver Cow Party(dijkstra最短路)
- poj 3268 Silver Cow Party 最短路/dijkstra
- POJ 3268 Silver Cow Party 最短路 dijkstra
- POJ-3268-Silver Cow Party [最短路][Dijkstra]
- POJ 3268 Silver Cow Party(dijkstra求单源最短路)
- POJ - 3268 Silver Cow Party (往返最短路,Floyd,Dijkstra 2次优化)
- poj 3268 Silver Cow Party(最短路)
- POJ 3268 Silver Cow Party 最短路
- POJ 3268 Silver Cow Party 最短路
- POJ 3268 Silver Cow Party 最短路
- POJ 3268 Silver Cow Party 最短路
- POJ 3268 Silver Cow Party 最短路
- ie自带打印
- UVA 10954
- LeetCode Power of Two
- 玩转Node.js - 03. 第一个I/O!
- hdu5358
- POJ 3268 Silver Cow Party(最短路dijkstra)
- Android屏幕适配全攻略(最权威的官方适配指导)
- 使用Unity3D的50个技巧:Unity3D最佳实践(下)
- 解决DM8168上电自动启动,不需要手动登陆root
- 基于 SurfaceView 详解 android 幸运大转盘,附带实例app
- 补丁比较工具Darun Grim使用
- POJ 2029
- HDU 5389 Zero Escape(DP + 滚动数组)
- 华为OJ平台试题 —— 字符串:名字的漂亮度