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. [SAP ABAP开发技术总结]业务对象和BAPI

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

  2. CA*Layer(CATransformLayer--CAGradientLayer)

    CATransformLayer CATransformLayer不同于普通的CALayer,因为它不能显示它自己的内容.只有当存在了一个能作用域子图层的变换它才真正存在.CATransformLay ...

  3. HDU 5724 Chess(国际象棋)

    HDU 5724 Chess(国际象棋) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  4. 不要温柔地走入AMD

    1.无依赖情况 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  5. POJ1011 (DFS+剪枝)

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 129606   Accepted: 30388 Descrip ...

  6. Python学习(1)安装Python

    *****  安装Python 在官网上 https://www.python.org/downloads/ 可以看到有3.5.1与2.7.11两个版本,我这里用的是3.5.1版本 我用的是win7/ ...

  7. hdu 4965 Fast Matrix Calculation

    题目链接:hdu 4965,题目大意:给你一个 n*k 的矩阵 A 和一个 k*n 的矩阵 B,定义矩阵 C= A*B,然后矩阵 M= C^(n*n),矩阵中一切元素皆 mod 6,最后求出 M 中所 ...

  8. html的空格和换行显示【摘自网络】

    一.HTML 代码中的所有连续的空格或空行(换行)都会被显示为一个空格,不管是内容还是标签之间. 二.当我们想让它们在同一行连续显示时,就让所有的代码之间没有空格,也不要换行. 三.当我们想要显示连续 ...

  9. js控制只能输入数字

    onkeyup=clearNoNum(this) function clearNoNum(obj) {    obj.value = obj.value.replace(/[^\d.]/g," ...

  10. 记录一下表格用poi的导出word

    也是网上找的代码http://53873039oycg.iteye.com/blog/2152009,但是横向合并单元格没成功.只能用很蠢的办法建立了好多table public void fillT ...