---
title: "poj3259 bellman水题"
author: "Midnight0101"
date: "2011-08-08T17:04:49Z"
tags: ["ACM", "poj"]
categories: ["编程语言"]
slug: "poj3259-bellman"
draft: false
lastmod: "2011-08-08T17:04:49Z"
id: 1142786
url: "https://midnight0101.iteye.com/blog/1142786"
---

poj3259\[url]http://poj.org/problem?id=3259\[/url] 最简单的bellman，判断是否有负环，n-1次松弛后若还能松弛，则有负环 \[code] #include #include #define INF 999999999 using namespace std; struct E { int u; int v; int w; }edges\[6000]; int n,m,w; int countEdge; int dist\[1000]; void addEdge(int e,int s,int t) { countEdge++; edges\[countEdge].u=e; edges\[countEdge].v=s; edges\[countEdge].w=t; } bool bellman() { int i,j; dist\[1]=0; for(i=2;i&lt;=n;i++) dist\[i]=INF; for(i=1;idist\[edges\[j].u]+edges\[j].w) dist\[edges\[j].v]=dist\[edges\[j].u]+edges\[j].w; } for(i=1;idist\[edges\[i].u]+edges\[i].w) return true; } return false; } int main() { int f; // ifstream cin("1.txt"); cin&gt;&gt;f; while(f--) { cin&gt;&gt;n&gt;&gt;m&gt;&gt;w; countEdge=0; int i,s,e,t; for(i=0;i&gt;s&gt;&gt;e&gt;&gt;t; addEdge(s,e,t); addEdge(e,s,t); } for(i=0;i&gt;s&gt;&gt;e&gt;&gt;t; addEdge(s,e,-t); } if(bellman()) cout&lt;&lt;"YES"&lt;