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?
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.
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
奶牛派对:有分别来自 N 个农场的 N 头牛去农场 X 嗨皮,农场间由 M 条有向路径连接。每头牛来回都挑最短的路走,求它们走的路的最大长度?
有编号为1-N的牛,它们之间存在一些单向的路径。给定一头牛的编号,其他牛要去拜访它并且拜访完之后要返回自己原来的位置,求这些牛中所花的最长的来回时间是多少。
思路:反着来,从点x出发,即单源最短路dijstra问题,那么来回怎么样,我们只要构一条反向边
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;
const int N = 1111;
const int inf = 0x3f3f3f3f;
struct P
{
int v,c;
P(int v,int c):v(v),c(c){}
P(){}
bool operator < (const P& t1)const
{
return c>t1.c;
}
};
priority_queue<P>que;
vector<P>vec[N],rvec[N];
int dp[N],rdp[N];
int s;
void dij1()
{
int i,j;
P p = P(s,0);
dp[s]=0;
que.push(p);
while(!que.empty()) {
p = que.top();
que.pop();
if(p.c>dp[p.v]) continue;
for(i=0;i<vec[p.v].size();i++) {
P tmp = vec[p.v][i];
if(dp[tmp.v]>dp[p.v]+tmp.c) {
dp[tmp.v]=dp[p.v]+tmp.c;
que.push(P(tmp.v,dp[tmp.v]));
}
}
}
}
void dij2()
{
int i,j;
P p = P(s,0);
rdp[s]=0;
while(!que.empty()) que.pop();
que.push(p);
while(!que.empty()) {
p = que.top();
que.pop();
if(p.c>rdp[p.v]) continue;
for(i=0;i<rvec[p.v].size();i++) {
P tmp = rvec[p.v][i];
if(rdp[tmp.v]>rdp[p.v]+tmp.c) {
rdp[tmp.v]=rdp[p.v]+tmp.c;
que.push(P(tmp.v,rdp[tmp.v]));
}
}
}
}
int main()
{
int n,m,i,j,u,v,c;
scanf("%d%d%d",&n,&m,&s);
fill(dp,dp+n+1,inf);
fill(rdp,rdp+n+1,inf);
while(m--) {
scanf("%d%d%d",&u,&v,&c);
vec[u].push_back(P(v,c));
rvec[v].push_back(P(u,c));
}
dij1();
dij2();
int ans = 0;
for(i=1;i<=n;i++) ans=max(ans,dp[i]+rdp[i]);
printf("%d\n",ans);
return 0;
}