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. CodeForces 432B Football Kit

     Football Kit Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Subm ...

  2. JAVA排序--[选择排序]

    package com.array; public class Sort_Select { /** * 项目名称:选择排序 ; * 项目要求:用JAVA对数组进行排序,并运用选择排序算法; * 作者: ...

  3. FLASH CC 2015 CANVAS 中 gotoAndStop、gotoAndPlay() 不起作用

    哎 话不多说先看我的代码: //舞台上 放着sp0.sp1....sp8,9个mc,每个mc都有几帧, //帧上有如下代码 var S=this; S.stop() inIt1();//not wor ...

  4. iOS - OC Struct 结构体

    1.结构体的定义与调用 // 定义结构体类型 // 结构体类型名为 MyDate1 struct MyDate1 { int year; int month; int day; }; // 定义结构体 ...

  5. iOS - Swift Range 范围

    前言 Range:结构体,这个结构体用来表示一个区间的范围. public struct Range<Element : ForwardIndexType> : Equatable, Co ...

  6. c++ string的实现。

    第三次做了.只是做个复习.偶然发现之前的版本有内存泄露.基本功还是不过关.这次应该没有内存泄漏了.虽然是个简单版本. 1)了解堆,栈,值copy. 2)几个常用的c的字符函数和c中的char 如何表示 ...

  7. Android_相关路径

    1. Android应用安装涉及到如下几个目录:system/app   系统自带的应用程序,无法删除.data/app      用户程序安装的目录,有删除权限.安装时把apk文件复制到此目录.da ...

  8. [转载] 新兵训练营系列课程——平台服务部署及Web框架

    原文: http://weibo.com/p/1001643875679132642345 大纲 微博平台主要负责微博基础功能.接下来将会介绍 平台的作用,以及服务提供的形式 平台Web服务的部署 平 ...

  9. thinkphp ajax 无刷新分页效果的实现

    思路:先做出传统分页效果,然后重新复制一份Page.class.php类,对它进行修改,把js中的函数传到page类中,把上一页.下一页.首页.尾页.链接页中的url地址改成js控制的函数,模板页面中 ...

  10. VNC的安装与配置

    一,安装tigervnc-server VNC软件包 [root@localhost ~]# yum install tigervnc-server 设置开机自启动 [root@localhost ~ ...