HDU 1217-Arbitrage(Floyd)
来源:互联网 发布:淘宝流量如何申请退款 编辑:程序博客网 时间:2024/05/21 19:26
address:http://acm.hdu.edu.cn/showproblem.php?pid=1217
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
正式开始最短路的第一道题, 但是 这确实是最短路,这不明明最长路 ,求权值最大,心在滴血,幸亏写出来了
这里面最上面的函数 值转换字符串为下标,不想用map 所以创建二维数组 ,这样就能存入图中
上代码:
#include <iostream>#include <bits/stdc++.h>using namespace std;#define maxn 1001double Map[maxn][maxn];char str[maxn][maxn];int n,m;int trans(char *a) //字符串换成下标{ for (int i=0; i<n; i++) { if (strcmp(a, str[i])==0) return i; } return -1;}void floyd(){ int i,j,k; for (k=0; k<n; k++) { for (i=0; i<n; i++) { for (j=0; j<n; j++) { if (Map[i][j]<Map[i][k]*Map[k][j]) Map[i][j] = Map[i][k]*Map[k][j]; } } }}int main(){ int t=0; while (scanf("%d",&n),n) { int i; memset(str, 0,sizeof(str)); for (int i=0; i<n; i++) { scanf("%s",str[i]); } memset(Map,0,sizeof(Map)); for (i=0; i<n; i++) Map[i][i] = 1.0; /*for(int i=0;i<n;i++) for(int j=0;j<n;j++) { if(i==j) Map[i][j]=1.0; else Map[i][j]=0.0; }*/ scanf("%d",&m); for (i=0; i<m; i++) { char a[maxn], b[maxn]; double c; scanf("%s%lf%s",a,&c,b); int x = trans(a); int y = trans(b); Map[x][y]=c; } floyd(); int flag = 0; for (i=0; i<n; i++) { //cout<<Map[i][i]<<endl; if (Map[i][i]>1.0) flag = 1; } if (flag == 1) printf("Case %d: Yes\n",++t); else printf("Case %d: No\n",++t); } return 0;}
- hdu 1217 Arbitrage--Floyd
- hdu 1217 Arbitrage 【floyd】
- hdu 1217Arbitrage floyd
- hdu 1217 Arbitrage (Floyd)
- HDU 1217-Arbitrage(Floyd)
- 【变形Floyd】HDU-1217 Arbitrage
- HDU 1217 Arbitrage floyd变形
- hdu 1217 Arbitrage Floyd || SPFA
- hdu 1217 Arbitrage (Floyd)
- HDU 1217 Arbitrage 【Floyd + map】
- hdu 1217Arbitrage(Floyd)
- HDU 1217 Arbitrage(floyd)
- (Floyd)【HDU 1217】Arbitrage
- Arbitrage hdu 1217 最长路Floyd
- hdu 1217 Arbitrage (Floyd算法)
- HDU 1217 Arbitrage 货币交换(floyd)
- hdu 1217 Arbitrage (floyd+map)
- hdu 1217 Arbitrage (Floyd + 最大路径)
- CAS学习笔记
- 为你的Android程序选择合适的数据容器
- Python、requests模块使用代理服务器实现微博访问
- uva
- BZOJ 1038 [ZJOI 2008] 瞭望塔
- HDU 1217-Arbitrage(Floyd)
- LeetCode-141 Linked List Cycle
- 20个高级Java面试题汇总
- MyBatis使用总结
- spring 设置通过代码启动加载配置文件和设置启动顺序
- opencv 高斯混合模型提取视频或者图片的前景
- redis必杀命令:服务器
- 设计模式の工厂模式
- TCP长连接和短连接的区别