同 AcWing 361 观光奶牛
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using p = pair<int, int>;
const double pi(acos(-1));
const int inf(0x3f3f3f3f);
const int mod(1e9 + 7);
const int maxn(1e3 + 10);
const int maxm(5e3 + 10);
bool vis[maxn];
int ecnt, f[maxn], head[maxn], cnt[maxn];
double dis[maxn];
struct edge {
int to, wt, nxt;
} edges[maxm];
template<typename T = int>
inline const T read()
{
T x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * f;
}
template<typename T>
inline void write(T x, bool ln)
{
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) write(x / 10, false);
putchar(x % 10 + '0');
if (ln) putchar(10);
}
void addEdge(int u, int v, int w)
{
edges[ecnt].to = v;
edges[ecnt].wt = w;
edges[ecnt].nxt = head[u];
head[u] = ecnt++;
}
bool spfa(int tot, double val)
{
queue<int> q;
for (int i = 1; i <= tot; ++i) {
q.push(i);
vis[i] = true;
dis[i] = 0;
cnt[i] = 0;
}
while (not q.empty()) {
int u = q.front();
q.pop();
vis[u] = false;
for (int i = head[u]; compl i; i = edges[i].nxt) {
int v = edges[i].to, w = edges[i].wt;
if (dis[v] < dis[u] + f[v] - val * w) {
dis[v] = dis[u] + f[v] - val * w;
if (not vis[v]) {
vis[v] = true;
q.push(v);
cnt[v] = cnt[u] + 1;
if (cnt[v] >= tot) {
return true;
}
}
}
}
}
return false;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
memset(head, -1, sizeof head);
int n = read(), m = read();
for (int i = 1; i <= n; ++i) {
f[i] = read();
}
while (m--) {
int u = read(), v = read(), w = read();
addEdge(u, v, w);
}
double low = 0, high = 1e3;
while (high - low >= 1e-4) {
double mid = (low + high) / 2;
if (spfa(n, mid)) {
low = mid;
} else {
high = mid;
}
}
printf("%.2lf\n", low);
return 0;
}