hdu1217

Arbitrage

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4123    Accepted Submission(s): 1878

Problem 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 file 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

程序:

#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判断正环的更多相关文章

  1. hdu 1317 XYZZY【Bellheman_ford 判断正环小应用】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1317 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  2. Currency Exchange POJ - 1860 (spfa判断正环)

    Several currency exchange points are working in our city. Let us suppose that each point specializes ...

  3. poj1860 兑换货币(bellman ford判断正环)

    传送门:点击打开链接 题目大意:一个城市有n种货币,m个货币交换点,你有v的钱,每个交换点只能交换两种货币,(A换B或者B换A),每一次交换都有独特的汇率和手续费,问你存不存在一种换法使原来的钱更多. ...

  4. Currency Exchange POJ - 1860 spfa判断正环

    //spfa 判断正环 #include<iostream> #include<queue> #include<cstring> using namespace s ...

  5. HDU 1317(Floyd判断连通性+spfa判断正环)

    XYZZY Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  6. HDU 1317XYZZY spfa+判断正环+链式前向星(感觉不对,但能A)

    XYZZY Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  7. 最优比例生成环(dfs判正环或spfa判负环)

    http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  8. poj - 1860 Currency Exchange Bellman-Ford 判断正环

    Currency Exchange POJ - 1860 题意: 有许多货币兑换点,每个兑换点仅支持两种货币的兑换,兑换有相应的汇率和手续费.你有s这个货币 V 个,问是否能通过合理地兑换货币,使得你 ...

  9. HDU 1317 XYZZY【Bellman_Ford判断正环】

    题意:给出n个房间,初始在房间1有100的能量值,每次进入一个房间,能量值可能增加也可能减小,(是点权,不是边权),问能否到达终点的时候能量值还为正 这题自己写的时候wa--wa-- 后来看了题解,还 ...

随机推荐

  1. JQueryMobile开发必须的知道的知识

    移动Web页面的基本组成元素: 页面头部,页面内容,页面底部 <!DOCTYPE html> <html> <head> <title>My Page& ...

  2. 关于Cocos2d-x发布游戏的时候遇到的问题和解决

    发布经常会遇到各种各样的问题,发布失败会返回一些值,但是这些值并不是重点,要看发布过程中产生的日志才能真正找到问题所在.我在发布自己做的第一个游戏的时候,遇到了各种各样的问题,不过都一一解决,下面是问 ...

  3. native生成策略:由Hibernate根据所使用的数据库支持能力从identity、sequence或者等生成策略中选择一种

    increment生成策略:当Hibernate准备在数据库表中插入一条新记录时,首先从数据库表中获取当前主键字段的最大值,然后在最大值基础上加1,作为当前持久化对象的标识符属性值.这种策略即incr ...

  4. (转)loff_t *ppos是什么东东

    ssize_t generic_file_read(struct file * filp, char * buf, size_t count, loff_t *ppos) 这是一个文件读函数 我们很容 ...

  5. 修改Java标准库源码

    以下是摘抄,实际操作没有测试   先前我曾提到,原本想借由“改动Java标准库源码”来测知Class object的生成,但由于其ctor原始设计为private,也就是说不可能透过这个管道生成Cla ...

  6. UVa 10633 - Rare Easy Problem

    题目:给定一个数N.去掉末尾的数变成M.如今已知N-M,确定N. 分析:数论.简单题. 设N = 10*a + b { 当中0 ≤ b ≤ 9 }.则M = a: N - M = N - a = 9* ...

  7. gen_server的一些猜测

    1. exit(Pid,Reason)貌似不会引起gen_server的terminate()的执行. 猜测依据:erlang编程指南的第十二章的272页 终止   当从 回调函数中的一个收到stop ...

  8. SQL-字符串连接聚合函数

    原文:http://blog.csdn.net/java85140031/article/details/6820699 问题: userId  role_name         role_id 1 ...

  9. hadoop 安装笔记

    http://www.powerxing.com/install-hadoop/ 查询相关链接~!

  10. 如何POST一个JSON格式的数据给Restful服务

    在Android/java平台上实现POST一个json数据: JSONObject jsonObj = new JSONObject(); jsonObj.put("username&qu ...