Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 18581 | Accepted: 8491 |
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
给出n头牛的住址,再给出一头牛要举办聚会,所有牛都得去,所有的路都是单向的,所以去的路和回的路不同,问所有牛来回最短路最大的那个是多少
由于图是有向图,画画图就能理解,从聚会点出来往回走,可以用最短路模板,而求去聚会的路上的时间时,依次遍历太耗时,我没试,但可以反着想,把终点和起点颠倒一下,把去看成回,把单向路箭头方向改变,再用一次模板,就可以一次求出去参加聚会的时间,然后取和最大的那个就行了
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxn 100010
#define inf 0x3f3f3f3f
using namespace std;
int m, n, ans, cnt, num1, num2;
int dis[maxn], head1[maxn], head2[maxn], dist[maxn];
bool vis[maxn];
struct node{
int to, val, next;
}edge[maxn], Edge[maxn];
void add(int u, int v, int w){
edge[num1].to = v;
edge[num1].val = w;
edge[num1].next = head1[u];
head1[u] = num1++;
Edge[num2].to = u;
Edge[num2].val = w;
Edge[num2].next = head2[v];
head2[v] = num2++;
}
void bfs(int t){
memset(dis, inf, sizeof(dis));
memset(vis, false, sizeof(vis));
queue<int >Q;
dis[t] = 0;
Q.push(t);
while(!Q.empty()){
int u = Q.front();
Q.pop();
vis[u] = false;
for(int i = head1[u]; i != -1; i = edge[i].next){
int v = edge[i].to;
if(dis[v] > dis[u] + edge[i].val){
dis[v] = dis[u] + edge[i].val;
if(!vis[v]){
vis[v] = true;
Q.push(v);
}
}
}
}
}
void bfs2(int t){
memset(dis, inf, sizeof(dis));//手贱,写成了sizeof(inf)-----生无可恋.jpg
memset(vis, false, sizeof(vis));
queue<int >Q;
dis[t] = 0;
Q.push(t);
while(!Q.empty()){
int u = Q.front();
Q.pop();
vis[u] = false;
for(int i = head2[u]; i != -1; i = Edge[i].next){
int v = Edge[i].to;
if(dis[v] > dis[u] + Edge[i].val){
dis[v] = dis[u] + Edge[i].val;
if(!vis[v]){
vis[v] = true;
Q.push(v);
}
}
}
}
}
int main(){
int a, b, c, x, y, z;
scanf("%d%d%d", &x, &y, &z);
num1 = num2 = 0;
memset(head1, -1, sizeof(head1));
memset(head2, -1, sizeof(head2));
memset(dist, 0, sizeof(dist));
for(int i = 0; i < y; i++){
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
bfs(z);
for(int i = 1; i <= x; i++){
if(i != z){
dist[i] = dis[i];
}
}
bfs2(z);
for(int i = 1; i <= x; i++){
if(i != z){
dist[i] += dis[i];
}
}
int ans = 0;
for(int i = 1; i <= x; i++){
if(i != z){
if(ans < dist[i]){
ans = dist[i];
}
}
}
cout<< ans <<endl;
return 0;
}