题目链接
题目分析
该题讲在这个小城里有多种货币,并且已知每种货币之间的汇率,现在给你某种货币s和一定量的基金v,现在让你开动脑筋能否通过某种操作让你的本金比之前更多。经过分析,你可以将其看作一个图,货币是结点,边是汇率。仔细想一下就是找到一个正权环路(不一定要经过s), 即使经过这个环路一次,但是可以让你的本金增加一点点就可以了,因为你可以经过很多次的交换让你的资本变到无穷大. 因为n只有100所以用floyd或者spfa都可以
floyd
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
int n, m, s; double v;
const int maxx = 105;
double g1[maxx][maxx], g2[maxx][maxx];
double res[maxx];
bool floyd() {
double tmp[maxx];
for (int i = 1; i <= n; ++i)
tmp[i] = res[i];
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j =1; j <= n; ++j)
res[j] = max(res[j], (res[i] - g2[i][j]) * g1[i][j]);
for (int i = 1; i <= n; ++i)
if (tmp[i] < res[i])
return true;
return false;
}
int main() {
scanf("%d%d%d%lf", &n, &m, &s, &v);
for (int i = 0; i < m; ++i) {
int u, v;
double r12, r21, r13, r31;
scanf("%d%d%lf%lf%lf%lf", &u, &v, &r12, &r21, &r13, &r31);
g1[u][v] = r12; g1[v][u] = r13;
g2[u][v] = r21; g2[v][u] = r31;
}
res[s] = v;
floyd(); //先来一次floyd得到对每个点(每种货币)的最大本金
printf("%s\n", floyd() ? "YES" : "NO"); //再来一次floyd,如果有某个点的本金大于上一次的本金说明存在正权回路
return 0;
}
spfa
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxx = 105;
bool vis[maxx];
int head[maxx], cnt[maxx];
float dis[maxx];
int ct;
struct edge //邻接表存储图
{
int to, nxt;
float w1, w2;
void set(float _w1, float _w2) {
w1 = _w1; w2 = _w2;
}
}e[maxx<<2];
void init(int n) {
for (int i = 1; i <= n; ++i)
head[i] = -1;
}
void add_edge(int u, int v, float r1, float r2) {
++ct;
e[ct].to = v; e[ct].nxt = head[u]; e[ct].set(r1, r2);
head[u] = ct;
}
bool spfa(int s, int n) {
queue<int> q;
q.push(s);
float tmp;
while (!q.empty()) {
s = q.front(); q.pop();
vis[s] = false;
for (int i = head[s]; i != -1; i = e[i].nxt) {
int nxt = e[i].to;
if (!vis[nxt] && (tmp = (dis[s] - e[i].w2) * e[i].w1) > dis[nxt]) {
dis[nxt] = tmp;
++cnt[nxt];
vis[nxt] = true;
q.push(nxt);
if (cnt[nxt] >= n) return true;
}
}
}
return false;
}
int main() {
int n, m, s; float v;
scanf("%d%d%d%f", &n, &m, &s, &v);
init(n);
for (int i = 0; i < m; ++i) {
int from, to;
float r12, r13, r21, r31;
scanf("%d%d%f%f%f%f", &from, &to, &r12, &r13, &r21, &r31);
add_edge(from, to, r12, r13);
add_edge(to, from, r21, r31);
}
dis[s] = v;
printf("%s\n", spfa(s, n) ? "YES" : "NO");
return 0;
}