标签:
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
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
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 1005;
const int M = 500005;
int n, m, s;
struct Edge {
int from, to, dist;
};
vector<Edge> edges, edgess;
vector<int> G[M], G2[M];
int vis[N], d[N], rec[N];
int L[N];
void init() {
for (int i = 0; i <= n; i++) {
L[i] = 0;
vis[i] = 0;
}
for (int i = 0; i < (n / 2) * n; i++) G[i].clear();
for (int i = 0; i < (n / 2) * n; i++) G2[i].clear();
edges.clear();
edgess.clear();
}
void addEdge(int from, int to, int dist) {
edges.push_back((Edge){from, to, dist});
edgess.push_back((Edge){to, from, dist});
int pos = edges.size();
G2[to].push_back(pos - 1);
G[from].push_back(pos - 1);
}
void input() {
int u, v, len;
for (int i = 0; i < m; i++) {
scanf("%d %d %d", &u, &v, &len);
addEdge(u, v, len);
}
}
void SPFA(vector<int> *GG, vector<Edge> e) {
memset(vis,0,sizeof(vis));
for (int i = 0; i < N; i++) d[i] = INF;
for (int i = 0; i < N; i++) rec[i] = INF;
d[s] = 0;
rec[s] = 0;
vis[s] = 1;
queue<int> Q;
Q.push(s);
while (!Q.empty()) {
int u = Q.front();
Q.pop();
vis[u] = 0;
for (int i = 0; i < GG[u].size(); i++) {
int v = e[GG[u][i]].to;
if (d[v] > d[u] + e[GG[u][i]].dist) {
d[v] = d[u] + e[GG[u][i]].dist;
rec[v] = rec[u] + 1;
if (!vis[v]) {
vis[v] = 1;
Q.push(v);
}
} else if (d[v] == d[u] + e[GG[u][i]].dist) {
if (rec[v] > rec[u] + 1) {
rec[v] = rec[u] + 1;
if (!vis[v]) {
vis[v] = 1;
Q.push(v);
}
}
}
}
}
}
int main() {
int x, Max;
while (scanf("%d %d %d", &n, &m, &x) == 3) {
Max = 0;
init();
input();
s = x;
SPFA(G, edges);
for (int i = 1; i <= n; i++) {
if (i == x) continue;
L[i] += d[i];
}
SPFA(G2, edgess);
for (int i = 1; i <= n; i++) {
if (i == x) continue;
L[i] += d[i];
}
for (int i = 1; i <= n; i++) {
Max = max(L[i], Max);
}
printf("%d\n", Max);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
poj 3268 Silver Cow Party(最短路)
标签:
原文地址:http://blog.csdn.net/llx523113241/article/details/47059039