bellman-ford 学习总结性文档

bellman-ford和dijkstra一样,是求最短路径的算法,和dijkstra一样,都是输入一个点,输出这个点到其他所有点的最短路径,这个算法的通用性更广,因为它可以很好地支持包含负权的图,而dijkstra不行,算法如下使用:

输入:输入一个起点;

输出:输出这个起点到所有点的最短路径(如果有);输出错误信息(如果存在负权回路)。

算法过程如下:

初始化各边最短路径长度:无穷大

初始化起点最短路径长度:0

for i=1->v-1(V为点的数量)

{

for j=1->m(m为边的数量)

   { 

        if 该边起点的当前已知最短长度+该边长度<该边终点当前已知最短长度

          then   更新终点的最短长度

    }

}

执行这些步骤之后,如果没有负权回路,那么算法应该已经求出了起点到其他所有点的最短路径,如果有负权回路,那么必定不能求出,接下来需要查看是否存在负权回路:

for j=1->m(m为边的数量)

   { 

        if 该边起点的当前已知最短长度+该边长度<该边终点当前已知最短长度

          then   存在负权回路,结果无效

    }

如果到这一步还没发现负权回路,那么可以确定没有负权回路,算法完毕。

算法总体来说还是比较直接的,写起来并不费力,下面举个例子,poj1860.

Description

Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the same pair of currencies. Each point has its own exchange rates, exchange rate of A to B is the quantity of B you get for 1A. Also each exchange point has some commission, the sum you have to pay for your exchange operation. Commission is always collected in source currency. 

For example, if you want to exchange 100 US Dollars into Russian Rubles at the exchange point, where the exchange rate is 29.75, and the commission is 0.39 you will get (100 – 0.39) * 29.75 = 2963.3975RUR. 

You surely know that there are N different currencies you can deal with in our city. Let us assign unique integer number from 1 to N to each currency. Then each exchange point can be described with 6 numbers: integer A and B – numbers of currencies it exchanges, and real R
AB, C
AB, R
BA and C
BA – exchange rates and commissions when exchanging A to B and B to A respectively. 

Nick has some money in currency S and wonders if he can somehow, after some exchange operations, increase his capital. Of course, he wants to have his money in currency S in the end. Help him to answer this difficult question. Nick must always have non-negative sum of money while making his operations. 

Input

The first line of the input contains four numbers: N – the number of currencies, M – the number of exchange points, S – the number of currency Nick has and V – the quantity of currency units he has. The following M lines contain 6 numbers each – the description of the corresponding exchange point – in specified above order. Numbers are separated by one or more spaces. 1<=S<=N<=100, 1<=M<=100, V is real number, 0<=V<=10
3

For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10
-2<=rate<=10
2, 0<=commission<=10
2

Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this sequence. You may assume that ratio of the numeric values of the sums at the end and at the beginning of any simple sequence of the exchange operations will be less than 10
4

Output

If Nick can increase his wealth, output YES, in other case output NO to the output file.

Sample Input

3 2 1 20.0
1 2 1.00 1.00 1.00 1.00
2 3 1.10 1.00 1.10 1.00

Sample Output

YES

这个其实就是给一个图,让你求一下这个图是否存在正权回路,bellman-ford是能求负权回路的,那么正权回路怎么办?只需要把算法的中间条件改一下就可以了:

初始化各边最长路径长度:0

初始化起点最长路径长度:初始钱币数量

再把遍历过程改一下:

if 该边起点的当前已知最短长度+该边长度>该边终点当前已知最长长度

改成

if 该边起点的当前已知最长长度+该边长度>该边终点当前已知最长长度

这个问题就迎刃而解了。

#include<iostream>
using namespace std;

class point{
	int start,end;
	double srate,scost,erate,ecost;
};

int n,m,s;
double v;
point* p;
double* dist;

int main(){
	cin>>n>>m>>s>>v;
	p=new point[m+1];
	dist=new double[n+1];
	for(int i=1;i<=m;i++){
		cin>>point[i].start>>point[i].end>>point[i].rate>>point[i].cost;
	}
	for(int i=1;i<=n;i++){
		dist[i]=0;
	}
	dist[s]=v;
	for(int i=1;i<=n-1;i++){
		bool flag=true;
		for(int j=1;j<=m;j++){
			if(dist[p[j].end]<(dist[p[j].start]-p[j].scost)*p[j].srate){
				dist[p[j].end]=(dist[p[j].start]-p[j].scost)*p[j].srate;
				flag=false;
			}
			if(dist[p[j].start]<(dist[p[j].end]-p[j].ecost)*p[j].erate){
				dist[p[j].start]=(dist[p[j].end]-p[j].ecost)*p[j].erate;
				flag=false;
			}
		}
		if(flag==true){
				cout<<"NO";
				return 0;
			}
	}
	for(int j=1;j<=m;j++){
			if(dist[p[j].end]<(dist[p[j].start]-p[j].scost)*p[j].srate){
				cout<<"NO";
				return 0;
			}
			if(dist[p[j].start]<(dist[p[j].end]-p[j].ecost)*p[j].erate){
				cout<<"NO";
				return 0;
			}
		}
		cout<<"YES";
		return0
}

    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/lwtdzh999/article/details/50790961
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞