设为首页 收藏本站
查看: 854|回复: 0

[经验分享] POJ 1860 Currency Exchange (Bellman-Ford 找正环)

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-11-24 14:50:47 | 显示全部楼层 |阅读模式
Currency Exchange

Time Limit: 1000MS



Memory Limit: 30000K
Total Submissions: 19715



Accepted: 7040

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
Source

Northeastern Europe 2001, Northern Subregion





题目链接:  http://poj.org/problem?id=1860



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



题解:  第一次学bellman-ford算法做的题,借鉴了大部分人的思路, bellman-ford一般用来判负环,这里是用来找正环,一种货币相当于图上一个点, 一个站点相当于一个环(这里要求a对b和b对a都可以互换),因此我们如果可以找到一个正权回路并且正环可以无限松弛(松弛操作)说明必然会出现套汇





#include <cstdio>
#include <cstring>
int const MAXN = 101; //点的最大值
int num;   //图上边的数量
int n, m, s; // n表示货币种类数目(图上点数), m表示站点数目,s表示所持货币的种类
double v;  //v表示所持货币面额
struct Point  //站点结构,记录两种货币间的汇率和手续费
{
int a, b;
double rab, cab;
}point[10010];
double maxgain[101];   //记录到原点的总权值
bool Bellman()
{
bool flag;   //标记能否找到正权回路
memset(maxgain,0,sizeof(maxgain));  //初始化maxgain的值为0
maxgain = v;         //起点初始化为自身值
for(int i = 0; i < n; i++)  //枚举图上每个点
{
flag = false;   
for(int j = 0; j < num; j++)  //枚举每条边,判断加入该边之后总值是否会变大
if(maxgain[point[j].b] < point[j].rab * (maxgain[point[j].a] - point[j].cab))
{
maxgain[point[j].b] = point[j].rab * (maxgain[point[j].a] - point[j].cab);
flag = true;
}
if(!flag) //若没找到正环,则说明无解,退出
break;
}
//判断正环是否可达,若可达则说明有解(专业说是判断正环是否可以无限松弛)
for(int i = 0; i < num; i++)
if(maxgain[point.b] < point.rab * (maxgain[point.a] - point.cab))
return true;
return false;
}
int main()
{
int c1, c2;
double r12, c12, r21, c21;
while(scanf(&quot;%d %d %d %lf&quot;, &n, &m, &s, &v) != EOF)
{
num = 0;
for(int i = 0; i < m; i++)
{
scanf(&quot;%d %d %lf %lf %lf %lf&quot;, &c1, &c2, &r12, &c12, &r21, &c21);
point[num].a = c1;
point[num].b = c2;
point[num].rab = r12;
point[num++].cab = c12;  
point[num].a = c2;      //c2 -> c1的转换信息
point[num].b = c1;
point[num].rab = r21;
point[num++].cab = c21;
}
if(Bellman())
printf(&quot;YES\n&quot;);
else
printf(&quot;NO\n&quot;);
}
return 0;
}






运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-143165-1-1.html 上篇帖子: oracle exchange特性做sql优化测试 下篇帖子: Exchange 2010 跟我走 之二 --接手项目
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表