Poj_2240 Arbitrage(最短路)
来源:互联网 发布:苹果网络锁破解 编辑:程序博客网 时间:2024/05/17 22:31
题意:
套利就是利用汇率的差异使自己的财富得到增加。给出几种货币之间的汇率,问能否通过套利使得财富增加。
思路:
转换为寻找有向图中是否有环。使用Bellman-Ford和SPFA分别实现了一下,SPFA稍微快一点,注意SPFA是入队超过N次。
代码实现:
//Bellman-Ford#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <algorithm>using namespace std;const int MAX_V = 32;struct Edge{ int from; int to; double rate;};int N,M;int cnt;bool flag;double curr[MAX_V];map<string,int> mm;Edge edge[MAX_V*MAX_V];bool find_loop();int main(){ cnt = 1; while( scanf("%d",&N) != EOF ){ if( N == 0 ){ break; } string a,b; mm.clear(); for( int i = 0; i < N; i++ ){ cin>>a; mm[a] = i+1; } double rate; scanf("%d",&M); for( int i = 0; i < M; i++ ){ cin>>a>>rate>>b; edge[i].from = mm[a]; edge[i].to = mm[b]; edge[i].rate = rate; } flag = find_loop(); if( flag == true ){ printf("Case %d: Yes\n",cnt++); } else{ printf("Case %d: No\n",cnt++); } } return 0;}bool find_loop(){ memset(curr,0,sizeof(curr)); curr[1] = 1; for( int i = 0; i < N; i++ ){ for( int j = 0; j < M; j++ ){ Edge e = edge[j]; if( curr[e.to] < curr[e.from]*e.rate ){ curr[e.to] = curr[e.from]*e.rate; if( i == N-1 ){ return true; } } } } return false;}//SPFA#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <map>#include <vector>#include <queue>using namespace std;const int MAX_V = 32;struct Edge{ int to; double rate; Edge(const int to,const double rate){ this->to = to; this->rate = rate; }};int N,M;int cnt;bool flag;int num[MAX_V];queue<int> que;bool inque[MAX_V];map<string,int> mm;double curr[MAX_V];vector<Edge> edge[MAX_V];int main(){ cnt = 1; while( scanf("%d",&N) != EOF ){ if( N == 0 ){ break; } string a,b; mm.clear(); for( int i = 0; i < MAX_V; i++ ){ edge[i].clear(); } for( int i = 0; i < N; i++ ){ cin>>a; mm[a] = i+1; } double rate; scanf("%d",&M); for( int i = 0; i < M; i++ ){ cin>>a>>rate>>b; int pa = mm[a]; int pb = mm[b]; edge[pa].push_back(Edge(pb,rate)); } flag = false; memset(num,0,sizeof(num)); memset(curr,0,sizeof(curr)); memset(inque,false,sizeof(inque)); curr[1] = 1; que.push(1); inque[1] = true; num[1]++; while( !que.empty() ){ int tmp = que.front(); que.pop(); inque[tmp] = false; if( flag == true ){ break; } int len = edge[tmp].size(); for( int i = 0; i < len; i++ ){ Edge e = edge[tmp][i]; if( curr[e.to] < curr[tmp]*e.rate ){ curr[e.to] = curr[tmp]*e.rate; if( inque[e.to] == false ){ inque[e.to] = true; num[e.to]++; que.push(e.to); if( num[e.to] > N ){ flag = true; break; } } } } } if( flag == true ){ printf("Case %d: Yes\n",cnt++); } else{ printf("Case %d: No\n",cnt++); } } return 0;}
0 0
- Poj_2240 Arbitrage(最短路)
- poj_2240 Arbitrage
- poj_2240 Arbitrage(Bellman_ford + map)
- Arbitrage(最短路问题)
- Arbitrage--最短路
- hdu--Arbitrage(最短路)
- poj 2240 Arbitrage 最短路
- POJ2240 Arbitrage(最短路)
- POJ2240 Arbitrage 最短路 bellmanford
- POJ 2240 Arbitrage(最短路)
- 1217 Arbitrage(最短路)
- POJ-2240 Arbitrage(最短路)
- zoj 1092 Arbitrage(最短路,floyd)
- HDU 1217 Arbitrage 乘法最短路
- poj 2240 Arbitrage(最短路+Bellman-Ford)
- uva 104 - Arbitrage(最短路Floyd)
- POJ 2240 Arbitrage(最短路 Floyd)
- hdu 1217 && poj2240 Arbitrage 最短路
- 复制构造函数(1)
- 使用VC++获得从摄像头的图像(转)
- Java泛型
- 任务2
- 92. Reverse Linked List II
- Poj_2240 Arbitrage(最短路)
- (十四)桥接模式详解(都市异能版)
- 直接插入——InsertSort
- C语言实现有限状态机FSM
- 初级汇编程序(三)
- linux常用命令
- 从0开始学习java——从慕课网到毕向东视频
- 全民直播时代,视频云带来了哪些价值?
- 智和网管平台(SugarNMS)油气管线网管系统解决方案