Topic links
Https://icpcarchive.ecs.baylor.edu/external/68/6800.pdf
Bellman-ford according to the template hit a section, can find the negative power loop, the problem is to judge 0 points in the negative right loop, so wrote a memory of Dfs.
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <vector >using namespace std; #define MAX 0x3f3f3f3f #define N 10100 int nodenum, edgenum; typedef struct Edge {int u, v;int cost;} Edge; Edge edge[n];int Dis[n];bool vis[n], dp[n];vector<int> g[n];bool dfs (int n) {if (N = = 0) return true;if (Vis[n]) Retu RN Dp[n];vis[n] = true;for (int i = 0; i < g[n].size (); i++) {if (Dfs (G[n][i])) {return dp[n] = true;}} return dp[n]=false;} BOOL Bellman_ford () {for (int i = 0; i < nodenum; ++i) dis[i] = (i = = 0 = 0:max), for (int i = 0; i < nodenum-1; ++i) for (int j = 0; j < Edgenum; ++j) if (DIS[EDGE[J].V] > dis[edge[j].u] + edge[j].cost) {DIS[EDGE[J].V] = Dis[edge [j].u] + edge[j].cost;} BOOL flag = 1;for (int i = 0; i < edgenum; ++i) if (DIS[EDGE[I].V] > dis[edge[i].u] + edge[i].cost) {if (Dfs (edge[i].u ) {flag = 0;break;}} return flag;} int main () {int casen;cin >> casen;for (int cas = 1; CAs <= casen;cas++) {SCANF ("%d%d", &nodenum, &edgenum), memset (dis, 0, sizeof (DIS)), memset (Vis, false, sizeof (VIS)), memset (DP, FALSE, sizeof (DP));DP [0] = vis[0] = true;for (int i = 0; i < N; i++) g[i].clear (); for (int i = 0; i < Edgenum; i++) {scanf ("% D%d%d ", &edge[i].u, &EDGE[I].V, &edge[i].cost); G[edge[i].u].push_back (EDGE[I].V);} int Ok=bellman_ford (); if (!ok) printf ("Case #%d:possible\n", CAs), else printf ("Case #%d:not possible\n", CAs);}}
UVALive-6800 the Mountain of Gold? (Bellman-ford find negative power loop, DFS)