标签:
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 ≤ 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
Output
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
<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)
标签:
原文地址:http://blog.csdn.net/hellohelloc/article/details/47678867