Holy Grail Bellman-Ford

Posted wstong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Holy Grail Bellman-Ford相关的知识,希望对你有一定的参考价值。

Holy Grail

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 305, maxm = 505;
 4 const int inf = 0x3f3f3f3f;
 5 struct edge {
 6     int v, w;
 7 };
 8 vector<edge> maps[maxn];
 9 int dis[maxn], n, m;
10 
11 bool BellmanFord(int s) {  // s为源点
12     fill(dis, dis+n, inf);
13     dis[s] = 0;
14     for (int i = 0; i < n-1; i++) {
15         for (int u = 0; u < n; u++) {
16             for (int j = 0; j < maps[u].size(); j++) {
17                 int v = maps[u][j].v;
18                 int w = maps[u][j].w;
19                 if (dis[u] + w < dis[v]) {
20                     dis[v] = dis[u] + w;
21                 }
22             }
23         }
24     }
25     for (int u = 0; u < n; u++) {
26         for (int j = 0; j < maps[u].size(); j++) {
27             int v = maps[u][j].v;
28             int w = maps[u][j].w;
29             if (dis[u] + w < dis[v])
30                 return false;
31         }
32     }
33     return true;
34 }
35 int main() {
36     int t; scanf("%d",&t);
37     while (t--) {
38         scanf("%d%d",&n,&m);
39         for (int i = 0; i < n; i++) maps[i].clear();
40 
41         for (int i = 1; i <= m; i++) {
42             int u, v, w; scanf("%d%d%d",&u,&v,&w);
43             maps[u].push_back(edge{v,w});
44         }
45         for (int i = 1; i <= 6; i++) {
46             int u, v; scanf("%d%d",&u,&v);
47             bool flag = BellmanFord(v);
48             printf("%d
",-dis[u]);
49 
50             //if (flag == false) continue;
51             maps[u].push_back(edge{v,-dis[u]});
52         }
53     }
54 }

 

以上是关于Holy Grail Bellman-Ford的主要内容,如果未能解决你的问题,请参考以下文章

The Preliminary Contest for ICPC Asia Nanjing 2019 H. Holy Grail(spfa或floyd,负环)

Googlebot是否真的不等待AJAX 请求完成?

grail中的多个文件上传

HDU4719-Oh My Holy FFF(DP线段树优化)

题解Oh My Holy FFF

hdu4719 Oh My Holy FFF[线段树优化dp]