用c++解决Input The first line contains four numbers: N - the number of currencies, M - the number of exchange points, S - the number of currency Nick has and V - the quantity of currency units he has. The following M lines contain 6 numbers each - the description of the corresponding exchange point - in specified above order. Numbers are separated by one or more spaces. 1 ≤ S ≤ N ≤ 100, 1 ≤ M ≤ 100, V is real number, 0 ≤ V ≤ 103. 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 If Nick can increase his wealth, output YES, in other case output NO.
时间: 2024-02-14 07:07:47 浏览: 33
这个问题涉及到一个货币兑换的问题,需要我们输入一些数据,然后判断是否能够增加货币的数量。
下面是用 C++ 解决这个问题的代码:
```c++
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = 100 + 10;
const double INF = 1e9;
int n, m, S;
double V, d[MAXN][MAXN];
struct Exchange {
int u, v;
double r, c;
} e[MAXN];
void floyd() {
for (int k = 1; k <= m; k++) {
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= m; j++) {
if (d[i][k] * e[k].r - e[k].c > 0) {
d[i][j] = max(d[i][j], d[i][k] * e[k].r - e[k].c);
}
}
}
}
}
int main() {
cin >> n >> m >> S >> V;
for (int i = 1; i <= m; i++) {
cin >> e[i].u >> e[i].v >> e[i].r >> e[i].c;
}
memset(d, 0, sizeof(d));
for (int i = 1; i <= m; i++) {
d[e[i].u][e[i].v] = V * e[i].r - e[i].c;
}
floyd();
bool flag = false;
for (int i = 1; i <= n; i++) {
if (d[S][i] > V) {
flag = true;
break;
}
}
if (flag) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
return 0;
}
```
具体思路如下:
1. 读入数据,包括货币的数量 $n$,兑换点的数量 $m$,货币 Nick 拥有的编号 $S$ 和数量 $V$,以及每个兑换点的描述。
2. 初始化一个邻接矩阵 $d$,表示每个货币对其他货币的兑换情况。初始值为 0,表示不需要兑换。
3. 对于每个兑换点 $k$,更新邻接矩阵 $d$,其中 $d_{i,j}$ 表示从货币 $i$ 兑换到货币 $j$ 的最大收益。如果从 $i$ 到 $j$ 经过 $k$ 的收益大于 0,那么更新 $d_{i,j}$。
4. 使用 Floyd 算法更新所有货币对之间的最大收益。
5. 判断是否存在一种兑换方案,使得从货币 $S$ 开始到其他货币的收益均大于 $V$。如果存在,则输出 YES,否则输出 NO。
注意,在更新 $d$ 的时候,$d_{i,j}$ 的初始值应该为 $V \times e_k.r - e_k.c$,表示从货币 $i$ 到货币 $j$ 经过兑换点 $k$ 的兑换后的收益。如果 $d_{i,j}$ 在更新后仍然为 0,表示从货币 $i$ 到货币 $j$ 没有找到任何一种兑换方式,即无法从货币 $i$ 兑换到货币 $j$。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)