POJ-1860-Currency Exchange(bellman)
来源:互联网 发布:家用网络机顶盒哪个好 编辑:程序博客网 时间:2024/06/08 18:28
Memory Limit: 30000KTotal Submissions: 28989
Accepted: 10834
Description
For example, if you want to exchange 100 US Dollars into Russian Rubles at the exchange point, where the exchange rate is 29.75, and the commission is 0.39 you will get (100 - 0.39) * 29.75 = 2963.3975RUR.
You surely know that there are N different currencies you can deal with in our city. Let us assign unique integer number from 1 to N to each currency. Then each exchange point can be described with 6 numbers: integer A and B - numbers of currencies it exchanges, and real RAB, CAB, RBA and CBA - exchange rates and commissions when exchanging A to B and B to A respectively.
Nick has some money in currency S and wonders if he can somehow, after some exchange operations, increase his capital. Of course, he wants to have his money in currency S in the end. Help him to answer this difficult question. Nick must always have non-negative sum of money while making his operations.
Input
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10-2<=rate<=102, 0<=commission<=102.
Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this sequence. You may assume that ratio of the numeric values of the sums at the end and at the beginning of any simple sequence of the exchange operations will be less than 104.
Output
Sample Input
3 2 1 20.01 2 1.00 1.00 1.00 1.002 3 1.10 1.00 1.10 1.00
Sample Output
YES
解题思路:这道题可以用bellman算法,不过要稍微变通一下,把检查有无负环改成检测有无正环,而且只要检测出来就行了,
不用求具体的路径
#include <cstring>
#include <cstdio>
const int MAXN = 105;
const int INF = 0x3f3f3f3f;
struct Edge{
int from;
int to;
double rab, cab;
};
Edge edge[MAXN * MAXN];
int edgeNum;
int m, n, s;
double v;
bool isYes()
{
double dis[MAXN];
memset(dis, 0, sizeof(dis));//检测正环,所以初始化为0
dis[s] = v;
bool isUp = false;
for (int i = 0; i < edgeNum + 1; i++) {
isUp = false;
for (int j = 0; j < edgeNum; j++) {
if (dis[edge[j].to] < (dis[edge[j].from] - edge[j].cab) * edge[j].rab) {
dis[edge[j].to] = (dis[edge[j].from] - edge[j].cab) * edge[j].rab;
isUp = true;
if (i == edgeNum) { //如果无正环,i最多循环edgeNum次,也就是遍历一次所有的边
return true;
}
}
}
if (!isUp) {
return false;
}
}
}
int main()
{
scanf("%d%d%d%lf", &n, &m, &s, &v);
edgeNum = 0;
int fromS, toS;
double rabS, cabS, rbaS, cbaS;
for (int i = 0; i < m; i++) {
scanf("%d%d%lf%lf%lf%lf", &fromS, &toS, &rabS, &cabS, &rbaS, &cbaS);
edge[edgeNum].from = fromS;
edge[edgeNum].to = toS;
edge[edgeNum].rab = rabS;
edge[edgeNum].cab = cabS;
edgeNum++;
edge[edgeNum].from = toS;
edge[edgeNum].to = fromS;
edge[edgeNum].rab = rbaS;
edge[edgeNum].cab = cbaS;
edgeNum++;
}
if (isYes()) {
printf("YES\n");
}
else {
printf("NO\n");
}
return 0;
}
- POJ 1860 Currency Exchange Bellman
- poj 1860 Currency Exchange (bellman)
- poj-1860-Currency Exchange-Bellman
- poj 1860 Currency Exchange bellman
- POJ-1860-Currency Exchange(bellman)
- POJ 1860 Currency Exchange Bellman-Ford
- POJ-1860-Currency Exchange-Bellman-ford
- poj 1860 Currency Exchange :bellman-ford
- POJ 1860 Currency Exchange(Bellman-Ford)
- POJ 1860 Currency Exchange(Bellman-Ford)
- poj 1860 Currency Exchange(bellman-ford)
- Bellman-Ford-POJ-1860-Currency Exchange
- POJ 1860 Currency Exchange(Bellman-Ford)
- poj 1860 Currency Exchange 【bellman-ford】
- POJ 1860 Currency Exchange(bellman变形)
- poj 1860 Currency Exchange (Bellman- Ford)
- POJ-1860 Currency Exchange(Bellman-Ford)
- POJ 1860 Currency Exchange bellman求正权路
- POJ-3259-Wormholes
- 线段树
- 起飞速度V1/VR/V2的概念
- 连连跳(简单bfs)
- VOR导航原理
- POJ-1860-Currency Exchange(bellman)
- POJ-2253-Frogger
- 学习资料|王江源博客推荐,21篇优…
- POJ-1125-Stockbroker Grapevine
- POJ-1258-Agri-Net
- 卷积神经网络基础总结
- POJ-2485-Highways(kruskal)
- Globle.asax错误:异常详细信息: S…
- HDU-1166-敌兵布阵 (线段树)