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

Bellman_Ford最短路径,附源代码

Posted by lddhbu at 2011-08-06 15:52:11 on Problem 1062
#include<iostream>
using namespace std;
const int MAX=100;
const int INF=1<<28;
struct Edge{
	int x,y,w;
};
struct Point{
	int P,L;
	int minlevel,maxlevel;
};
Point point[MAX+1];
Edge edge[MAX*MAX+1];
int n,M,m;
int lowcase[MAX+1];
void init(int start){
	for(int i=1;i<=n;i++)
	{
		lowcase[i]=INF;
		point[i].minlevel=point[i].L-M;
		point[i].maxlevel=point[i].L+M;
	}
	lowcase[1]=0;
}
bool relax(Edge e){
	if(lowcase[e.y]>lowcase[e.x]+e.w&&point[e.y].L>=point[e.x].minlevel&&point[e.y].L<=point[e.x].maxlevel)
	{
		lowcase[e.y]=lowcase[e.x]+e.w;
		point[e.y].minlevel=max(point[e.y].minlevel,point[e.x].minlevel);
		point[e.y].maxlevel=min(point[e.y].maxlevel,point[e.x].maxlevel);
		return true;
	}
	return false;
}
int Bellman_Ford(int start,int end){
	int i,j;
	bool flag;
	init(start);
	for(i=1;i<n;i++)
	{
		flag=false;
		for(j=1;j<=m;j++)
			flag=relax(edge[j]);
		if(!flag)
			break;
	}
	return 1;
}
int main(){
	int i,j;
	int a,b,c,res;
	while(cin>>M>>n)
	{
		m=1;
		for(i=1;i<=n;i++)
		{
			scanf("%d%d%d",&a,&b,&c);
			point[i].P=a;
			point[i].L=b;
			for(j=1;j<=c;j++)
			{
				scanf("%d%d",&a,&b);
				edge[m].x=i;
				edge[m].y=a;
				edge[m].w=b;
				m++;
			}
		}
		m--;
		Bellman_Ford(1,n);
		res=INF;
		for(i=1;i<=n;i++)
			res=min(res,lowcase[i]+point[i].P);
		cout<<res<<endl;
	}
}

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