Arbitrage(bellman_ford)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16652 | Accepted: 7004 |
Description
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
Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n.
Output
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
Source
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
const int M = , inf = 0x3f3f3f3f;
struct Arbitrage
{
int u , v ;
double r ;
}e[M * M];
int n , m ;
char cur[M][] ;
char a[] , b[] ;
double d[M] ; void init (char a[] , char b[] , int no)
{
for (int i = ; i <= n ; i++) {
if (strcmp (cur[i] , a) == )
e[no].u = i ;
if (strcmp (cur[i] , b) == )
e[no].v = i ;
}
} int bellman_ford (int o)
{
for (int i = ; i <= n ; i++)
d[i] = ;
d[o] = 1.0 ;
double temp = 1.0 ;
bool flag ;
for (int i = ; i <= n ; i++) {
flag = ;
for (int j = ; j < m ; j++) {
if (d[e[j].v] < d[e[j].u] * e[j].r) {
d[e[j].v] = d[e[j].u] * e[j].r ;
flag = ;
}
if (d[o] > temp) {
return true ;
}
}
}
return false ;
} int main ()
{
//freopen ("a.txt" , "r" , stdin) ;
int ans = ;
while (~ scanf ("%d" , &n)) {
if (n == )
break ;
getchar () ;
for (int i = ; i <= n ; i++) {
gets (cur[i]) ;
}
scanf ("%d" , &m) ;
for (int i = ; i < m ; i++) {
cin >> a >> e[i].r >> b ;
init (a , b , i) ;
}
/* for (int i = 0 ; i < m ; i++) {
printf ("u = %d , v = %d , r = %.2f\n" , e[i].u , e[i].v , e[i].r) ;
}*/
int i ;
for (i = ; i <= n ; i++) {
if (bellman_ford (i)) {
printf ("Case %d: Yes\n" , ans++) ;
// printf ("Arbitrage num : %d\n" , i) ;
break ;
}
/*printf ("%d team: \n" , i) ;
for (int j = 1 ; j <= n ; j++)
printf ("%.2f " ,d[j]) ;
puts ("") ; */
}
if (i == n + )
printf ("Case %d: No\n" , ans++) ;
}
return ;
}
Arbitrage(bellman_ford)的更多相关文章
- [ACM] hdu 1217 Arbitrage (bellman_ford最短路,推断是否有正权回路或Floyed)
Arbitrage Problem Description Arbitrage is the use of discrepancies in currency exchange rates to tr ...
- POJ 2240 Arbitrage Bellman_ford 判读是否存在正环
和POJ1860差不多,就是用bellmanford判读是否存在正环,注意的是同种货币之间也可以交换,就是说:A货币换A货币汇率是2的情况也是存在的. #include<stdio.h> ...
- POJ 2240 Arbitrage【Bellman_ford坑】
链接: http://poj.org/problem?id=2240 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ 2240 - Arbitrage(bellman_ford & floyd)
题意: 给出一些货币和货币之间的兑换比率,问是否可以使某种货币经过一些列兑换之后,货币值增加. 举例说就是1美元经过一些兑换之后,超过1美元.可以输出Yes,否则输出No. 分析: 首先我们要把货币之 ...
- poj 2240 Arbitrage (最短路 bellman_ford)
题目:http://poj.org/problem?id=2240 题意:给定n个货币名称,给m个货币之间的汇率,求会不会增加 和1860差不多,求有没有正环 刚开始没对,不知道为什么用 double ...
- poj 2240 Arbitrage(Bellman_ford变形)
题目链接:http://poj.org/problem?id=2240 题目就是要通过还钱涨自己的本钱最后还能换回到自己原来的钱种. 就是判一下有没有负环那么就直接用bellman_ford来判断有没 ...
- POJ 1860 Currency Exchange + 2240 Arbitrage + 3259 Wormholes 解题报告
三道题都是考察最短路算法的判环.其中1860和2240判断正环,3259判断负环. 难度都不大,可以使用Bellman-ford算法,或者SPFA算法.也有用弗洛伊德算法的,笔者还不会SF-_-…… ...
- Nyoj Arbitrage(Floyd or spfa or Bellman-Ford)
描述Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a curren ...
- POJ 2240 - Arbitrage - [bellman-ford求最短路]
Time Limit: 1000MS Memory Limit: 65536K Description Arbitrage is the use of discrepancies in currenc ...
随机推荐
- EF 基本数据过滤
没猜错的话, 你们一定会和一大堆查询条件过不去, 重复的写,反复的写, 写到山崩地裂. 今天看了园友的文字:实体框架高级应用之动态过滤 EntityFramework DynamicFilters 我 ...
- IDEA SSH
被IDEA各种虐0.0各种配置,没有人教就百度,别人也弄了点,然后整整花了一天,从eclipse转到IDEA真的不容易啊TT
- 翻译-微服务API Gateway
原文地址:http://microservices.io/patterns/apigateway.html,以下是使用google翻译对原文的翻译. 让我们想象一下你正在建立一个使用微服务模式的网上商 ...
- Orchard创建自定义表单
本文链接:http://www.cnblogs.com/souther/p/4520130.html 主目录 自定义表单模块可以用来获取网站前台用户的信息.自定义表单需要与一个内容类型结合使用.它可以 ...
- xml基本操作
在实际项目中遇到一些关于xml操作的问题,被逼到无路可退的时候终于决定好好研究xml一番.xml是一种可扩展标记语言,可跨平台进行传输,因此xml被广泛的使用在很多地方. 本文由浅入深,首先就xml的 ...
- AngularJS——grunt神器的安装
前言: 刚开始学 angularJS,在慕课网上看的大漠老师的视频(http://www.imooc.com/learn/156),里面刚开始讲述了前端开发-调试-测试所使用的手段和工具,本人对前端开 ...
- NABCD模型需求分析
仓库管理系统的NABCD模型 N-Need仓库管理是与我们日常生活息息相关的问题,随着改革开放的不断深入,经济飞速的发展,企业要想生存.发展,要想在激烈的市场竞争中立于不败之地,没有现代化的管理是万万 ...
- Xamarin.Forms——尺寸大小(五 Dealing with sizes)
如之前所见的大量可视化元素均有自己的尺寸大小: iOS的状态栏高度为20,所以我们需要调整iOS的页面的Padding值,留出这个高度. BoxView设置它的默认宽度和高度为40. Frame的默认 ...
- 【BZOJ-1030】文本生成器 AC自动机 + DP
1030: [JSOI2007]文本生成器 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 3253 Solved: 1330[Submit][Stat ...
- STL Iterators
Summary of Chapter 33 STL Iterators from The C++ Programming Language 4th. Ed., Bjarne Stroustrup. - ...