Arbitrage

题目链接:

http://acm.hust.edu.cn/vjudge/contest/122685#problem/I

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

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

Hint




##题意:

求货币经过一系列兑换操作后能否升值.


##题解:

转化为图模型后就是求满足条件的环是否存在.
这里把求最短路时的加法改成乘法即可,结果就是是否存在环使得路径大于1.
以下分别用三种方法求:
bellman-ford和floyd用时都较多,800+ms.
spfa只需要90+ms, 不过需要用c++交,否则TLE.(真是神奇)


##代码:
####spaf法:94ms (必须用c++交,否则TLE)
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 1100
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

int m,n,k;

int edges, u[maxn], v[maxn];

double w[maxn];

int first[maxn], _next[maxn];

double dis[maxn];

void add_edge(int s, int t, double val) {

u[edges] = s; v[edges] = t; w[edges] = val;

_next[edges] = first[s];

first[s] = edges++;

}

queue q;

bool inq[maxn];

int inq_cnt[maxn];

bool spfa(int s) {

memset(inq, 0, sizeof(inq));

memset(inq_cnt, 0, sizeof(inq_cnt));

for(int i=1; i<=n; i++) dis[i] = 0; dis[s] = 1;

while(!q.empty()) q.pop();

q.push(s); inq_cnt[s]++;

while(!q.empty()) {
int p = q.front(); q.pop();
inq[p] = 0;
for(int e=first[p]; e!=-1; e=_next[e]) {
double tmp = dis[u[e]] * w[e];
if(dis[v[e]] < tmp) {
dis[v[e]] = tmp;
if(!inq[v[e]]) {
q.push(v[e]);
inq[v[e]] = 1;
inq_cnt[v[e]]++;
if(inq_cnt[v[e]] >= n) return 0;
}
}
}
} return 1;

}

map<string,int> name;

int main(int argc, char const *argv[])

{

//IN;

int ca = 1;
while(scanf("%d", &n) != EOF && n)
{
memset(first, -1, sizeof(first));
edges = 0;
name.clear(); for(int i=1; i<=n; i++) {
string s; cin >> s;
name.insert(make_pair(s, i));
}
cin >> m;
for(int i=1; i<=m; i++) {
string s,t; double w;
cin>> s >> w >> t;
int u = name.find(s)->second;
int v = name.find(t)->second;
add_edge(u,v,w);
} if(!spfa(1)) printf("Case %d: Yes\n", ca++);
else printf("Case %d: No\n", ca++);
} return 0;

}


####bellman-ford法:875ms
``` cpp
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 1100
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std; int m,n,k;
int edges, u[maxn], v[maxn];
double w[maxn];
int first[maxn], next[maxn];
double dis[maxn]; void add_edge(int s, int t, double val) {
u[edges] = s; v[edges] = t; w[edges] = val;
next[edges] = first[s];
first[s] = edges++;
} bool bellman(int s) {
for(int i=1; i<=n; i++) dis[i]=0; dis[s] = 1; for(int i=1; i<=n; i++) {
for(int e=0; e<edges; e++) {
double tmp = dis[u[e]] * w[e];
if(dis[v[e]] < tmp) {
dis[v[e]] = dis[u[e]] * w[e];
if(i == n) return 0;
}
}
} return 1;
} map<string,int> name; int main(int argc, char const *argv[])
{
//IN; int ca = 1;
while(scanf("%d", &n) != EOF && n)
{
memset(first, -1, sizeof(first));
edges = 0;
name.clear(); for(int i=1; i<=n; i++) {
string s; cin >> s;
name.insert(make_pair(s, i));
}
cin >> m;
for(int i=1; i<=m; i++) {
string s,t; double w;
cin>> s >> w >> t;
int u = name.find(s)->second;
int v = name.find(t)->second;
add_edge(u,v,w);
} if(!bellman(1)) printf("Case %d: Yes\n", ca++);
else printf("Case %d: No\n", ca++);
} return 0;
}

floyd法:875ms

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 35
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std; int m,n,k;
double dis[maxn][maxn]; void floyd() {
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
if(dis[i][j] < dis[i][k]*dis[k][j])
dis[i][j] = dis[i][k] * dis[k][j];
} map<string,int> name; int main(int argc, char const *argv[])
{
//IN; int ca = 1;
while(scanf("%d", &n) != EOF && n)
{
name.clear();
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
dis[i][j] = (i==j? 1.0:inf); for(int i=1; i<=n; i++) {
string s; cin >> s;
name.insert(make_pair(s, i));
}
cin >> m;
for(int i=1; i<=m; i++) {
string s,t; double w;
cin>> s >> w >> t;
int u = name.find(s)->second;
int v = name.find(t)->second;
dis[u][v] = w;
} floyd(); int flag = 1;
for(int i=1; i<=n; i++)
if(dis[i][i] > 1.0) {flag = 0;break;} if(!flag) printf("Case %d: Yes\n", ca++);
else printf("Case %d: No\n", ca++);
} return 0;
}

POJ 2240 Arbitrage (求负环)的更多相关文章

  1. POJ 2240 Arbitrage (spfa判环)

    Arbitrage Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of ...

  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 3259 Wormholes(最短路径,求负环)

    POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...

  4. 最短路(Floyd_Warshall) POJ 2240 Arbitrage

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

  5. bzoj 1486: [HNOI2009]最小圈 dfs求负环

    1486: [HNOI2009]最小圈 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1022  Solved: 487[Submit][Status] ...

  6. Contest20140710 loop bellman-ford求负环&&0/1分数规划

    loop|loop.in|loop.out 题目描述: 给出一个有向带权图,权为边权,求一个简单回路,使其平均边权最小. 简单回路指不多次经过同一个点的回路. 输入格式: 第一行两个整数,表示图的点数 ...

  7. poj 2240 Arbitrage 题解

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

  8. POJ3259 Wormholes —— spfa求负环

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  9. poj 2240 Arbitrage(Bellman_ford变形)

    题目链接:http://poj.org/problem?id=2240 题目就是要通过还钱涨自己的本钱最后还能换回到自己原来的钱种. 就是判一下有没有负环那么就直接用bellman_ford来判断有没 ...

  10. ACM: POJ 3259 Wormholes - SPFA负环判定

     POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu   ...

随机推荐

  1. Ubuntu 12.04搭建MTK 6577 安卓开发环境

    Ubuntu 12.04搭建 MTK 6577安卓开发环境 1.       下载并安装Vmware虚拟机: 2.       下载并在虚拟机上安装Ubuntu 12.04 iso 安装包:下载地址: ...

  2. BZOJ 2337 XOR和路径(高斯消元)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2337 题意:给定一个带权无向图.从1号点走到n号点.每次从当前点随机(等概率)选择一条相 ...

  3. [Codeforces677B]Vanya and Food Processor(模拟,数学)

    题目链接:http://codeforces.com/contest/677/problem/B 题意:n个土豆,每个土豆高ai.现在有个加工机,最高能放h,每次能加工k.问需要多少次才能把土豆全加工 ...

  4. 10 Useful du (Disk Usage) Commands to Find Disk Usage of Files and Directories

    The Linux “du” (Disk Usage) is a standard Unix/Linux command, used to check the information of disk ...

  5. UVa 11235 (RMQ) Frequent values

    范围最值问题,O(nlogn)的预处理,O(1)的查询. 这个题就是先对这些数列进行游程编码,重复的元素只记录下重复的次数. 对于所查询的[L, R]如果它完全覆盖了某些连续的重复片段,那么查询的就是 ...

  6. Codeforces Round #272 (Div. 2)

    A. Dreamoon and Stairs 题意:给出n层楼梯,m,一次能够上1层或者2层楼梯,问在所有的上楼需要的步数中是否存在m的倍数 找出范围,即为最大步数为n(一次上一级),最小步数为n/2 ...

  7. PHP单元测试工具PHPUnit初体验

    今天接到了个任务,需要对数字进行计算,因为涉及到整数,小数,和科学计数法等很多条件,所以人工测试非常麻烦,于是想到了PHP的单元测试工具PHPUnit,所以写个文档备查. 看了PHPUnit的文档之后 ...

  8. windows 下FFMPEG的编译方法 附2012-9-19发布的FFMPEG编译好的SDK下载

    经过一晚上加一上午的奋斗,终于成功编译出了最新版的FFMPEG,下面是我编译的心得,因为是最新的,应该会对大家有用,编译的FFMPEG的版本是0.11.2,2012-09-19新发布的版本 平台:WI ...

  9. 搜集的一些RTMP项目,有Server端也有Client端

    查询一些RTMP的协议封装时找到了一些RTMP开源项目,在这里列举一下,以后有时间或是有兴趣可以参考一下: just very few of them. Red5 only contains a se ...

  10. ios实现类似魔兽小地图功能 在

    写了一个类似魔兽小地图功能的控件. 比如你有一个可以放大缩小的scrollView.会在里面进行一些放大缩小,点击里面的按钮呀,等操作. 这个小地图控件.就会和你的大scrollView同步.并有缩略 ...