http://acm.hdu.edu.cn/showproblem.php?pid=1217

Arbitrage

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9455    Accepted Submission(s): 4359

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
题目大意:给一堆国家的名字然后给一些国家的货币汇率,然后问能否通过一些转换,使自己获利。
题目分析:可以转化为求最长路问题,也就是求从起点到起点的最长路【只不过每次移动不是加法而是乘法】,看这条路最长能否超过1。也就是说求多起点多终点的最长路,并且由于汇率有大于一的和小于一的,就相当于负权边,所以不能使用DIJ算法,而且是多起点多终点的且数据规模不大,故使用Floyd算法最好。
【Floyd模板:最外层    K   次外层   I 最内层 J  转移方程: qwq[ I ][ J ]  =   max(  qwq[ I ][ J ]   ,  qwq[ I ][ K ] +  qwq[ K ][ J ]   )   也就是最外层的变量作为  "中间变量"  来更新内层】
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
char ss[][];
double qwq[][];
int find(char dd[])
{
for(int i = ; i< n ;i++)
{
if(strcmp(dd,ss[i])==)return i;
}
}
int main()
{
int case1=;
while(scanf("%d",&n)&&n)
{
memset(qwq,,sizeof(qwq));
for(int i = ; i< n ; i++)
{
scanf("%s",ss[i]);
qwq[i][i]=;
}
int m;
scanf("%d",&m);
while(m--)
{
char qq[],ww[];
double asd;
scanf("%s%lf%s",qq,&asd,ww);
int u=find(qq);
int v=find(ww);
qwq[u][v]=asd;
}
for(int i = ; i < n ; i++)
{
for(int j = ; j < n; j++)
{
for(int k = ; k< n ; k++)
{
qwq[j][k]=max(qwq[j][i]*qwq[i][k],qwq[j][k]);
}
}
}
bool flag=false;
for(int i = ; i < n; i++)
{
if(qwq[i][i]>1.0)
{
flag=true;
break;
}
}
printf("Case %d: ",case1++);
if(flag){
printf("Yes\n");
}
else
printf("No\n");
} return ;
}

【HDOJ1217】【Floyd求最长路】的更多相关文章

  1. XYZZY(spfa求最长路)

    http://acm.hdu.edu.cn/showproblem.php?pid=1317 XYZZY Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  2. spfa求最长路

    http://poj.org/problem?id=1932 spfa求最长路,判断dist[n] > 0,需要注意的是有正环存在,如果有环存在,那么就要判断这个环上的某一点是否能够到达n点,如 ...

  3. 训练赛 Grouping(强连通分量缩点 + DAG求最长路)

    http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=158#problem/F 大致题意:给出n个人和m种关系(ti,si),表示ti ...

  4. hdu 1534(差分约束+spfa求最长路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1534 思路:设s[i]表示工作i的开始时间,v[i]表示需要工作的时间,则完成时间为s[i]+v[i] ...

  5. POJ 3592--Instantaneous Transference【SCC缩点新建图 &amp;&amp; SPFA求最长路 &amp;&amp; 经典】

    Instantaneous Transference Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6177   Accep ...

  6. POJ - 3249 Test for Job (在DAG图利用拓扑排序中求最长路)

    (点击此处查看原题) 题意 给出一个有n个结点,m条边的DAG图,每个点都有权值,每条路径(注意不是边)的权值为其经过的结点的权值之和,每条路径总是从入度为0的点开始,直至出度为0的点,问所有路径中权 ...

  7. Vulnerable Kerbals CodeForces - 772C【拓展欧几里得建图+DAG上求最长路】

    根据拓展欧几里得对于同余方程 $ax+by=c$ ,有解的条件是 $(a,b)|c$. 那么对于构造的序列的数,前一个数 $a$  和后一个数 $b$ ,应该满足 $a*x=b(mod m)$ 即 $ ...

  8. HDU - 6201 transaction transaction transaction(spfa求最长路)

    题意:有n个点,n-1条边的无向图,已知每个点书的售价,以及在边上行走的路费,问任选两个点作为起点和终点,能获得的最大利益是多少. 分析: 1.从某个结点出发,首先需要在该结点a花费price[a]买 ...

  9. 洛谷 P3627 [APIO2009]抢掠计划 Tarjan缩点+Spfa求最长路

    题目地址:https://www.luogu.com.cn/problem/P3627 第一次寒假训练的结测题,思路本身不难,但对于我这个码力蒟蒻来说实现难度不小-考试时肛了将近两个半小时才刚肛出来. ...

随机推荐

  1. python小数据池,代码块知识

    一.什么是代码块? 根据官网提示我们可以获知: A Python program is constructed from code blocks. A block is a piece of Pyth ...

  2. VS Code 常用快捷键

    VS Code 常用快捷键 1.注释: 单行注释:ctrl+/, 注释后再按取消 取消单行注释:alt+shift+A 注释后再按取消 2.移动行 向上移动一行:alt+up 向下移动一行:alt+d ...

  3. react+dva+antd/antd-mobile

    github仓库pc: https://github.com/llcMite/react-dva-antd.git github仓库mobile:https://github.com/llcMite/ ...

  4. OOP⑵

    1.问题? 怎么创建对象? 类名 对象名=new 类名(); 在java中只要是看到了()! 这就是方法! 2.构造方法: 创建某个对象的方法! Student stu=new Student(); ...

  5. day02 运算符和编码

    今日所学 主要是运算符和编码的初认识, 1   还有比较运算 ==,!=,<>,>,<,>=,<=等 2  .  赋值运算 =,+=,-=等 还有今天的难点逻辑运算 ...

  6. linux 删除日志

    https://jingyan.baidu.com/album/c1a3101e73129ade656deb9d.html?picindex=2 里面的 ls -s 可以看到目录 https://zh ...

  7. ps基础学习笔记一

    图像?表示分为位图方式和矢量图方式 位图是像素点组成,一副图像所含像素越多,图像的效果就越好 矢量图是基于一定数学方式描述,适合表示色彩较少,一色块为主,曲线简单的图像,文件小ps一般用来处理位图,c ...

  8. W phase 学习

    W phase 的组成:(相关文献发现W phase适用于6级以上的地震) P, PP,S,SS,SP,PS等等长周期的震相: 它的传播机制和whispering gallery 相似. 从简振理论来 ...

  9. Html.DropDownList()的用法

    页面代码如下: <%= Html.DropDownList("Category", ViewData["Categories"] as SelectLis ...

  10. [Paper] Selection and replacement algorithm for memory performance improvement in Spark

    Summary Spark does not have a good mechanism to select reasonable RDDs to cache their partitions in ...