POJ2240:Arbitrage(最长路+正环)
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(最长路+正环)的更多相关文章
- XYZZY spfa 最长路 判环
题意: 有n个点 m条边 每个边有权值 一开始有一百血 每次经过一条路都会加上其权值 判断是否能够到达n 显然 有正环的时候肯定能够到达 最短路好题!!!!!!! 显用folyed判断是否联通 ...
- 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 ...
- POJ2240 Arbitrage(Floyd判负环)
跑完Floyd后,d[u][u]就表示从u点出发可以经过所有n个点回到u点的最短路,因此只要根据数组对角线的信息就能判断是否存在负环. #include<cstdio> #include& ...
- poj 1932 XYZZY(spfa最长路+判断正环+floyd求传递闭包)
XYZZY Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4154 Accepted: 1185 Description ...
- BZOJ 2019 [Usaco2009 Nov]找工作:spfa【最长路】【判正环】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2019 题意: 奶牛们没钱了,正在找工作.农夫约翰知道后,希望奶牛们四处转转,碰碰运气. 而 ...
- POJ 2240 Arbitrage spfa 判正环
d[i]代表从起点出发可以获得最多的钱数,松弛是d[v]=r*d[u],求最长路,看有没有正环 然后这题输入有毒,千万别用cin 因为是大输入,组数比较多,然后找字符串用strcmp就好,千万不要用m ...
- HDU 4514并查集判环+最长路
点击打开链接 题意:中文题...... 思路:先推断是否能成环,之前以为是有向图,就用了spfa推断,果断过不了自己出的例子,发现是无向图.并查集把,两个点有公共的父节点,那就是成环了,之后便是求最长 ...
- POJ-2240 Arbitrage---判断正环+枚举
题目链接: https://vjudge.net/problem/POJ-2240 题目大意: 已知n种货币,以及m种货币汇率及方式,问能否通过货币转换,使得财富增加. 思路: 由于这里问的是财富有没 ...
- POJ 2240 Arbitrage (Bellman Ford判正环)
Arbitrage Time Limit: 1000MS Memory Limit: 65536K Total Submissions:27167 Accepted: 11440 Descri ...
随机推荐
- 使用mysql5.7版本数据库需要注意的地方/持续更新
数据库mysql 5.7版本的初始密码修改 安装完后实在是找不到初始密码的文件,后面发现再错误日志中 先关闭mysql pkill mysqld 安全模式启动数据库并修改密码 mysqld_safe ...
- python生成器详解
1. 生成器 利用迭代器(迭代器详解python迭代器详解),我们可以在每次迭代获取数据(通过next()方法)时按照特定的规律进行生成.但是我们在实现一个迭代器时,关于当前迭代到的状态需要我们自己记 ...
- Python学习:3.Python学习基础
Python基础概念 一.编码 Python解释器加载.py文件中的代码的时候,对内容进行编码,在Python2.x中默认使用的是ASCII,因此我们使用Python2.x版本输出中文的时候,会出现以 ...
- 静态栈抽象数据类型stack实现
#include<stdio.h> #include<stdbool.h> #include<stdlib.h> #define MAX_STACK_SIZE 10 ...
- java元注解(注解在注解上的注解)
//ElementType.TYPE 给类.接口.枚举上使用 @Target(ElementType.TYPE)//给注解进行注解,表示该注解可以用在什么地方 //@Retention(Retenti ...
- git将本地项目上传到远程仓库
1.cd mygit 打开项目文件夹 2.git init 初始化git 3.git remote add origin xxx(远程仓库地址) 添加远程库 git remote -v 查看远程 ...
- c#获取当前运行程序所在的目录
C#获取项目程序及运行路径的方 1.asp.net webform用“Request.PhysicalApplicationPath获取站点所在虚拟目录的物理路径,最后包含“\”: 2.c# wi ...
- mock.js中新增测试接口无效,返回404
项目是使用的npm+vue+mock模拟数据 我碰到的是在mock配置文件中新增接口,但是接口在测试时无效,返回404.但是在前端代码中把新接口换成配置文件中之前就有的,然后测试就正常了. 所以按问题 ...
- Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. org/apache/hadoop/hbase/
Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. org/apache/hadoop/hbase/ ...
- [转] 前端开发利器--Brackets 的七种武器和旁门左道
转自:http://www.jianshu.com/p/ff7798aa4548 Brackets是Adobe开发的web编辑器,是一款免费开源.多平台支持的软件,并在于GitHub上维护.Brack ...