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
Line 1: Three space-separated integers, respectively: N, M, and X
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
Line 1: One integer: the maximum of time any one cow must walk.
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
Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.
题目大意:/给定一些路线和花费,然后N头牛,每头牛要求按照最短路走到X处,并用最短路在返回,问这些牛中所有路线里最大值是多少
思路:可以先从X用Dijkstra,再将道路逆置再来一下,求二者和最大即可
#include<algorithm>
#include<iostream>
#include<stdlib.h>
#include<cstring>
#include<cstdio>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
const int MAX = 1005;
const int INF = 0x3f3f3f3f;
struct A{
int arcs[MAX][MAX];//权值-->路径长度
int arcnum; //边的数目
int vexnum; //顶点的数目
}G;
bool visit[MAX]; //顶点的访问情况
int dis[MAX]; //出发点到其余点的最短距离
int X,v;
int ans[MAX];
//可以先从X用Dijkstra,再将道路逆置再来一下,求二者和最大即可
//最短路Dijkstra算法
void Dijkstra(int start){
for(int i=1 ; i<=G.vexnum ; i++){
dis[i] = G.arcs[start][i];
visit[i] = false;
}
visit[start] = true;
//再依次添加n-1个点,更新最短路
for(int i=1 ; i<=G.vexnum-1 ; i++){
int m = INF;
for(int j=1 ; j<=G.vexnum ; j++){
//找到距离最小的
if(!visit[j] && dis[j]<m){
m = dis[j];
v = j;
}
}
//找到最短距离的点
visit[v] = true;
for(int j=1 ; j<=G.vexnum ; j++){
//更新起始点到未访问点的最短距离
dis[j] = min(dis[j],dis[v]+G.arcs[v][j]);
}
}
for(int i=1 ; i<=G.vexnum ; i++){
ans[i] += dis[i];
}
}
int main(void){
while(cin>>G.vexnum>>G.arcnum>>X){
//初始化图
for(int i=1 ; i<=G.vexnum ; i++){
for(int j=1 ; j<=G.vexnum ; j++){
G.arcs[i][j] = (i==j)?0:INF;
}
}
for(int i=1 ; i<=G.vexnum ; i++){
ans[i] = 0;
}
//输入权值(路径长度)
for(int i=1 ; i<=G.arcnum ; i++){
int x,y,w;
cin>>x>>y>>w;
G.arcs[x][y] = w;
}
Dijkstra(X);
//逆置图
for(int i=1 ; i<=G.vexnum ; i++){
for(int j=1 ; j<i ; j++){
swap(G.arcs[i][j],G.arcs[j][i]);
}
}
Dijkstra(X);
int max=0;
for(int i=1 ; i<=G.vexnum ; i++){
if(ans[i] > max){
max = ans[i];
}
}
cout<<max<<endl;
}
return 0;
}