POJ 2240 Arbitrage
来源:互联网 发布:计算机专业不想编程 编辑:程序博客网 时间:2024/06/11 16:50
POJ 2240 Arbitrage
[★★☆☆☆]图论 最短路
题目大意:
可以简单描述为知道从i到j的汇率,问能不能赚钱
样例
输入:
USDollar
BritishPound
FrenchFranc
3
USDollar 0.5 BritishPound
BritishPound 10.0 FrenchFranc
FrenchFranc 0.21 USDollar3
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 USDollar0
输出:
Case 1: Yes
Case 2: No解题思路:
水题,本来能1A的,结果交上去超时。
看了Discuss是编译器的问题,我本来是G++换成C++就A了。
我用的Bellman算法,Floyd也行。代码
#include <iostream>#include <algorithm>#include <string>using namespace std;struct edge { int from, to; double r;};edge E[1000];int cte;bool used[35];string S[35];double d[35];int V;int nS(string s) { for (int i = 1; i <= V; i++) { if (s == S[i]) return i; } return 0;}void adde(int f, int t, double r) { edge te = {f, t, r}; E[cte++] = te;}int usedall() { for (int i = 1; i <= V; i++) { if (d[i] == 0) return i; } return 0;}bool bellman() { int u; for (int i = 1; i <= V; i++) d[i] = 0; while ((u = usedall())) { d[u] = 1; int ct; for (ct = 1; ct <= V; ct++) { bool update = false; for (int i = 0; i < cte; i++) { edge e = E[i]; if (d[e.from] != 0 && d[e.to] < d[e.from] * e.r) { d[e.to] = d[e.from] * e.r; update = true; } } if (!update) break; } if (ct == V+1) return 1; } return 0;}int main() { int cs = 1; while ((cin >> V) && V != 0) { cte = 0; for (int i = 1; i <= V; i++) { cin >> S[i]; } int n; string s1, s2; double r; cin >> n; for (int i = 1; i <= n; i++) { cin >> s1 >> r >> s2; adde(nS(s1), nS(s2), r); } cout << "Case " << cs++; if (bellman()) cout << ": Yes" << endl; else cout << ": No" << endl; } return 0;}
0 0
- POJ 2240 Arbitrage
- poj 2240 Arbitrage
- POJ 2240 Arbitrage (Floyd)
- poj 2240Arbitrage(Floyd)
- POJ 2240 Arbitrage Floyd
- Poj 2240 Arbitrage
- POJ 2240 Arbitrage
- poj 2240 Arbitrage
- POJ 2240 Arbitrage (spfa)
- POJ 2240 Arbitrage
- POJ 2240 Arbitrage
- Poj 2240 Arbitrage
- POJ 2240 Arbitrage
- POJ 2240 Arbitrage
- POJ 2240 Arbitrage
- POJ 2240 Arbitrage
- POJ-2240-Arbitrage
- poj 2240 Arbitrage
- /sys/i386/include/pmap.h中与页表相关宏值的含义
- HTTPS跨域访问时,证书导致的问题
- Rails ActiveRecord的default_scope的坑
- python(一)变量,常量,编码,数据类型
- Shell 简易教程
- POJ 2240 Arbitrage
- 安卓_侧滑
- 1013 Problem M
- sqlalchemy 使用不同文件中的数据模型
- python(二)列表,元组,循环
- 爬虫技术入门(一)
- ICS 家庭作业2.65
- 222. Count Complete Tree Nodes
- 【BZOJ4319】cerc2008 Suffix reconstruction【构造】【后缀数组】