POJ 3268 Silver Cow Party——dijkstra

网友投稿 570 2022-11-28

POJ 3268 Silver Cow Party——dijkstra

POJ 3268 Silver Cow Party——dijkstra

正向反向见图,然后两遍dijkstra

#include #include #include #include #include #include #include using namespace std;typedef pair P;const int INF = 0x3f3f3f3f;const int maxn = 1010;bool vis[maxn];int n, m, party, dis[2][maxn];vector

G[2][maxn];void dijkstra(int s, int flag) { for (int i = 1; i <= n; i++) dis[flag][i] = INF, vis[i] = false; dis[flag][s] = 0; priority_queue, greater

> q; q.push(make_pair(0, s)); while (!q.empty()) { P p = q-(); q.pop(); int u = p.second; if (vis[u]) continue; vis[u] = true; for (int i = 0; i < G[flag][u].size(); i++) { int v = G[flag][u][i].first, val = G[flag][u][i].second; if (dis[flag][v] > dis[flag][u] + val) { dis[flag][v] = dis[flag][u] + val; q.push(make_pair(dis[flag][v], v)); } } }}int main() { while (~scanf("%d %d %d", &n, &m, &party)) { for (int i = 1; i <= n; i++) { G[0][i].clear(), G[1][i].clear(); } while (m--) { int u, v, val; scanf("%d %d %d", &u, &v, &val); G[0][v].push_back(make_pair(u, val)); G[1][u].push_back(make_pair(v, val)); } dijkstra(party, 0); dijkstra(party, 1); int ans = 0; for (int i = 1; i <= n; i++) { if (i == party) continue; if (dis[0][i] != INF && dis[1][i] != INF) ans = max(ans, dis[0][i] + dis[1][i]); } printf("%d\n", ans); } return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:UVA 11136 Hoax or what——multiset
下一篇:POJ 3259 Wormholes——spfa判负环
相关文章

 发表评论

暂时没有评论,来抢沙发吧~