POJ 2240_Arbitrage
来源:互联网 发布:海康提示网络不可达 编辑:程序博客网 时间:2024/05/20 20:45
题意:
给定一系列货币汇率,求能否从一种货币,经过一系列转换,最终转化回更高价值的该种货币。
分析:
即为求最长路并判断是否存在“正”权值回路,这里用的bellmanford算法。
代码:
#include<iostream>#include<cstring>#include<map>using namespace std;const int maxn = 50, maxm =1005;int n, m, flag = 0, cnt = 1;map<string, int>mp;struct edge{ int from, to; double w;}e[maxm];double d[maxn];int find_loop(int v){ memset(d,0,sizeof(d)); d[v] = 1; for(int i = 0; i < n; i ++){ for(int j = 0; j < m; j++){ if(d[e[j].to] < d[e[j].from] * e[j].w){ d[e[j].to] = d[e[j].from] * e[j].w; if(i==n-1) return 1; } } } return 0;}int main (void){ while(cin>>n&&n){ flag = 0; string ci, cj; for(int i = 0; i < n; i ++) {cin>>ci;mp[ci] = i;} cin>>m; for(int i = 0; i < m ; i ++){ cin>>ci>>e[i].w >>cj; e[i].from = mp[ci]; e[i].to = mp[cj]; } for(int i = 0; i < n; i++){ flag = find_loop(i); if(flag) break; } if(flag) cout<<"Case "<<cnt++<<": Yes"<<endl; else cout<<"Case "<<cnt++<<": No"<<endl; } return 0;}
0 0
- POJ 2240_Arbitrage
- POJ训练计划2240_Arbitrage(SPFA)
- POJ 2240
- POJ 2240
- poj-2240
- poj 2240
- poj 2240
- poj 2240
- poj 2240
- poj 2240
- poj-2240
- poj 2240
- POJ-2240
- poj 2240
- poj 2240
- POJ 2240
- poj 2240
- POJ 2240 Arbitrage
- android数据存储的四种方案(三)
- 面试笔试杂项积累-leetcode 156-160
- POJ 1182_食物链
- 面试笔试杂项积累-leetcode 161-165
- 深夜学算法之SkipList:让链表飞
- POJ 2240_Arbitrage
- utf8解决JSP中文乱码问题
- linux习惯每天一个命令之mount命令
- Hibernate细节
- 今天是春节
- Codeforces Round #342 (Div. 2)(A)贪心,数学
- UVA 11400_ Lighting System Design
- Codeforces Round #342 (Div. 2)(B)贪心
- 即使是新春