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. windows tomcat 优化

    windows tomcat 优化 1.  tomcat conf server.xml 在server.xml中修改以一部分,增加节点数目,可以很好的提高性能: <Connector port ...

  2. ASP.NET四则运算--策略模式

    在ASP.NET中实现四则运算,同样使用了类的封装,以及策略模式.只不过是把封装的类.前台代码以及后台的代码分离开来,但同样是要达到功能的实现. Calculator.cs using System; ...

  3. Servlet&jsp基础:第四部分

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

  4. OnClientClick的用法

    摘自:http://blog.csdn.net/coolpig86/article/details/5439560 OnClientClick用于执行客户端脚本.当我们单击一个按钮时,最先执行的是On ...

  5. LTE Module User Documentation(翻译3)——仿真输出

    LTE用户文档 (如有不当的地方,欢迎指正!) 6 仿真输出 ns-3 LTE 模型当前支持输出 PHY, MAC, RLC 和 PDCP 级别的 Key Performance Indicators ...

  6. [转]-如何将Eclipse中的项目迁移到Android Studio 中

    英文地址:http://developer.android.com/sdk/installing/migrate.html 翻译:Android Studio 中文组(大锤译) 如果你之前有用Ecli ...

  7. iOS - OC NSTimeZone 时区

    前言 @interface NSTimeZone : NSObject <NSCopying, NSSecureCoding> NSTimeZone 表示时区信息. 1.NSTimeZon ...

  8. iOS - UISlider

    前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UISlider : UIControl <NSCoding> @a ...

  9. iOS开发之引用百度地图SDK(一)-----------SDK开发指南

    (void)viewWillAppear:(BOOL)animated { [_mapView viewWillAppear]; _mapView.delegate = self; // 此处记得不用 ...

  10. golang学习之beego框架配合easyui实现增删改查及图片上传

    golang学习之beego框架配合easyui实现增删改查及图片上传 demo目录: upload文件夹主要放置上传的头像文件,main是主文件,所有效果如下: 主页面: 具体代码: <!DO ...