# 洛谷P1821 [USACO07FEB]银牛派对Silver Cow Party

## 题目描述

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?

## 输入输出样例

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


10

## 说明

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>

using namespace std;

const int maxn = 1010,maxm = 100010,inf = 0x7ffffff;

int n,m,x,head[maxn],to[maxm],nextt[maxm],tot = 1,a[maxm],b[maxm],t[maxm],ans[maxn],w[maxm],d[maxn],vis[maxn];
int maxx;

void add(int x,int y,int z)
{
w[tot] = z;
to[tot] = y;
}

void spfa()
{
memset(vis,0,sizeof(vis));
queue <int> q;
q.push(x);
for (int i = 1; i <= n; i++)
d[i] = inf;
vis[x] = 1;
d[x] = 0;
while (!q.empty())
{
int u = q.front();
q.pop();
vis[u] = 0;
for (int i = head[u];i;i = nextt[i])
{
int v = to[i];
if (d[v] > d[u] + w[i])
{
d[v] = d[u] + w[i];
if (!vis[v])
{
vis[v] = 1;
q.push(v);
}
}
}
}
}

int main()
{
scanf("%d%d%d",&n,&m,&x);
for (int i = 1; i <= m; i++)
{
scanf("%d%d%d",&a[i],&b[i],&t[i]);
}
spfa();
for (int i = 1; i <= n; i++)
ans[i] = d[i];
tot = 1;
for (int i = 1; i <= m; i++)
spfa();
for (int i = 1; i <= n; i++)
{
ans[i] += d[i];
maxx = max(maxx,ans[i]);
}
printf("%d\n",maxx);

return 0;
}

posted @ 2017-09-12 21:41 zbtrs 阅读(...) 评论(...) 编辑 收藏