Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

参照别人AC的程序,怎么自己写的就老实WA?太郁闷了,太蛋疼了!

Posted by caiyueqiao at 2011-11-21 12:18:22 on Problem 3259
#include<iostream>
#include<cstring>
#include<fstream>
#define INFINITY 10001
using namespace std;

class weight{
public:
	int S;
	int E;
	int T;
}edges[5200];
int nEdges;
int dist[1001]; 

//参数n,图中的节点总数
bool Bellman_Ford(int n) {
	bool beRelaxed;
	dist[1] = 0;
	for(int i = 0; i < n-1; ++i) { //n-1次循环
		beRelaxed = false;//标记循环中是否有松弛操作
		for(int j = 0; j < nEdges; ++j) {
			if(dist[edges[j].E] > dist[edges[j].S] + edges[j].T) {
				dist[edges[j].E] = dist[edges[j].S] + edges[j].T;
				beRelaxed = true;
			}
			if(!beRelaxed) break;//不再需要松弛了,剪枝
		}
	}
	for(int j = 0; j < nEdges; ++j) {
		if(dist[edges[j].E] > dist[edges[j].S] + edges[j].T) {
			return true; //存在负环,可以穿越时空
		}
	}
	return false;
}

int main() {
	//ifstream cin("in.txt");
	int F, N, M, W;
	int i;
	int u, v, w;
	cin>>F;
	while(F--) {
		nEdges = 0;
		for(i = 0; i < 1001; ++i) {
			dist[i] = INFINITY;
		}
		cin>>N>>M>>W;
		for(i = 0; i < M; ++i) {
			cin>>u>>v>>w;
			edges[nEdges].S = u;
			edges[nEdges].E = v;
			edges[nEdges++].T = w;
			edges[nEdges].S = v;
			edges[nEdges].E = u;
			edges[nEdges++].T = w;
		}
		for(i = 0; i < W; ++i) {
			cin>>u>>v>>w;
			edges[nEdges].S = u;
			edges[nEdges].E = v;
			edges[nEdges++].T = -w; //时间倒流
		}
		if(Bellman_Ford(N)) {
			cout<<"YES"<<endl;
		} else {
			cout<<"NO"<<endl;
		}
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator