Arbitrage
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17360   Accepted: 7308

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
    • Source Code
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
char str[40][40],str2[40],str1[40];
double book[40][40];
int main(){
int t;
int count=0;
while(scanf("%d",&t)!=EOF){
if(t==0)
break;
memset(str,0,sizeof(str));
count++; memset(book,0,sizeof(book));
getchar();
for(int i=0;i<t;i++){
scanf("%s",str[i]);
book[i][i]=1.0;
getchar();
}
int n;
scanf("%d",&n);
double d;
for(int i=0;i<n;i++){
memset(str1,0,sizeof(str1));
memset(str2,0,sizeof(str2));
scanf("%s %lf %s",str1,&d,str2);
int temp1,temp2;
for(int ii=0;ii<t;ii++){
if(strcmp(str1,str[ii])==0)
temp1=ii;
}
for(int ii=0;ii<t;ii++){
if(strcmp(str2,str[ii])==0)
temp2=ii;
}
book[temp1][temp2]=d;
getchar();
} for(int k=0;k<t;k++){
for(int i=0;i<t;i++){
for(int j=0;j<t;j++){
if(book[i][j]<book[i][k]*book[k][j])
book[i][j]=book[i][k]*book[k][j];
}
}
} int flag=0;
for(int i=0;i<t;i++){
if(book[i][i]>1.0)
flag=1;
}
if(flag==1)
printf("Case %d: Yes\n",count);
else
printf("Case %d: No\n",count); }
return 0;
}

poj2240最短路 floyd的更多相关文章

  1. ACM/ICPC 之 最短路-Floyd+SPFA(BFS)+DP(ZOJ1232)

    这是一道非常好的题目,融合了很多知识点. ZOJ1232-Adventrue of Super Mario 这一题折磨我挺长时间的,不过最后做出来非常开心啊,哇咔咔咔 题意就不累述了,注释有写,难点在 ...

  2. 模板C++ 03图论算法 2最短路之全源最短路(Floyd)

    3.2最短路之全源最短路(Floyd) 这个算法用于求所有点对的最短距离.比调用n次SPFA的优点在于代码简单,时间复杂度为O(n^3).[无法计算含有负环的图] 依次扫描每一点(k),并以该点作为中 ...

  3. 最短路 - floyd算法

    floyd算法是多源最短路算法 也就是说,floyd可以一次跑出所以点两两之间的最短路 floyd类似动态规划 如下图: 用橙色表示边权,蓝色表示最短路 求最短路的流程是这样的: 先把点1到其他点的最 ...

  4. HDU1869---(最短路+floyd)

    http://acm.hdu.edu.cn/showproblem.php?pid=1869 思路:最短路+floyd 分析:1 题目是要求所有的数据能否满足“六度分离”,那么我们就想到所有点之间的最 ...

  5. 【bzoj2324】[ZJOI2011]营救皮卡丘 最短路-Floyd+有上下界费用流

    原文地址:http://www.cnblogs.com/GXZlegend/p/6832504.html 题目描述 皮卡丘被火箭队用邪恶的计谋抢走了!这三个坏家伙还给小智留下了赤果果的挑衅!为了皮卡丘 ...

  6. 【ACM程序设计】求短路 Floyd算法

    最短路 floyd算法 floyd是一个基于贪心思维和动态规划思维的计算所有点到所有点的最短距离的算法. P57-图-8.Floyd算法_哔哩哔哩_bilibili 对于每个顶点v,和任一顶点对(i, ...

  7. poj 3613 经过k条边最短路 floyd+矩阵快速幂

    http://poj.org/problem?id=3613 s->t上经过k条边的最短路 先把1000范围的点离散化到200中,然后使用最短路可以使用floyd,由于求的是经过k条路的最短路, ...

  8. POJ2240 Arbitrage(Floyd判负环)

    跑完Floyd后,d[u][u]就表示从u点出发可以经过所有n个点回到u点的最短路,因此只要根据数组对角线的信息就能判断是否存在负环. #include<cstdio> #include& ...

  9. 最短路--floyd算法模板

    floyd算法是求所有点之间的最短路的,复杂度O(n3)代码简单是最大特色 #include<stdio.h> #include<string.h> ; const int I ...

随机推荐

  1. iOS---cell-自适应高度

    RootViewController: // // RootViewController.m // UI__cell自适应高度 // // Created by dllo on 16/3/15. // ...

  2. Linux下安装项目管理工具Redmine

    http://www.redmine.org.cn/download Linux下安装项目管理工具Redmine1.Ruby安装Ruby on Rails网站推荐使用1.8.7版. 点击(此处)折叠或 ...

  3. jsonp与跨域

    <script>标签的src属性并不被同源策略所约束,所以可以获取任何服务器上脚本并执行. JSONP是JSON with Padding的略称.它是一个非官方的协议,它允许在服务器端集成 ...

  4. text-align:justify_内容居中对齐

    一直发现text-align : justify这个对齐方式不好使,都不知道为什么么么哒: 因为两端对齐的这个行的结束要一个有空字符串或者别的不可见的字符,用户代理会把这个行的最后几个字符往右边拉,实 ...

  5. 【Gym 100015A】Another Rock-Paper-Scissors Problem

    题 题意 Sonny出石头剪刀布的猜拳策略是 先出R,然后每连续两段都是打败前一段的出拳, 现在问你第n回合打败他要出什么. 分析 他的策略 R P S PSR  SRP PSRSRPRPS SRPR ...

  6. dijkstra,SPFA,Floyd求最短路

    Dijkstra: 裸的算法,O(n^2),使用邻接矩阵: 算法思想: 定义两个集合,一开始集合1只有一个源点,集合2有剩下的点. STEP1:在集合2中找一个到源点距离最近的顶点k:min{d[k] ...

  7. IQ测试

    1.4个人过桥,只能两两过桥,且只有一盏灯,必须有灯才能过桥,4个人过桥时间分别为1,2,5,10分钟,最短多少时间可以过桥? 答案:1和2先走,1再返回,花3分钟.5和10走,2回去,花了3+10+ ...

  8. 看看这些JavaScript题目你会做吗?

    题目1 咋一看这题目,还以为答案选择B呢,其实正确答案为D,知道原因吗?接着往下看 map对数组的每个元素调用定义的回调函数并返回包含结果的数组,咋一看还以为它会像如下这样执行: function t ...

  9. sdk和ndk

    让我先来说说android sdk (Android Software Development Kit, 即Android软件开发工具包)可以说只要你使用java去开发Android这个东西就必须用到 ...

  10. linux mysql 相关操作命令

    1.linux下启动mysql的命令:mysqladmin start/ect/init.d/mysql start (前面为mysql的安装路径) 2.linux下重启mysql的命令:mysqla ...