POJ 1860 Bellman-Ford算法

转载链接:http://blog.csdn.net/lyy289065406/article/details/6645778

提示:关键在于反向利用Bellman-Ford算法

题目大意

有多种汇币,汇币之间可以交换,这需要手续费,当你用100A币交换B币时,A到B的汇率是29.75,手续费是0.39,那么你可以得到(100 - 0.39) * 29.75 = 2963.3975 B币。问s币的金额经过交换最终得到的s币金额数能否增加

货币的交换是可以重复多次的,所以我们需要找出是否存在正权回路,且最后得到的s金额是增加的

怎么找正权回路呢?(正权回路:在这一回路上,顶点的权值能不断增加即能一直进行松弛)

题目分析:

一种货币就是图上的一个点

一个“兑换点”就是图上两种货币之间的一个兑换环,相当于“兑换方式”M的个数,是双边

唯一值得注意的是权值,当拥有货币A的数量为V时,A到A的权值为K,即没有兑换

而A到B的权值为(V-Cab)*Rab

本题是“求最大路径”,之所以被归类为“求最小路径”是因为本题题恰恰与bellman-Ford算法的松弛条件相反,求的是能无限松弛的最大正权路径,但是依然能够利用bellman-Ford的思想去解题。

因此初始化d(S)=V   而源点到其他店的距离(权值)初始化为无穷小(0),当s到其他某点的距离能不断变大时,说明存在最大路径

附个人代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;

int n, m, fn; // 分别表示货币种数 、 转换点 、 源点 、 源点权值
double fv;
double dis[210];

struct Node
{
    int u, v;
    double r, c; // c 兑换费用 r 兑换率
}edge[210];

int tot = 0;  // 边数  n 是点数

bool relax(int j)
{
    double t = (dis[edge[j].u] - edge[j].c) * edge[j].r;
    if (dis[edge[j].v] < t)  // 与bellman 算法刚好相反。bellman算法用来找找负环 求最短路径
    {                        //这里用同样的思想找正环 求最长路径
        dis[edge[j].v] = t;
        return true;
    }
    return false;
}

bool bellman(int ori)
{
    memset(dis, 0, sizeof(dis));
    dis[ori] = fv;
    bool flag;
    for (int i=0; i<n; ++i)  // 尝试n-1次(对每个点一次) 对每条边进行松弛。寻找最长边
    {                        // 若不存在负环则可以确定源点到 每个顶点的 最长距离
        flag = false;
        for (int j=0; j<tot; ++j)
        {
            if (relax(j)) flag = true;
            //if (flag == false) return false;
        }
        if (dis[ori] > fv) return true;
        if (flag == false) return false;
    }
    for (int i=0; i<tot; ++i)
    {
        if (relax(i)) return true;
    }
    return false;
}

int main()
{
    while (~scanf("%d%d%d%lf", &n, &m, &fn, &fv))
    {
        tot  = 0;
        for (int i=0; i<m; ++i)
        {
            int a, b;
            double rab, cab, rba, cba;
            scanf("%d%d%f%f%f%f", &a, &b, &rab, &cab, &rba, &cba);
            edge[tot].u = a;
            edge[tot].v = b;
            edge[tot].r = rab;
            edge[tot++].c = cab;
            edge[tot].u = b;
            edge[tot].v = a;
            edge[tot].r = rba;
            edge[tot++].c = cba;
        }
        if (bellman(fn))
            printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

时间: 2024-08-08 03:59:42

POJ 1860 Bellman-Ford算法的相关文章

Bellman - Ford 算法解决最短路径问题

Bellman - Ford 算法: 一:基本算法 对于单源最短路径问题,上一篇文章中介绍了 Dijkstra 算法,但是由于 Dijkstra 算法局限于解决非负权的最短路径问题,对于带负权的图就力不从心了,而Bellman - Ford算法可以解决这种问题. Bellman - Ford 算法可以处理路径权值为负数时的单源最短路径问题.设想可以从图中找到一个环路且这个环路中所有路径的权值之和为负.那么通过这个环路,环路中任意两点的最短路径就可以无穷小下去.如果不处理这个负环路,程序就会永远运

Bellman—Ford算法思想

---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G运行Bellman—Ford算法的结果是一个布尔值,表明图中是否存在着一个从源点s可达的负权回路.若存在负权回路,单源点最短路径问题无解:若不存在这样的回路,算法将给出从源点s到图G的任意顶点v的最短路径值d[v] Bellman—Ford算法流程 分为三个阶段: (1)初始化:将除源点外的所有顶点

poj 3259 Wormholes (BELLman—FOrd算法)(邻接矩阵表示)

http://poj.org/problem?id=3259 之前一开始 ,没做出来,搁置了好几天才看见bug所在.所以今天a掉了 ,把代码贴出来,这个使用邻接矩阵表示的 ,下一篇用邻接表可以提高效率. #include<iostream> #include<queue> #include<stdio.h> #include<string.h> using namespace std; const int INF=600; int G[INF][INF];

最短路径——Bellman Ford算法(C++)

源代码: #include<cstdio>#include<cstring>int m(1),n,k,i[1001],x[1001],y[1001],f[1001];int main(){ scanf("%d%d",&n,&k); for (int a=1;a<=n;a++) for (int b=1;b<=n;b++) { scanf("%d",&i[m]); if (i[m]!=-1) { x[m]=a

POJ 1860 Currency Exchange (Bellman ford)

Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22405   Accepted: 8095 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and pe

POJ 1860 Currency Exchange(BellmanFord算法的变形,求正环)

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 o

POJ 3259 Wormholes SPFA算法题解

本题其实也可以使用SPFA算法来求解的,不过就一个关键点,就是当某个顶点入列的次数超过所有顶点的总数的时候,就可以判断是有负环出现了. SPFA原来也是可以处理负环的. 不过SPFA这种处理负环的方法自然比一般的Bellman Ford算法要慢点了. #include <stdio.h> #include <string.h> #include <limits.h> const int MAX_N = 501; const int MAX_M = 2501; const

POJ 1860 Currency Exchange (最短路)

Currency Exchange Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) Total Submission(s) : 4   Accepted Submission(s) : 2 Problem Description Several currency exchange points are working in our city. Let us suppose that

POJ 1860 Currency Exchange BellmanFord题解

会建图,然后使用标准的Bellman Ford算法,判断负环就解决了. 不过本题实际应用不是计算负环,而是计算最大值,也就是求出源点到所有点的最大收益值之后,然后判断是否可以进一步增加收益,如果可以那么证明有环可以不断反复走这个环,不断增加收益,实际就是判负环的应用了. #include <stdio.h> #include <vector> #include <string.h> #include <algorithm> #include <iost

Dijkstra算法(求解单源最短路)详解 + 变形 之 poj 1860 Currency Exchange

/* 求解单源最短路问题:Dijkstra算法(该图所有边的权值非负) 关键(贪心): (1)找到最短距离已经确定的节点,从它出发更新与其相邻节点的最短距离: (2)此后不再关心(1)中“最短距离已经确定的节点”. 时间复杂度(大概的分析,不准确): “找到最短距离已经确定的节点” => O(|V|) "从它出发更新与其相邻节点的最短距离" => 邻接矩阵:O(|V|),邻接表:O(|E|) 需要循环以上两个步骤V次,所以时间复杂度:O(V^2) 即:在|E|较小的情况下,