Silver Cow Party
TimeLimit:2000MS MemoryLimit:65536K
64-bit integer IO format:%lld
Problem 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
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.
SampleInput
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
SampleOutput
10
题意:有n个点m条边,a->b=c,一开始所有牛要到 x 点那去聚会, 聚会结束以后 所有牛又要回到自己的位置(除了x以外),在这些过程中求某只消耗的最大时间。
思路:正向跑一遍dijstra 将边置反再跑一遍 之后枚举得出最大值即可
下面献上我的low逼代码
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 1005;
const int INF = 0x3f3f3f3f;
int mp[N][N];
int vis[N],dist[N],d[N];
int n,m,x;
int dijstra()
{
for(int i=1; i<=n; i++)
{
vis[i]=0;
dist[i]=mp[x][i];///两个数组分别记录两次的dist值
d[i]=mp[i][x];///置反
}
int maxx=INF;
int v;
for(int i=1; i<=n; i++)
{
maxx=INF;
for(int j=1; j<=n; j++)
{
if(!vis[j] && dist[j]<maxx)///找出未经过的点中 最短的边
{
v=j;
maxx=dist[j];
}
}
vis[v]=1;
for(int j=1; j<=n; j++)
{
if(dist[j]>dist[v]+mp[v][j] && !vis[j])///松弛
dist[j]=dist[v]+mp[v][j];
}
}
for(int i=1; i<=n; i++)///清空标记数组
vis[i]=0;
for(int i=1; i<=n; i++)///同理
{
maxx=INF;
for(int j=1; j<=n; j++)
{
if(!vis[j] && d[j]<maxx)
{
v=j;
maxx=d[j];
}
}
vis[v]=1;
for(int j=1; j<=n; j++)
{
if(d[j]>d[v]+mp[j][v] && !vis[j])
d[j]=d[v]+mp[j][v];
}
}
int res=-1;
for(int i=1;i<=n;i++)///枚举得出答案
{
if(d[i]+dist[i]>res)
res=d[i]+dist[i];
}
return res;
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&x))
{
for(int i=1;i<=n;i++)///邻接矩阵建图基本操作
{
for(int j=1;j<=n;j++)
{
if(i!=j)
mp[i][j]=INF;
else
mp[i][j]=0;
}
}
for(int i=0; i<m; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
mp[a][b]=c;
}
printf("%d\n",dijstra());
}
return 0;
}