# 最短路径算法—Bellman-Ford（poj1860 currency exchange）

###### Bellman-Ford算法详讲

Dijkstra算法是处理单源最短路径的有效算法，但它局限于边的权值非负的情况，若图中出现权值为负的边，Dijkstra算法就会失效，求出的最短路径就可能是错的。

Bellman-Ford算法的流程如下：

BellmanFord算法可以大致分为三个部分

dv） > d (u) + w(u,v)

#include<iostream>
#include<cstdio>
using namespace std;

#define MAX 0x3f3f3f3f
#define N 1010
int nodenum, edgenum, original; //点，边，起点

typedef struct Edge //边
{
int u, v;
int cost;
}Edge;

Edge edge[N];
int dis[N], pre[N];

bool Bellman_Ford()
{
for(int i = 1; i <= nodenum; ++i) //初始化
dis[i] = (i == original ? 0 : MAX);
for(int i = 1; i <= nodenum - 1; ++i)
for(int j = 1; 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;
pre[edge[j].v] = edge[j].u;
}
bool flag = 1; //判断是否含有负权回路
for(int i = 1; i <= edgenum; ++i)
if(dis[edge[i].v] > dis[edge[i].u] + edge[i].cost)
{
flag = 0;
break;
}
return flag;
}

void print_path(int root) //打印最短路的路径（反向）
{
while(root != pre[root]) //前驱
{
printf("%d-->", root);
root = pre[root];
}
if(root == pre[root])
printf("%d\n", root);
}

int main()
{
scanf("%d%d%d", &nodenum, &edgenum, &original);
pre[original] = original;
for(int i = 1; i <= edgenum; ++i)
{
scanf("%d%d%d", &edge[i].u, &edge[i].v, &edge[i].cost);
}
if(Bellman_Ford())
for(int i = 1; i <= nodenum; ++i) //每个点最短路
{
printf("%d\n", dis[i]);
printf("Path:");
print_path(i);
}
else
printf("have negative circle\n");
return 0;
}


# 最短路径算法—Bellman-Ford(贝尔曼-福特)算法分析与实现(C/C++)

1.Dijkstra算法：

http://www.wutianqi.com/?p=1890

2.Floyd算法：

http://www.wutianqi.com/?p=1903

Dijkstra算法是处理单源最短路径的有效算法，但它局限于边的权值非负的情况，若图中出现权值为负的边，Dijkstra算法就会失效，求出的最短路径就可能是错的。这时候，就需要使用其他的算法来求解最短路径，Bellman-Ford算法就是其中最常用的一个。该算法由美国数学家理查德•贝尔曼（Richard Bellman, 动态规划的提出者）和小莱斯特•福特（Lester Ford）发明。Bellman-Ford算法的流程如下：

• 数组Distant[i]记录从源点s到顶点i的路径长度，初始化数组Distant[n]为, Distant[s]为0；
•
以下操作循环执行至多n-1次，n为顶点数：
对于每一条边e(u, v)，如果Distant[u] + w(u, v) < Distant[v]，则另Distant[v] = Distant[u]+w(u, v)。w(u, v)为边e(u,v)的权值；
若上述操作没有对Distant进行更新，说明最短路径已经查找完毕，或者部分点不可达，跳出循环。否则执行下次循环；
• 为了检测图中是否存在负环路，即权值之和小于0的环路。对于每一条边e(u, v)，如果存在Distant[u] + w(u, v) < Distant[v]的边，则图中存在负环路，即是说改图无法求出单源最短路径。否则数组Distant[n]中记录的就是源点s到各顶点的最短路径长度。

Bellman－Ford算法可以大致分为三个部分

d（v） > d (u) + w(u,v)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 /* * About: Bellman-Ford算法 * Author: Tanky Woo * Blog: www.WuTianqi.com */   #include <iostream> using namespace std; const int maxnum = 100; const int maxint = 99999;   // 边， typedef struct Edge{ int u, v; // 起点，重点 int weight; // 边的权值 }Edge;   Edge edge[maxnum]; // 保存边的值 int dist[maxnum]; // 结点到源点最小距离   int nodenum, edgenum, source; // 结点数，边数，源点   // 初始化图 void init() { // 输入结点数，边数，源点 cin >> nodenum >> edgenum >> source; for(int i=1; i<=nodenum; ++i) dist[i] = maxint; dist[source] = 0; for(int i=1; i<=edgenum; ++i) { cin >> edge[i].u >> edge[i].v >> edge[i].weight; if(edge[i].u == source) //注意这里设置初始情况 dist[edge[i].v] = edge[i].weight; } }   // 松弛计算 void relax(int u, int v, int weight) { if(dist[v] > dist[u] + weight) dist[v] = dist[u] + weight; }   bool Bellman_Ford() { for(int i=1; i<=nodenum-1; ++i) for(int j=1; j<=edgenum; ++j) relax(edge[j].u, edge[j].v, edge[j].weight); bool flag = 1; // 判断是否有负环路 for(int i=1; i<=edgenum; ++i) if(dist[edge[i].v] > dist[edge[i].u] + edge[i].weight) { flag = 0; break; } return flag; } int main() { //freopen("input3.txt", "r", stdin); init(); if(Bellman_Ford()) for(int i = 1 ;i <= nodenum; i++) cout << dist[i] << endl; return 0; }

Currency Exchange
 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 34920 Accepted: 13397

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 RAB, CAB, RBA and CBA - 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<=103
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10-2<=rate<=102, 0<=commission<=102
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 104

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

#include<iostream>
#include<cstring>
using namespace std;
int n,m,s;
int num;
double v,dis[205];
struct edge
{
int v1;
int v2;
double rat;
double com;
}edges[410];
bool bellmanford()
{
for(int i=1;i<n;i++)
{
bool flag=false;
for(int j=0;j<num;j++)
{
int v1=edges[j].v1;
int v2=edges[j].v2;
if((dis[v1]-edges[j].com)*edges[j].rat>dis[v2])
{
dis[v2]=(dis[v1]-edges[j].com)*edges[j].rat;
flag=true;
}
}
if(flag==false) break;
}
for(int j=0;j<num;j++)
{
int v1=edges[j].v1;
int v2=edges[j].v2;
if((dis[v1]-edges[j].com)*edges[j].rat>dis[v2]) return true;
}
return false;
}
int main()
{
while(cin>>n>>m>>s>>v)
{
num=0;
for(int i=0;i<m;i++)
{
cin>>edges[num].v1>>edges[num].v2>>edges[num].rat>>edges[num].com;
edges[num+1].v1=edges[num].v2; edges[num+1].v2=edges[num].v1;
num++;
cin>>edges[num].rat>>edges[num].com;
num++;
}
for(int i=1;i<=n;i++) dis[i]=0;
dis[s]=v;
if(bellmanford()) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120