HDU 1217 Arbitrage (Floyd + SPFA判环)
来源:互联网 发布:迅雷看看优化版 编辑:程序博客网 时间:2024/05/29 10:56
题目链接:HDU 1217 Arbitrage
简单的货币转换问题,给定多种货币,以及货币之间的汇率,问能否通过货币的转换实现收益。
例如:
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.
【解法1】
Floyd算法。
Floyd算法可以求任意两点的最短距离, 这里通过小小的变形。求最大”收益“;
#include <iostream>#include <cstdio>#include <algorithm>#include <map>using namespace std;double maze[40][40];int main(){ int cas=1; int n; while(scanf("%d",&n)!=EOF && n){ char tmp[30]; map<string,int>mp; for(int i=0;i<n;i++){ scanf(" %s",tmp); mp[tmp] = i; } int m; scanf("%d",&m); char st[30],end[30]; double rate; memset(maze,0,sizeof(maze)); //初始化为 0 maze[0][0] = 1; //起点为1; for(int i=0;i<m;i++){ scanf(" %s%lf%s",st,&rate,end); maze[mp[st]][mp[end]] = rate; } for(int k=0;k<n;k++){ for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(maze[i][j] < maze[i][k] * maze[k][j]){ //这里是乘法,看是否通过汇率转换实现增大本金; maze[i][j] = maze[i][k] * maze[k][j]; } } } } cout<<"Case "<<cas++<<": "; if(maze[0][0] > 1){ cout<<"Yes"<<endl; } else cout<<"No"<<endl; } return 0;}
【解法2】
SPFA 判环 ,如果起点可以通过汇率转换增大。那么在SPFA的松弛操作中会无限加入队列,判断是否重复加入n次以上即可。
和我上一篇博客的解法一致。
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <queue>#include <map>using namespace std;double maze[40][40];const int maxn = 40;double dis[maxn]; //记录各个种类money的当前值,初始化为0 ,起点为1;bool vis[maxn]; //标记是否在队列之中int cnt[maxn]; //判环int n;int SPFA(){queue<int>Q;Q.push(0); vis[0]=1; dis[0] = 1;cnt[0]++;while(!Q.empty()){int now = Q.front(); Q.pop(); vis[now] = false;for(int i=0;i<n;i++){double rate = maze[now][i];if(dis[i] < dis[now] * rate) //如果可以增大{dis[i] = dis[now] * rate;if(!vis[i]){vis[i]=1;Q.push(i);}if(++cnt[i] > n){ //如果节点加入队列超过n次return -1;}}}return 1;}void init(){memset(vis,0,sizeof(vis));memset(cnt,0,sizeof(cnt));memset(dis,0,sizeof(dis));}int main(){int cas=1;map<string,int>mp;while(scanf("%d",&n)!=EOF && n){char tmp[30];mp.clear();for(int i=0;i<n;i++){scanf(" %s",tmp);mp[tmp] = i;}int m;scanf("%d",&m);char st[30],end[30];double rate;init();maze[0][0] = 1;for(int i=0;i<m;i++){scanf(" %s%lf%s",st,&rate,end);maze[mp[st]][mp[end]] = rate;}int ret = SPFA();cout<<"Case "<<cas++<<": ";if(ret == -1)cout<<"Yes"<<endl;else cout<<"No"<<endl;}return 0;}
0 0
- HDU 1217 Arbitrage (Floyd + SPFA判环)
- hdu 1217 Arbitrage Floyd || SPFA
- poj 2204 hdu 1217 Arbitrage spfa判环 解题报告
- HDU 1217 Arbitrage(最短路spfa判环)
- poj2240 Arbitrage (spfa判环)
- hdu 1217 Arbitrage--Floyd
- hdu 1217 Arbitrage 【floyd】
- hdu 1217Arbitrage floyd
- hdu 1217 Arbitrage (Floyd)
- HDU 1217-Arbitrage(Floyd)
- hdu 1217 Arbitrage (spfa算法)
- 【变形Floyd】HDU-1217 Arbitrage
- HDU 1217 Arbitrage floyd变形
- hdu 1217 Arbitrage (Floyd)
- HDU 1217 Arbitrage 【Floyd + map】
- hdu 1217Arbitrage(Floyd)
- HDU 1217 Arbitrage(floyd)
- (Floyd)【HDU 1217】Arbitrage
- js中变量的作用域
- codevs 没有上司的舞会
- 软件开发过程中遇到的问题(Java 、JavaScript)
- 正则表达式
- 【C++面向对象学习笔记】-基础知识
- HDU 1217 Arbitrage (Floyd + SPFA判环)
- Bitmap recycle()说明
- Hadoop 案例13----倒排索引
- 网络编程中的几个常用函数
- Struts2学习之Action接收参数方法二(用DomainModel接收参数(模型驱动))
- iOS触摸事件学习
- android中的颜色代码
- Grunt 单独对css压缩并生成min.css文件
- h:panelGrid、h:panelGroup标签学习