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. 逆袭之旅DAY16.东软实训.Oracle.索引

    2018-07-12 14:44:27 四.索引1.创建索引手动创建:create index 索引名 on 表名(列名,[列名,...])create table employee(pno numb ...

  2. UVa 10891 - Game of Sum 动态规划,博弈 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  3. TModalResult 和 MessageBox 返回值

    //其实是对应的{ TModalResult values } const mrNone = ; mrOk = idOk; mrCancel = idCancel; mrAbort = idAbort ...

  4. Android system :灯光系统_HAL_lights

    一.android灯光系统框架: Java: frameworks/base/services/core/java/com/android/server/lights/LightsService.ja ...

  5. 卷积神经网络-Dropout

    dropout 是神经网络用来防止过拟合的一种方法,很简单,但是很实用. 基本思想是以一定概率放弃被激活的神经元,使得模型更健壮,相当于放弃一些特征,这使得模型不过分依赖于某些特征,即使这些特征是真实 ...

  6. c#继承中的函数调用实例

    using System;   namespace Test {     public class Base     {         public void Print()         {   ...

  7. ChinaCock界面控件介绍-CCButton

    即将发布的ChinaCock新版本,将带来一个CCButton控件,实现可视按钮.Delphi原生的Button,在上面滑动后,当释放手指时会误触发OnClick事件,这不是我们想要的结果,CCBut ...

  8. L2-002. 链表去重(数组模拟)

    L2-002. 链表去重 因为数值比较小,所以直接用数组来模拟 #include<cstdio> #include<cstring> #include<iostream& ...

  9. tomcat自动缓存的几种解决方式

    第一种方法:打开一个项目,这里我打开的Mail项目,然后点击Myeclipse菜单栏中的project-选择clean: 选择要clean的项目,确定即可不用进入tomcat服务器直接清理缓存. 上面 ...

  10. Python 笔试 —— 效率与优雅

    1. 效率 字符串拼接: 加号拼接字符串将造成对象的创建和垃圾的回收: 使用字符串的 join 方法对尤其是循环中的字符串进行拼接(先将不断出现的字符串 append 到 一个 list 中,再进行 ...