UVALive-6800 the Mountain of Gold? (Bellman-ford find negative power loop, DFS)

Source: Internet
Author: User

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, &AMP;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.