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. 【软件工程】Alpha冲刺 (6/6)

    链接部分 队名:女生都队 组长博客: 博客链接 作业博客:博客链接 小组内容 恩泽(组长) 过去两天完成了哪些任务 描述 tomcat的学习与实现 服务器后端部署,API接口的beta版实现 后端代码 ...

  2. Android自定义权限与使用

    1. 如何自定义权限 Android允许我们使用permission标签,在Manifest文件中定义属于自己的权限,一个例子如下, <?xml version="1.0" ...

  3. linux centos6.5 环境下安装redis的过程

    过程还是挺折磨人的!谢谢许正同学一直耐心给我指导,虽然他也很忙.废话不多说: 首先,确保linux虚拟机联网: vm虚拟机>设置>Network Adapter 设置>网络配置设置成 ...

  4. PackageUtils

    import android.content.Context; import android.content.Intent; import android.content.pm.Application ...

  5. 机器学习之DBSCAN聚类算法

    可以看该博客:https://www.cnblogs.com/aijianiula/p/4339960.html 1.知识点 """ 基本概念: 1.核心对象:某个点的密 ...

  6. PCL点云库(Point Cloud Library)简介

    博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=29 什么是PCL PCL(Point Cloud Library)是在吸收了 ...

  7. Linux添加用户/权限/用户主目录等相关

    一. 用户主目录 useradd -d   ${path}   username -d命令是指定用户主目录, 添加完之后  su -  username 默认会转到 这个用户的主目录下,即 ${pat ...

  8. Python类call函数的作用

    call函数可以把类变成函数来调用call方法 class Demo(): def __init__(self, name): self.name = name def __call__(self): ...

  9. centos7最小安装怎么安装防火墙

    CentOS 7.0默认使用的是firewall作为防火墙,需要事先关闭. 关闭firewall: 1 2 3 systemctl stop firewalld.service systemctl d ...

  10. java设计模式中的动态代理

    Java的三种代理模式 1.代理模式 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象访问目标对象.这样做的好处是:可以在目标对象实现的基础上,增强额外的功能操作,即扩 ...