poj3268Silver Cow Party:http://poj.org/problem?id=3268
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 16780 | Accepted: 7663 |
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
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
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
题意:有n个农场,共有m条单源路。告诉你x号农场举办party,这n个农场里的牛都要去参加,参加完还要返回各自的农场(去和返不一定是同一条路),每条牛都走最短路,求这些牛中所走的路中最长的一条。
思路:先将去的时候的各自最短路求出来,然后将回来的时候的最短路求出来,两项相加取最大值就可以了。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
using namespace std;
#define maxn 2000 + 10
#define INF 10000001
int n,x;
int grap[maxn][maxn],d[maxn],vis[maxn],ba[maxn];
void Dij()
{
for(int i = 1; i <= n; i++)
{
d[i] = grap[i][x];
ba[i] = grap[x][i];
}
memset(vis,0,sizeof(vis));
for(int i = 1; i <= n; i++)
{
int u ;
int minn = INF;
for(int j = 1; j <= n; j++)
if(!vis[j] && minn > d[j])
{
u = j;
minn = d[j];
}
vis[u] = 1;
for(int j = 1; j <= n; j++)
if(!vis[j] && (grap[j][u] + minn < d[j]))
d[j] = grap[j][u] + minn;
}
memset(vis,0,sizeof(vis));
for(int i = 1; i <= n; i++)
{
int u ,minn = INF;
for(int j = 1; j <= n; j++)
if(!vis[j] && ba[j] < minn)
{
u = j;
minn = ba[j];
}
vis[u] = 1;
for(int j = 1; j <= n; j++)
if(!vis[j] && ba[j] > grap[u][j] + minn)
ba[j] = grap[u][j] + minn;
}
}
int main()
{
int m,u,v,w;
scanf("%d%d%d",&n,&m,&x);
for(int i = 1; i <= n; i++)
for(int j = i; j <= n; j++)
grap[i][j] = grap[j][i] = (i == j?0:INF);
for(int i = 0; i < m; i++)
{
scanf("%d%d%d",&u,&v,&w);
grap[u][v] = min(grap[u][v],w);
}
Dij();
int ans = 0;
for(int i = 1; i <= n; i++)
ans = max(ans,d[i] + ba[i]);
printf("%d\n",ans);
return 0;
}
参考: http://blog.csdn.net/wangjian8006/article/details/7872048

275

被折叠的 条评论
为什么被折叠?



