Arbitrage
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 30790   Accepted: 12761

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<=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

用spfa解决POJ2240,因学习spfa 的时间较短,犯了不少错误,在代码行中,会注释出来以备察看一些新手可能需要注意的地方,接下来看题意....

题意:给你一些钱币,看你是否能通过汇率转换,赚到更多的钱。

例如给出   :

  我们看第一个样例,1美元可以兑换0.5英镑,1英镑可以兑换10法郎,但1法郎只能兑换0.21美元。

  那么如果你有1美元,我们先兑换成英镑,在兑换成法郎,最后兑换成美元  == 1*0.5*10*0.21=1.05美元,,,明显看出,经过一系列的兑换之后,我们的金钱更多了....那么再看第二个样例, 我们显然可以知道,此题的题意就是让我们求出一条兑换钱币的次序,或者说路径,看是否能有一条路径兑换之后,我们的钱币比原来更多了。

  那么接下来看代码吧..


#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <queue>
#define MAXN 31
int n,m,head[MAXN],vis[MAXN],cnt[MAXN];
char name[][];  //用来读入各种货币的名称
using namespace std;
typedef struct
{
int start,end;
int next;
double w;
} Edge;
Edge edges[];
double dis[MAXN]; int Find(char str1[])
{
for(int i=; i<=n; i++)
if(strcmp(name[i],str1)==)
return i;//没有考虑找不到的情况,但是实际上,题目不会给出找不到的货币名称....
return -;
} bool spaf(int s)
{
queue<int>que;
memset(dis,,sizeof(dis));
memset(vis,false,sizeof(vis));
memset(cnt,,sizeof(cnt));
vis[s]=dis[s]=;    //这里一开始学习spfa的时候,还保留着用其他方法的习惯,总给vis[1]置为1 ,,但其实这里应该写成vis[s]=1,不然题目就会出现一些小错误,导致过了样例,却还是wa了
que.push(s);
while(!que.empty())
{
int temp=que.front();
que.pop();
vis[temp]=;
for(int i=head[temp]; i!=-; i=edges[i].next)
{int x=edges[i].start,y=edges[i].end;
double w=edges[i].w;
if(dis[y]<dis[x]*w)
{
dis[y]=dis[x]*w;
if(!vis[y])
{
vis[y]=;
if(++cnt[y]>n)    //为了防止某一个节点无限被调用,形成环路,则可以一直赚钱,卡在这里
return true;
que.push(y);
}
}
}
}
return false;
}
int main()
{
int len=;
char start[],end[];
while(scanf("%d",&n)&&n!=)
{
getchar();
for(int i=; i<=n; ++i)
gets(name[i]);
scanf("%d",&m);
memset(head,-,sizeof(head));
for(int i=; i<m; ++i)
{
scanf(" %s %lf %s",start,&edges[i].w,end);
edges[i].start=Find(start);
edges[i].end=Find(end);
edges[i].next=head[edges[i].start];
head[edges[i].start]=i;
}
m=;
for(int i=; i<=n; ++i)
{
if(spaf(i)==true)    
{
m=;
break;
}
}
if(m)
printf("Case %d: Yes\n",len++);
else
printf("Case %d: No\n",len++);
}
return ;
}

用SPFA 解决POJ2240的更多相关文章

  1. SPFA解决单源最短路径

    SPFA(Shortest Path Faster Algorithm): 一:基本算法 在求解单源最短路径的时候,最经典的是 Dijkstra 算法,但是这个算法对于含有负权的图就无能为力了,而 B ...

  2. lightoj 1074 spfa判断负环

     Extended Traffic Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Sub ...

  3. BZOJ 1003 物流运输 题解 【SPFA+DP】

    BZOJ 1003 物流运输 题解 Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的 ...

  4. [Usaco2015 Jan]Grass Cownoisseur Tarjan缩点+SPFA

    考试的时候忘了缩点,人为dfs模拟缩点,没想到竟然跑了30分,RB爆发... 边是可以重复走的,所以在同一个强连通分量里,无论从那个点进入从哪个点出,所有的点一定能被一条路走到. 要使用缩点. 然后我 ...

  5. POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环)

    POJ 1860 Currency Exchange / ZOJ 1544 Currency Exchange (最短路径相关,spfa求环) Description Several currency ...

  6. UESTC - 1987 童心未泯的帆宝和乐爷 (第k短路 A*算法+SPFA算法 模板)

    传送门: http://www.qscoj.cn/#/problem/show/1987 童心未泯的帆宝和乐爷 Edit Time Limit: 10000 MS     Memory Limit: ...

  7. 【BZOJ 2595】2595: [Wc2008]游览计划 (状压DP+spfa,斯坦纳树?)

    2595: [Wc2008]游览计划 Time Limit: 10 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 1572  Solved: 7 ...

  8. 【题解】洛谷P2296 [NOIP2014TG] 寻找道路(SPFA+DFS)

    题目来源:洛谷P2296 思路 一开始看还以为是一道水题 虽然本来就挺水的 本道题的难点在于如何判断是否路径上的点都会直接或者间接连着终点 我们需要在一开始多建一个反向图 然后从终点DFS回去 把路径 ...

  9. 最短路径算法——Dijkstra,Bellman-Ford,Floyd-Warshall,Johnson

    根据DSqiu的blog整理出来 :http://dsqiu.iteye.com/blog/1689163 PS:模板是自己写的,如有错误欢迎指出~ 本文内容框架: §1 Dijkstra算法 §2 ...

随机推荐

  1. 在linux操作系统上进行简单的C语言源码的gcc编译实验

    尝试在linux上用gcc 而非封装完好的codeblocks,vs等ide 来编译c和cpp源程序 首先查看我的gcc版本,我的是VM centos 自带的,没有的话得自行安装,安装上gcc就可以在 ...

  2. 泛目录程序(莲花泛目录程序/黑帽SEO/寄生虫/莲花泛目录解析/泛目录软件)

    莲花泛目录程序强大之处: 1.内容分类详细2.自动推送URL链接3.内置超强原创内容功能系统,页面深受百度搜索引擎喜爱.4.蜘蛛触发繁殖:蜘蛛触发程序任何页面,程序自动生成独立页面并引导繁殖.5.操作 ...

  3. 第三方库requests详解

    Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTT ...

  4. 0 - Visualizing and Understanding Convolutional Networks(阅读翻译)

    卷积神经网络的可视化理解(Visualizing and Understanding Convolutional Networks) 摘要(Abstract) 近来,大型的卷积神经网络模型在Image ...

  5. LeetCode 229. 求众数 II(Majority Element II )

    题目描述 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2,3] 输出: ...

  6. selenium 右侧滚动条操作

    对于web上有右侧滚动条的操作 可用使用JS语句执行 拖到底部 js = "var q=document.documentElement.scrollTop=10000"brows ...

  7. .netcore centos配置systemctl自动启动

    systemd分两种服务系统和用户服务 对应存储位路径为系统(/usr/lib/systemd/system).用户(/etc/systemd/user/) [Unit] Description=ap ...

  8. 自定义zabbix脚本--网卡平均流量

    自定义zabbix脚本--网卡平均流量1. 在客户端修改配置文件 /etc/zabbix/zabbix_agentd.conf需要改动两个地方:(1) UnsafeUserParameters=1(2 ...

  9. 如何在 CentOS 里下载 RPM 包及其所有依赖包

    方法一.利用 Downloadonly 插件下载 RPM 软件包及其所有依赖包 默认情况下,这个命令将会下载并把软件包保存到 /var/cache/yum/ 的 rhel-{arch}-channel ...

  10. Swift小知识点总结

    1>判断输入字符串位数: if phoneTextField.text?.lengthOfBytes(using: .utf8) != 11 { self.showHint(hint: &quo ...