Arbitrage
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 15640   Accepted: 6563

Description

Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French francs, and 1 French franc buys 0.21 US dollar. Then, by converting currencies, a clever trader can start with 1 US dollar and buy 0.5 * 10.0 * 0.21 = 1.05 US dollars, making a profit of 5 percent.

Your job is to write a program that takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not.

Input

The input will contain one or more test cases. Om the first line of each test case there is an integer n (1<=n<=30), representing the number of different currencies. The next n lines each contain the name of one currency. Within a name no spaces will appear. The next line contains one integer m, representing the length of the table to follow. The last m lines each contain the name ci of a source currency, a real number rij which represents the exchange rate from ci to cj and a name cj of the destination currency. Exchanges which do not appear in the table are impossible. 
Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n.

Output

For each test case, print one line telling whether arbitrage is possible or not in the format "Case case: Yes" respectively "Case case: No".

Sample Input

3
USDollar
BritishPound
FrenchFranc
3
USDollar 0.5 BritishPound
BritishPound 10.0 FrenchFranc
FrenchFranc 0.21 USDollar 3
USDollar
BritishPound
FrenchFranc
6
USDollar 0.5 BritishPound
USDollar 4.9 FrenchFranc
BritishPound 10.0 FrenchFranc
BritishPound 1.99 USDollar
FrenchFranc 0.09 BritishPound
FrenchFranc 0.19 USDollar 0

Sample Output

Case 1: Yes
Case 2: No

Source

 
     感觉自己这种算法还是太弱了......唉! 没有用floy之间用bellman_floy来做的,bellman算法其实总结起来就是三点:
      第一: 初始化
      第二: 循环优化求解最大或者最小
     第三 : 检测是否存在负环

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
#include<map>
#include<iterator>
using namespace std;
const double inf =-;
const int maxn = ;
struct node
{
int u,v;
double val;
};
node edge[];
double dist[maxn];
/*松弛状态判别*/
bool relax(int u,int v,double val){
if(dist[v]<dist[u]*val){
dist[v]=dist[u]*val;
return ;
}
return ;
}
bool Bellman(int st,int n,int m){
for(int i=;i<=n;i++){ //初始化
dist[i]=inf;
}
dist[st]=;
bool flag;
/*循环优化部分*/
for(int i=; i<n;i++) {
flag=false;
for(int j=;j<=m;j++){
if(relax(edge[j].u,edge[j].v,edge[j].val))
flag=true;
}
if(!flag) break;
}
/*检验部分*/
for(int i=;i<=m;i++){
if(relax(edge[i].u,edge[i].v,edge[i].val))
return ; //有负圈
}
return ;
} int main()
{
int n,m;
map<string ,int> sac;
string temp;
int test=;
while(scanf("%d",&n)==&&n!=){
if(!sac.empty())sac.clear();
for(int i=;i<=n;i++){
cin>>temp;
// sac.insert(pair<string ,int>(temp,i));
sac[temp]=i;
}
cin>>m;
double ss;
string aa,bb;
map<string,int>::iterator p1,p2;
for(int i= ; i<=m ; i++ ){
cin>>aa>>ss>>bb;
p1=sac.find(aa);
p2=sac.find(bb);
edge[i].u=p1->second;
edge[i].v=p2->second;
edge[i].val=ss;
} if(Bellman(,n,m)) printf("Case %d: Yes\n",test);
else printf("Case %d: No\n",test);
test++;
// printf("%lf\n",dist[1]);
}
return ;
}

poj-------(2240)Arbitrage(最短路)的更多相关文章

  1. poj 2240 Arbitrage (最短路 bellman_ford)

    题目:http://poj.org/problem?id=2240 题意:给定n个货币名称,给m个货币之间的汇率,求会不会增加 和1860差不多,求有没有正环 刚开始没对,不知道为什么用 double ...

  2. POJ 2240 Arbitrage(最短路 套汇)

    题意  给你n种币种之间的汇率关系  推断是否能形成套汇现象  即某币种多次换为其他币种再换回来结果比原来多 基础的最短路  仅仅是加号换为了乘号 #include<cstdio> #in ...

  3. 最短路(Floyd_Warshall) POJ 2240 Arbitrage

    题目传送门 /* 最短路:Floyd模板题 只要把+改为*就ok了,热闹后判断d[i][i]是否大于1 文件输入的ONLINE_JUDGE少写了个_,WA了N遍:) */ #include <c ...

  4. poj 2240 Arbitrage 题解

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21300   Accepted: 9079 Descri ...

  5. POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)

    POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...

  6. poj 2240 Arbitrage (Floyd)

    链接:poj 2240 题意:首先给出N中货币,然后给出了这N种货币之间的兑换的兑换率. 如 USDollar 0.5 BritishPound 表示 :1 USDollar兑换成0.5 Britis ...

  7. POJ 2240 - Arbitrage - [bellman-ford求最短路]

    Time Limit: 1000MS Memory Limit: 65536K Description Arbitrage is the use of discrepancies in currenc ...

  8. POJ 2240 Arbitrage(floyd)

    http://poj.org/problem?id=2240 题意 : 好吧,又是一个换钱的题:套利是利用货币汇率的差异进行的货币转换,例如用1美元购买0.5英镑,1英镑可以购买10法郎,一法郎可以购 ...

  9. POJ 2240 Arbitrage【Bellman_ford坑】

    链接: http://poj.org/problem?id=2240 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  10. POJ 2240 Arbitrage (求负环)

    Arbitrage 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/I Description Arbitrage is the ...

随机推荐

  1. [转]SIP穿越NAT&FireWall解决方案

    原文链接(也是转载)http://blog.csdn.net/yetyongjin/article/details/6881491.我修改了部分错字.   SIP从私网到公网会遇到什么样的问题呢? 1 ...

  2. Hearthstone-Deck-Tracker项目的编译

    https://github.com/HearthSim/Hearthstone-Deck-Tracker https://github.com/HearthSim/HearthDb https:// ...

  3. 卡尔曼滤波— Constant Velocity Model

    假设你开车进入隧道,GPS信号丢失,现在我们要确定汽车在隧道内的位置.汽车的绝对速度可以通过车轮转速计算得到,汽车朝向可以通过yaw rate sensor(A yaw-rate sensor is ...

  4. [数据结构与算法]栈Stack的多种实现

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  5. 指令随笔之:tail、cat、scp、&、&&、;、|、>、>>

    tail(中文意思是跟踪)   tail默认只看文件的最后10行内容,cat则一次显示全部内容 ping 192.168.120.204 > zyx.log &    #  &表 ...

  6. 函数对象适配器之ptr_fun的使用示例

    //============================================================================ // Name : CopyInts4.c ...

  7. 四十条测试你是不是合格的PHP程序员

    四十条测试你是否合格的PHP程序员,不官方,也不权威,但很给力.超过三条就不合格了.超过五条就得好好反省下自己的不足了. 1. 不会利用如phpDoc这样的工具来恰当地注释你的代码 2. 对优秀的集成 ...

  8. [转载] [Mark]分布式存储必读论文

    原文: http://50vip.com/423.html 分布式存储泛指存储存储和管理数据的系统, 与无状态的应用服务器不同, 如何处理各种故障以保证数据一致,数据不丢, 数据持续可用, 是分布式存 ...

  9. [mysql]支持emoji(字符集问题)!

    问题的根源 主要问题就是在字符集,一般解决这种问题都是靠试验.我实验了一通,得出的结论和大家分享一下(如有错误,还望指正): 数据库的字符集 数据库连接的字符集 配置方法 设置数据库的字符集为utf8 ...

  10. Android照相机模块编程 照片颠倒问题及查询摄像头参数问题的解决

    这两天编程弄Android照相机模块,设置好各种参数后,发现预览的时候,照片是颠倒了,不是上下颠倒而是颠倒90°. 我的手机是华为U9200,用的Android4.0.3,后来看到http://www ...