Files
python/TangDou/AcWing/MinusCircle/361_FuHuan.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

71 lines
1.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
const int N = 1010, M = 5010;
int n, m;
int f[N], cnt[N];
double dist[N];
bool st[N];
// 邻接表
int idx, h[N], e[M], w[M], ne[M];
void add(int a, int b, int c) {
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
bool check(double mid) {
queue<int> q;
memset(cnt, 0, sizeof cnt);
memset(dist, 0x3f, sizeof dist);
memset(st, false, sizeof st);
for (int i = 1; i <= n; i++) {
q.push(i);
st[i] = 1;
}
while (q.size()) {
int u = q.front();
q.pop();
st[u] = 0;
for (int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
// 最短路
if (dist[v] > dist[u] + w[i] * mid - f[u]) {
dist[v] = dist[u] + w[i] * mid - f[u];
// 判负环
cnt[v] = cnt[u] + 1;
if (cnt[v] >= n) return 1;
if (!st[v]) {
q.push(v);
st[v] = 1;
}
}
}
}
return 0;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &f[i]); // 每个点都有一个权值f[i]
// 初始化邻接表
memset(h, -1, sizeof h);
int a, b, c;
while (m--) {
scanf("%d %d %d", &a, &b, &c);
add(a, b, c);
}
// 浮点数二分
double l = 0, r = INF;
while (r - l > eps) {
double mid = (l + r) / 2;
if (check(mid))
l = mid; // 存在负环时,mid再大一点,最终取得01分数规则的最大值
else
r = mid; // 不存在负环时,mid再小一点
}
printf("%.2lf\n", l);
return 0;
}