#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
//求多点到一定点的距离是, 可以建立反图,后就转化为求定点到各点的最短路径问题。
const int M = 1005;
const int inf = 99999999;
struct node {
int to;
int w;
node(int tto, int ww) : to(tto), w(ww){}
};
int n, m, x;
int vist[M];
vector<node>g[M], rg[M];
int dist1[M], dist2[M];
void spfa(int s, vector<node> gr[], int *dist) {
queue<int>que;
for(int i = 0; i <= n; i++)
dist[i] = inf;
memset(vist, 0, sizeof(vist));
vist[s] = 1;
que.push(s);
dist[s] = 0;
while(!que.empty()) {
int v = que.front();
que.pop();
vist[v] = 0;
for(int i = 0; i < (int)gr[v].size(); i++) {
node p = gr[v][i];
if(dist[p.to] > dist[v] + p.w) {
dist[p.to] = dist[v] + p.w;
if(!vist[p.to]) {
vist[p.to] = 1;
que.push(p.to);
}
}
}
}
}
int main()
{
int a, b, t;
while(scanf("%d%d%d", &n, &m, &x) != EOF) {
for(int i = 0; i < m; i++) {
scanf("%d%d%d", &a, &b, &t);
g[a].push_back(node(b, t));
rg[b].push_back(node(a, t));
}
spfa(x, g, dist1);
spfa(x, rg, dist2);
int ans = 0;
for(int i = 1; i <= n; i++) {
if(dist1[i] != inf&&dist2[i] != inf)
ans = max(ans, dist1[i]+dist2[i]);
}
printf("%d\n", ans);
}
return 0;
}
poj3668 spfa
最新推荐文章于 2020-07-10 21:58:40 发布