DFS判断正环
Arbitrage
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4123 Accepted Submission(s): 1878
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.
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.
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
Case 1: Yes
Case 2: No
程序:
#include"stdio.h"
#include"string.h"
#include"iostream"
#include"map"
#include"string"
#include"queue"
#include"stdlib.h"
#include"math.h"
#define M 40
#define eps 1e-10
#define inf 99999999
#define mod 1000000000
using namespace std;
struct st
{
int u,v,next;
double w;
}edge[M*M*2];
int head[M],t,cnt[M],n;
double dis[M];
void init()
{
t=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,double w)
{
edge[t].u=u;
edge[t].v=v;
edge[t].w=w;
edge[t].next=head[u];
head[u]=t++;
}
int dfs(int u)
{
cnt[u]=1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(dis[v]<dis[u]*edge[i].w)
{
dis[v]=dis[u]*edge[i].w;
if(cnt[v])
return 1;
if(dfs(v))
return 1;
}
}
cnt[u]=0;
return 0;
}
int solve()
{
memset(cnt,0,sizeof(cnt));
for(int i=1;i<=n;i++)//防止题目不连通的情况
{
memset(dis,0,sizeof(dis));
dis[i]=1;
if(dfs(i))
return 1;
}
return 0;
}
int main()
{
int m,i,kk=1;
while(scanf("%d",&n),n)
{
map<string,int>mp;
char ch1[111],ch2[111];
for(i=1;i<=n;i++)
{
scanf("%s",ch1);
mp[ch1]=i;
}
scanf("%d",&m);
init();
double c;
while(m--)
{
scanf("%s%lf%s",ch1,&c,ch2);
add(mp[ch1],mp[ch2],c);
}
int ans=solve();
printf("Case %d: ",kk++);
if(ans)
printf("Yes\n");
else
printf("No\n");
}
}
DFS判断正环的更多相关文章
- hdu 1317 XYZZY【Bellheman_ford 判断正环小应用】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1317 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- Currency Exchange POJ - 1860 (spfa判断正环)
Several currency exchange points are working in our city. Let us suppose that each point specializes ...
- poj1860 兑换货币(bellman ford判断正环)
传送门:点击打开链接 题目大意:一个城市有n种货币,m个货币交换点,你有v的钱,每个交换点只能交换两种货币,(A换B或者B换A),每一次交换都有独特的汇率和手续费,问你存不存在一种换法使原来的钱更多. ...
- Currency Exchange POJ - 1860 spfa判断正环
//spfa 判断正环 #include<iostream> #include<queue> #include<cstring> using namespace s ...
- HDU 1317(Floyd判断连通性+spfa判断正环)
XYZZY Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- HDU 1317XYZZY spfa+判断正环+链式前向星(感觉不对,但能A)
XYZZY Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- 最优比例生成环(dfs判正环或spfa判负环)
http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- poj - 1860 Currency Exchange Bellman-Ford 判断正环
Currency Exchange POJ - 1860 题意: 有许多货币兑换点,每个兑换点仅支持两种货币的兑换,兑换有相应的汇率和手续费.你有s这个货币 V 个,问是否能通过合理地兑换货币,使得你 ...
- HDU 1317 XYZZY【Bellman_Ford判断正环】
题意:给出n个房间,初始在房间1有100的能量值,每次进入一个房间,能量值可能增加也可能减小,(是点权,不是边权),问能否到达终点的时候能量值还为正 这题自己写的时候wa--wa-- 后来看了题解,还 ...
随机推荐
- fanqiang_bak
- gpio irq
/***************************************************************** * gpio irq * * 一直以来都没了解过gpio的irq, ...
- imx6 电容屏参数更改
imx6使用电容屏时需要获取对应的usb的event.其中用到了shell的一些命令.分析如下. # inputCheckLine=`cat /proc/bus/input/devices | gre ...
- 优矿众包对冲基金计划”优选策略---100w实盘资金管理权!!
https://uqer.io/contest/ http://www.cnblogs.com/dunitian/p/4939369.html 优连
- 在linux上开发210的hdmi-servers输出
这段时间一直在研究hdmi-servers,因为友善对这个在是闭源的,所以由于兴趣的关系和工作的关系,决定自己写一个hdmi-servers. 在hdmi中,最关键的是弄清楚了Hdmi显示数据的怎么来 ...
- 【Java面试题】7 构造器Constructor是否可被override?
构造器Constructor不能被继承,因此不能重写Override,但可以被重载Overload. Constructor不能被继承,所以Constructor也就不能被override.每一个类必 ...
- linux,ubuntu14.04.5下安装搜狗输入法
参考内容: http://www.linuxidc.com/Linux/2015-03/114347.htm https://jingyan.baidu.com/album/ad310e80ae6d9 ...
- c++ 静态成员变量
在C++中,静态成员是属于整个类的而不是某个对象,静态成员变量只存储一份供所有对象共用.所以在所有对象中都可以共享它.使用静态成员变量实现多个对象之间的数据共享不会破坏隐藏的原则,保证了安全性还可以节 ...
- Kubernetes1.1源码分析(二)
3.controller-manager模块 在controller manager模块中有几个重要的结构体.当中包含EndpointController.ReplicationManager.GCC ...
- Loadrunner C 编程_1
就目前的了解.Loadrunner的脚本语言其实和C没什么区别.他内部的好多机制都是C实现的. 不过是一种“类C” 所以我从几个方面分析 1:定义常量变量和C一样 2:在LR中,C的变量和LR的参数是 ...