Time Limit: 2000MS |
| Memory Limit: 65536K |
Total Submissions: 8756 |
| Accepted: 3911 |
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 ofM (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; roadi requiresTi (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
Hint
#include<iostream>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAXN 1010
#define INF 0xFFFFFF
//spfa判断存在负权值的方法,一个点入队n次
struct node
{
int to;
int dis;
};
bool in[MAXN];
int s[MAXN];
vector<node> g[2][MAXN];
int n,m,x,dis[MAXN];
//int path[MAXN];
void spfa(int s,int x)
{
queue<int> q;
for(int i=1;i<=n;i++)
{
dis[i]=INF;
in[i]=false;
//path[i]=-1;
}
dis[s]=0;
in[s]=true;
//path[s]=s;
q.push(s);
while(!q.empty())
{
int tag=q.front();
in[tag]=false;
q.pop();
for(int i=0;i<g[x][tag].size();i++)
{
int j=g[x][tag][i].to;
if(dis[tag]+g[x][tag][i].dis<dis[j])
{
dis[j]=dis[tag]+g[x][tag][i].dis;
//path[j]=tag;
if(!in[j])
{
q.push(j);
in[j]=true;
}
}
}
}
}
void init()
{
int x,y,d;
node temp;
for(int i=1;i<=n;i++)
{
g[0][i].clear();
g[1][i].clear();
}
for(int i=1;i<=m;i++)
{
// cin>>x>>y>>d;
scanf("%d%d%d",&x,&y,&d);
temp.to=y;
temp.dis=d;
g[0][x].push_back(temp);
temp.to=x;
g[1][y].push_back(temp);
}
}
void solve()
{
int maxs=0;
spfa(x,0); //出边表的 SPFA
for(int i=1;i<=n;i++)
s[i]=dis[i];
spfa(x,1); //入边表
for(int i=1;i<=n;i++)
{
s[i]+=dis[i];
maxs=max(maxs,s[i]);
}
printf("%d\n",maxs);
}
int main()
{
while(scanf("%d%d%d",&n,&m,&x)!=EOF)
{
init();
solve();
}
return 0;
}