# BZOJ 1631: [Usaco2007 Feb]Cow Party (SPFA)

Posted by 111qqz on Saturday, May 21, 2016

## 1631: [Usaco2007 Feb]Cow Party

Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 670  Solved: 493 [Submit][Status][Discuss]

## Description

农场有N(1≤N≤1000)个牛棚，每个牛棚都有1只奶牛要参加在X牛棚举行的奶牛派对．共有M(1≤M≤100000)条单向路连接着牛棚，第i条踣需要Ti的时间来通过．牛们都很懒，所以不管是前去X牛棚参加派对还是返回住所，她们都采用了用时最少的路线．那么，用时最多的奶牛需要多少时间来回呢？

## 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

10

## HINT

1A,开心。

``````/* ***********************************************
Author :111qqz
Created Time :2016年04月04日 星期一 19时36分38秒
File Name :code/bzoj/1631.cpp
************************************************ */

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define fst first
#define sec second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(a,x) memset(a,x,sizeof(a))
typedef long long LL;
#define pi pair < int ,int >
#define MP make_pair

using namespace std;
const double eps = 1E-8;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
const int inf = 0x3f3f3f3f;
const int N=1E3+7;
const int M=1E5+7;
int n,m,X;

vector < pi > edge[N];
bool inq[N];
int d1[N],d2[N];
int ans;
{
int u,v,w;
}r[M];

void spfa( int s,int *d)
{
queue<int>q;
q.push(s);
inq[s] = true;
d[s] = 0 ;

while (!q.empty())
{
int u = q.front();
q.pop();
inq[u] = false;

int siz  = edge[u].size();

for ( int i = 0 ; i < siz ; i++)
{
int v = edge[u][i].fst;

if (d[v]>d[u] + edge[u][i].sec)
{
d[v] = d[u] + edge[u][i].sec;

if (inq[v]) continue;

inq[v] = true;
q.push(v);
}
}
}

}
int main()
{
#ifndef  ONLINE_JUDGE
freopen("code/in.txt","r",stdin);
#endif

scanf("%d%d%d",&n,&m,&X);
for ( int i = 1 ; i <= m ; i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
r[i].u = x;
r[i].v = y;
r[i].w = z;

edge[x].push_back(MP(y,z));  //单向边
}

ans = 0 ;
ms(inq,false);
ms(d1,0x3f);
spfa(X,d1);

for ( int i = 1 ; i <= n ; i++) edge[i].clear(); //重新反向连边再跑一次。

for ( int i = 1 ; i <= m ; i++)
{
edge[r[i].v].push_back(MP(r[i].u,r[i].w));
}
ms(inq,false);
ms(d2,0x3f);
spfa(X,d2);

int ans = -1;
for ( int i = 1 ; i <= n ; i++) ans = max(ans,d1[i]+d2[i]);

printf("%d\n",ans);

#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0;
}
``````