题意:
n个牛要到牛x处聚会,聚会完后回到自己的地方,每个牛走的都是最短路,问哪一个牛走的路用的时间最长。方法:以牛x为起点,通过dijiktra算法就可以求出每个牛的返程路径,但每个牛去牛x的路径不可能用floyd算法,所有我们把所有路径返过来,再以牛x为起点,求一次最短路径,就可以知道每个牛的去程时间了。
Silver Cow PartyTime Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
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
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<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<set>
#define INF 999999999
#define LL long long
#define mod 1000003
using namespace std;
struct Edge
{
int to, dist;
Edge(){}
Edge(int v, int d) :to(v), dist(d) {}
};
struct node
{
int from,to,dist;
};
node road[100005];//记录路径,之后反过来
int n, m;
vector<Edge> edges[1005];
bool done[1005];
int d[1005];
void init()
{
for (int i = 0; i <= n; i++)
edges[i].clear();
}
struct HeapNode
{
int d,u;
HeapNode(int dd,int uu)
{
d=dd;
u=uu;
}
bool operator < (const HeapNode& rhs) const {
return d>rhs.d;
}
};
void dijk(int s)
{
for(int i=1;i<=n;i++)
d[i]=INF;
d[s]=0;
priority_queue<HeapNode>q;
memset(done, 0, sizeof(done));
q.push(HeapNode( 0,s ));
while (!q.empty())
{
HeapNode x = q.top();
q.pop();
int u = x.u;
if (done[u]) continue;
done[u] = true;
for (int i = 0; i<edges[u].size(); i++)
{
Edge& e = edges[u][i];
if(d[e.to]>d[u]+e.dist)
{
d[e.to]=d[u]+e.dist;
q.push(HeapNode(d[e.to],e.to));
}
}
}
}
int main()
{
int mm;
int x;
while(scanf("%d%d%d",&n,&mm,&x)!=EOF)
{
int a, b, c;
init();
int dd[1005];
int sum=-1;
for (int i = 0; i<mm; i++)
{
scanf("%d %d %d", &a, &b, &c);
road[i].from=a,road[i].to=b,road[i].dist=c;
edges[a].push_back(Edge(b,c));
}
dijk(x);
for(int i=1;i<=n;i++)
dd[i]=d[i];
init();//记得再次初始化
for(int i=0;i<mm;i++)
{
a=road[i].from,b=road[i].to,c=road[i].dist;
edges[b].push_back(Edge(a,c));
}
dijk(x);
for(int i=1;i<=n;i++)
{
dd[i]+=d[i];
if(dd[i]>sum)
sum=dd[i];
}
printf("%d\n",sum);
}
}