Arbitrage
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions:27167   Accepted: 11440

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 思路:
用bellman判是否存在正环,可以将距离全部初始化为1.
不到万不得已,绝对不用SPAF.
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
char s[][];
int u[],v[];
double w[];
int n,m;
double dis[]; bool Bellman()
{ for(int i=;i<=n;i++){
dis[i]=;
}
int flag=;
for(int i=;i<=n;i++){
flag=;
for(int j=;j<=m;j++){
if(dis[v[j]]<dis[u[j]]*w[j]){
dis[v[j]]=dis[u[j]]*w[j];
flag=;
}
}
}
return flag;
} int main()
{
int T=;
while(scanf("%d",&n)&&n){
T++;
for(int i=;i<=n;i++){
scanf("%s",s[i]);
} scanf("%d",&m);
char a[],b[],va,vb;
double uu;
for(int i=;i<=m;i++){
scanf("%s%lf%s",a,&uu,b);
for(int j=;j<=n;j++){
if(!strcmp(a,s[j])){va=j;break;}
}
for(int j=;j<=n;j++){
if(!strcmp(b,s[j])){vb=j;break;}
}
u[i]=va;v[i]=vb;w[i]=uu;
} if(Bellman()){
printf("Case %d: Yes\n",T);
}
else printf("Case %d: No\n",T);
}
}
												

POJ 2240 Arbitrage (Bellman Ford判正环)的更多相关文章

  1. poj1860 兑换货币(bellman ford判断正环)

    传送门:点击打开链接 题目大意:一个城市有n种货币,m个货币交换点,你有v的钱,每个交换点只能交换两种货币,(A换B或者B换A),每一次交换都有独特的汇率和手续费,问你存不存在一种换法使原来的钱更多. ...

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

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

  3. poj 2240 Arbitrage 题解

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

  4. poj 2049(二分+spfa判负环)

    poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...

  5. 最短路(Floyd_Warshall) POJ 2240 Arbitrage

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

  6. POJ 2240 Arbitrage spfa 判正环

    d[i]代表从起点出发可以获得最多的钱数,松弛是d[v]=r*d[u],求最长路,看有没有正环 然后这题输入有毒,千万别用cin 因为是大输入,组数比较多,然后找字符串用strcmp就好,千万不要用m ...

  7. POJ 2240 Arbitrage Bellman_ford 判读是否存在正环

    和POJ1860差不多,就是用bellmanford判读是否存在正环,注意的是同种货币之间也可以交换,就是说:A货币换A货币汇率是2的情况也是存在的. #include<stdio.h> ...

  8. POJ 1860——Currency Exchange——————【最短路、SPFA判正环】

    Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u S ...

  9. POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】

    题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total ...

随机推荐

  1. vue計算屬性

    計算屬性:computed 和method的差別:computed是基於它的依賴緩存,只有它的相關依賴發生改變時才會重新獲取值. method是在重新渲染時,函數總會重新調用. comuputed:默 ...

  2. 今天开始学习模式识别与机器学习Pattern Recognition and Machine Learning (PRML),章节5.1,Neural Networks神经网络-前向网络。

    话说上一次写这个笔记是13年的事情了···那时候忙着实习,找工作,毕业什么的就没写下去了,现在工作了有半年时间也算稳定了,我会继续把这个笔记写完.其实很多章节都看了,不过还没写出来,先从第5章开始吧, ...

  3. Vmware 控制脚本

    #_*_ coding:utf8 _*_ import sys,time import yaml import re import os import ssl import random import ...

  4. BZOJ3676[Apio2014]回文串——回文自动机

    题目描述 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你求出s的所有回文子串中的最 大出现值. 输入 输入只有一行,为一个只包含小写字 ...

  5. BZOJ3165[Heoi2013]Segment——李超线段树

    题目描述 要求在平面直角坐标系下维护两个操作: 1.在平面上加入一条线段.记第i条被插入的线段的标号为i. 2.给定一个数k,询问与直线 x = k相交的线段中,交点最靠上的线段的编号. 输入 第一行 ...

  6. Mysql 计划任务

    -- 设置 show variables like '%sche%'; ; -- Start存储过程 drop PROCEDURE if exists test; CREATE PROCEDURE t ...

  7. subprocess 模块

    import subprocess # 就用来执行系统命令 import os cmd = r'dir D:\上海python全栈4期\day23 | findstr "py"' ...

  8. Codeforces519 E. A and B and Lecture Rooms

    传送门:>Here< 题意:询问给出一棵无根树上任意两点$a,b$,求关于所有点$i$,$dist(a,i) = dist(b,i)$的点的数量.要求每一次询问在$O(log n)$的时间 ...

  9. 如何保证 spring-boot 和 spring-cloud版本一致

    spring-boot 版本 和 spring-cloud版本是一一对应的,很多错误都是由于版本不一致导致的.很多百度的东西太老了, 版本一升级就会出错. spring的jar包依赖关系是最难的,但聪 ...

  10. python学习日记(文件操作练习题)

    登录注册(三次机会) name = input('请注册姓名:') password = input('请注册密码:') with open('log',mode='w',encoding='utf- ...