You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.8 KiB

2 years ago
#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;
}