poj 2240 floyd算法
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 17349 | Accepted: 7304 |
Description
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 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
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
在这一题求的是从起点開始通过一系列的路径最后回到起点是否能找到一条路使得他们的乘积>1,所以map[i][i]初始化就不是1。而是0.这里的方法是用floyd求出每一个通路的最大值,不断的更新以达到求最大值的目的。
開始没有想到。想用深搜来实现,但是这样的方法更简单高效
#include<iostream>
#include<map>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std; double g[50][50];
int main()
{
int n,w;
double val;
string s,s1,s2;
map<string,int>q;
int cas=0;
while(~scanf("%d",&n)&&n)
{
memset(g,0,sizeof(g));
for(int i=0;i<n;i++)
{
cin>>s;
q[s]=i;
}
scanf("%d",&w);
while(w--)
{
cin>>s1>>val>>s2;
g[q[s1]][q[s2]]=val;
} for(int k=0;k<n;k++)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
g[i][j]=max(g[i][j],g[i][k]*g[k][j]);
}
}
}
int ok=0;
for(int i=0;i<n;i++)
{
if(g[i][i]>1)
{
ok=1;
break;
}
}
printf("Case %d: %s\n",++cas,ok?"Yes":"No");
}
return 0;
}
poj 2240 floyd算法的更多相关文章
- Poj(2240),Floyd求汇率是不是赚钱
题目链接:http://poj.org/problem?id=2240. Floyd算法修改一下,我要最大路径(通过转汇率变到最大)改成max. #include <iostream> # ...
- poj 2240(floyd)
http://poj.org/problem?id=2240 题意:有些人会利用货币的不用汇率来进行套现,比如1美元换0.5英镑,而1英镑又可以换10法郎,而1法郎又可以换0.21的美元,那么经过货币 ...
- 图论---POJ 3660 floyd 算法(模板题)
是一道floyd变形的题目.题目让确定有几个人的位置是确定的,如果一个点有x个点能到达此点,从该点出发能到达y个点,若x+y=n-1,则该点的位置是确定的.用floyd算发出每两个点之间的距离,最后统 ...
- poj 2240 Arbitrage (Floyd)
链接:poj 2240 题意:首先给出N中货币,然后给出了这N种货币之间的兑换的兑换率. 如 USDollar 0.5 BritishPound 表示 :1 USDollar兑换成0.5 Britis ...
- (poj 3660) Cow Contest (floyd算法+传递闭包)
题目链接:http://poj.org/problem?id=3660 Description N ( ≤ N ≤ ) cows, conveniently numbered ..N, are par ...
- ACM: POJ 3660 Cow Contest - Floyd算法
链接 Cow Contest Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Descri ...
- [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)
Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...
- POJ 1502 MPI Maelstrom(模板题——Floyd算法)
题目: BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distri ...
- poj 2139 奶牛拍电影问题 floyd算法
题意:奶牛拍一系列电影,n头牛拍m部电影,同一部电影种的搭档们距离为1,求最小距离? 思路:Floyd 图 最短路径 存图: 初始化图 for (int i = 0; i <= n; i++) ...
随机推荐
- GO语言为结构体排序
package main import ( "fmt" "io/ioutil" "sort" "time" ) type ...
- CentOS 7.0yum安装MySQL
CentOS 7.0yum安装MySQL 1.下载mysql的repo源 $ wget http://repo.mysql.com/mysql-community-release-el7-5.noar ...
- GridDataView实现 点击任意一格可以修改
直接上代码好了 private void dgv1Member_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { string ...
- javafx ComboBox Event and change cell color
public class EffectTest extends Application { public static void main(String[] args) { launch(args); ...
- 【CS Round #39 (Div. 2 only) C】Reconstruct Sum
[Link]:https://csacademy.com/contest/round-39/task/reconstruct-sum/ [Description] 给你一个数字S; 让你找有多少对A, ...
- PatentTips - Transparent unification of virtual machines
BACKGROUND Virtualization technology enables a single host computer running a virtual machine monito ...
- python生成md5, shell生成md5
echo -n 'aaa'|md5sum|cut -d ' ' -f1 python用hashlib md5=hashlib.md5(mid.upper()).hexdigest().upper()
- swift 一疑问:reduce
var products = [ Product(name: "Kayak", description: "A boat for one person",p ...
- [Angular & Unit Testing] Automatic change detection
When you testing Component rendering, you often needs to call: fixture.detectChanges(); For example: ...
- 【Material Design视觉设计语言】应用样式设计
作者:郭孝星 微博:郭孝星的新浪微博 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells Github:https://github.co ...