Arbitrage

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 29374   Accepted: 12279

题目链接:http://poj.org/problem?id=2240

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

题意:

给出几种货币单位,以及货币与货币之间的兑换汇率,问最后是否能够套利。就是用1个单位的货币,不断去兑换其它的货币,最后得到大于1个单位的相应货币。

题解:

总的思路就是跑最长路看看是否有正环吧,有的话就说明至少存在一种货币可以用来套利。

这里跑最长路的时候要把之前的“ + ”改造为“ * ”,至于正确性,乘法取个对数也等价于加吧?具体证明我也不是很清楚。

代码如下:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <string>
#include <stack>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = ;
map <string ,int> mp;
int n,num;
struct Edge{
int u,v,next;
double w;
}e[N*N<<];
int head[N],vis[N],c[N];
int tot;
void adde(int u,int v,double w){
e[tot].v=v;e[tot].w=w;e[tot].next=head[u];head[u]=tot++;
}
double d[N];
int spfa(int s){
memset(d,,sizeof(d));memset(vis,,sizeof(vis));
d[s]=;queue <int> q;q.push(s);memset(c,,sizeof(c));
c[s]=;vis[s]=;
while(!q.empty()){
int u=q.front();q.pop();vis[u]=;
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(d[v]<d[u]*e[i].w){
d[v]=d[u]*e[i].w;
if(!vis[v]){
vis[v]=;
q.push(v);
if(++c[v]>n) return ;
}
}
}
}
return ;
}
int main(){
int cnt =;
while(scanf("%d",&n)!=EOF){
if(n==) break ;
string s;
num=;cnt++;
for(int i=;i<=n;i++){
cin>>s;
mp[s]=++num;
}
int tmp;
scanf("%d",&tmp);
memset(head,-,sizeof(head));tot=;
for(int i=;i<=tmp;i++){
string s1,s2;
double w;
cin>>s1>>w>>s2;
adde(mp[s1],mp[s2],w);
}
printf("Case %d: ",cnt);
if(spfa()) puts("Yes");
else puts("No");
}
return ;
}

POJ2240:Arbitrage(最长路+正环)的更多相关文章

  1. XYZZY spfa 最长路 判环

    题意: 有n个点  m条边  每个边有权值 一开始有一百血  每次经过一条路都会加上其权值 判断是否能够到达n 显然  有正环的时候肯定能够到达 最短路好题!!!!!!! 显用folyed判断是否联通 ...

  2. HDU1529-Casher Emploryment(最最...最经典的差分约束 差分约束-最长路+将环变线)

    A supermarket in Tehran is open 24 hours a day every day and needs a number of cashiers to fit its n ...

  3. POJ2240 Arbitrage(Floyd判负环)

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

  4. poj 1932 XYZZY(spfa最长路+判断正环+floyd求传递闭包)

    XYZZY Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4154   Accepted: 1185 Description ...

  5. BZOJ 2019 [Usaco2009 Nov]找工作:spfa【最长路】【判正环】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2019 题意: 奶牛们没钱了,正在找工作.农夫约翰知道后,希望奶牛们四处转转,碰碰运气. 而 ...

  6. POJ 2240 Arbitrage spfa 判正环

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

  7. HDU 4514并查集判环+最长路

    点击打开链接 题意:中文题...... 思路:先推断是否能成环,之前以为是有向图,就用了spfa推断,果断过不了自己出的例子,发现是无向图.并查集把,两个点有公共的父节点,那就是成环了,之后便是求最长 ...

  8. POJ-2240 Arbitrage---判断正环+枚举

    题目链接: https://vjudge.net/problem/POJ-2240 题目大意: 已知n种货币,以及m种货币汇率及方式,问能否通过货币转换,使得财富增加. 思路: 由于这里问的是财富有没 ...

  9. POJ 2240 Arbitrage (Bellman Ford判正环)

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

随机推荐

  1. python的基本知识,range在python2.x中和python3.x中的区别

    这些是最开始学习python时的笔记,今天整理一下,在这里记录一下. 各种基础代码解释 for key,item in enumerate(li): print(key,item) inp=input ...

  2. unity开发c#代码

    1.摄像头跟随主角移动,并支持旋转. 开发过程中需要摄像头以一定距离跟随player,同时会进行旋转,属于一种常见的跟随方式. using UnityEngine; using System.Coll ...

  3. git初始化仓库相关

    当我们需要新建一个git项目会遇到的问题 全局设置 git config --global user.name "名字" git config --global user.emai ...

  4. jsp中的input

    Input表示Form表单中的一种输入对象,其又随Type类型的不同而分文本输入框,密码输入框,单选/复选框,提交/重置按钮等,下面一一介绍. 1,type=text 输入类型是text,这是我们见的 ...

  5. Vue 去脚手架插件,自动加载vue文件,style怎么办

    书接上上会,因为当时也没想好怎么办,所以装聋作哑的忽略了Vue文件中的Style,Vue的做法我看着晕乎乎的,细想的话,无非就是自动填写到dom中,所担心的无非是命名冲突. 在一个项目中(像我这种自娱 ...

  6. js 邮箱和手机号码验证

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 4299: Codechef FRBSUM

    4299: Codechef FRBSUM https://www.lydsy.com/JudgeOnline/problem.php?id=4299 分析: 主席树. https://blog.se ...

  8. Java之枚举笔记(Enum)

    package com.simope.ljm; public class MyEnum { public static void main(String[] args) { System.out.pr ...

  9. Mac下用tomcat搭建下载服务器

    1.下载tomcat 去官方网址: http://tomcat.apache.org/ 下载最新版 2.下载解压后,自己可以随便放在哪个文件夹下,自己记得路径即可.比如Users/你的用户名/Docu ...

  10. 一丶人生苦短,我用python【第一篇】

    1 解释器 解释器(英语:Interpreter),又译为直译器,是一种电脑程序,能够把高级编程语言一行一行直接转译运行.解释器不会一次把整个程序转译出来,只像一位"中间人",每次 ...