POJ1860

题目大意:你在某一点有一些钱,给定你两点之间钱得兑换规则,问你有没有办法使你手里的钱增多。就是想看看转一圈我的钱能不能增多,出现这一点得条件就是有兑换钱得正权回路,所以选择用bellman_ford得算法

准备工作,bellman_ford得更新是根据已知边得数据来的,所以需要存住边的数据,还要有dis数组作为更新判断,dis也就相当于中心节点得钱兑换到i节点(中间可能会经过别的)变成了多少钱

#include <iostream>
#include <string.h>
#include <cstdio>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 110;
struct node{
int from,to;
double r,c;
}edge[maxn * 2];
double d[maxn];

根据题意,存储边的数据

for(int i = 1;i <= m * 2;i++)
{
scanf("%d %d %lf %lf %lf %lf",&a,&b,&r1,&c1,&r2,&c2);
edge[i].from = a;edge[i].to = b;
edge[i].r = r1;edge[i].c = c1;
i++;
edge[i].from = b;edge[i].to = a;
edge[i].r = r2;edge[i].c = c2;
}

进行bellman_ford算法,进行钱财得初始化,初始化d时要根据其含义和松弛得判断条件来进行初始化,然后松弛n次,再进行判断,如果还能松弛,那就代表有正权回路返回True~~

bool Bellman_ford(int n,int s,double v,int len)
{
for(int i = 1;i <= n;i++)d[i] = 0.0; d[s] = v; for(int i = 1;i <= n;i++)
{
bool flag = true;
for(int j = 1;j <= len ;j++)
{
int from = edge[j].from;
int to = edge[j].to;
double r = edge[j].r;
double c = edge[j].c;
if(d[to] < (d[from] - c) * r)
{
d[to] = (d[from] - c) * r;
flag = false;
}
}
if(flag)return false;
}
for(int i = 1;i <= len;i++)
{
if(d[edge[i].to] < (d[edge[i].from] - edge[i].c) * edge[i].r)
{
return true;
}
}
return false; }

Bellman_ford货币兑换——正权回路判断的更多相关文章

  1. 图论 --- spfa + 链式向前星 : 判断是否存在正权回路 poj 1860 : Currency Exchange

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 19881   Accepted: 711 ...

  2. Currency Exchange 货币兑换 Bellman-Ford SPFA 判正权回路

    Description Several currency exchange points are working in our city. Let us suppose that each point ...

  3. [ACM] hdu 1217 Arbitrage (bellman_ford最短路,推断是否有正权回路或Floyed)

    Arbitrage Problem Description Arbitrage is the use of discrepancies in currency exchange rates to tr ...

  4. HDU - 1317 ~ SPFA正权回路的判断

    题意:有最多一百个房间,房间之间连通,到达另一个房间会消耗能量值或者增加能量值,求是否能从一号房间到达n号房间. 看数据,有定5个房间,下面有5行,第 iii 行代表 iii 号 房间的信息,第一个数 ...

  5. POJ 1860 Currency Exchange(最短路&spfa正权回路)题解

    题意:n种钱,m种汇率转换,若ab汇率p,手续费q,则b=(a-q)*p,你有第s种钱v数量,问你能不能通过转化让你的s种钱变多? 思路:因为过程中可能有负权值,用spfa.求是否有正权回路,dis[ ...

  6. poj 1860 Currency Exchange (SPFA、正权回路 bellman-ford)

    链接:poj 1860 题意:给定n中货币.以及它们之间的税率.A货币转化为B货币的公式为 B=(V-Cab)*Rab,当中V为A的货币量, 求货币S通过若干此转换,再转换为原本的货币时是否会添加 分 ...

  7. POJ1860-Currency Exchange (正权回路)【Bellman-Ford】

    <题目链接> <转载于 >>> > 题目大意: 有多种汇币,汇币之间可以交换,这需要手续费,当你用100A币交换B币时,A到B的汇率是29.75,手续费是0. ...

  8. POJ 3259 Wormholes(最短路&spfa正权回路)题解

    题意:给你m条路花费时间(双向正权路径),w个虫洞返回时间(单向负权路径),问你他能不能走一圈回到原点之后,时间倒流. 思路:题意有点难看懂,我们建完边之后找一下是否存在负权回路,存在则能,反之不能. ...

  9. POJ1860Currency Exchange(Bellman + 正权回路)

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 23938   Accepted: 867 ...

随机推荐

  1. Spring,Hibernate 集成解决多hbm.xml文件繁多的方案

    开发一个大一点的项目有很多的hbm.xml文件,有时候上百个也不稀奇,如果用 <property name="mappingLocations"> <list&g ...

  2. 基元线程同步构造 AutoResetEvent和ManualResetEvent 线程同步

    在.Net多线程编程中,AutoResetEvent和ManualResetEvent这两个类经常用到, 他们的用法很类似,但也有区别.ManualResetEvent和AutoResetEvent都 ...

  3. 'Microsoft.VisualStudio.Editor.Implementation.EditorPackage' package did not load correctly

    Visual Studio 2012 Ultimate Removing "C:\Users\UserName\AppData\Local\Microsoft\VisualStudio\11 ...

  4. FP-growth算法高效发现频繁项集(Python代码)

    FP-growth算法高效发现频繁项集(Python代码) http://blog.csdn.net/leo_xu06/article/details/51332428

  5. New Document (2)

    #Markdown 语法说明 (简体中文版) / (点击查看快速入门) ##概述 ###宗旨 兼容 HTML 特殊字符自动转换 区块元素 段落和换行 标题 区块引用 列表 代码区块 分隔线 区段元素 ...

  6. vprintf 和 vsnpintf 的用法

    函数定义: int vprintf ( const char * format, va_list arg ); printf() and friends are for normal use. vpr ...

  7. keras—多层感知器MLP—IMDb情感分析

    import urllib.request import os import tarfile from keras.datasets import imdb from keras.preprocess ...

  8. ios 打tag

    修改spec文件的version: git commit -am"version 0.1.1" git push origin master -u git tag 0.1.1 gi ...

  9. 【校招面试 之 C/C++】第10题 C++不在构造函数和析构函数中调用虚函数

    1.不要在构造函数中调用虚函数的原因 在概念上,构造函数的工作是为对象进行初始化.在构造函数完成之前,被构造的对象被认为“未完全生成”.当创建某个派生类的对象时,如果在它的基类的构造函数中调用虚函数, ...

  10. Tortoise SVN 使用笔记

    最近一个项目,团队使用的是SVN来控制版本,所以顺便学习下SVN的知识.在Windows上,肯定是用图形化客户端Tortoise SVN在加速我们的工作. 首先,在一个准备用在工作目录的文件夹下,点击 ...