#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1023;
const double eps = 1e-3;
typedef long long LL;
struct node {
int v, next;
double w;
int t;
node() {
}
node(int v, int next, double w, int t) :
v(v), next(next), w(w), t(t) {
}
} edge[maxn << 3];
int head[maxn], e;
double f[maxn];
void add_edge(int u, int v, int w) {
edge[e] = node(v, head[u], 0, w);
head[u] = e++;
}
bool inq[maxn];
int cnt[maxn];
double dis[maxn];
int q[maxn], front, rear;
bool spfa(int s, int n) {
int i, u, v;
for (i = 1; i <= n; ++i)
dis[i] = -1e10, cnt[i] = 0;
dis[1] = 0;
q[rear++] = s;
cnt[s]++;
while (front != rear) {
u = q[front++];
front &= maxn;
inq[u] = false;
for (i = head[u]; ~i; i = edge[i].next) {
v = edge[i].v;
if (dis[u] + edge[i].w > dis[v]) {
dis[v] = dis[u] + edge[i].w;
if (!inq[v]) {
inq[v] = true;
q[rear++] = v;
rear &= maxn;
if ((++cnt[v]) >= n)
return true;
}
}
}
}
return false;
}
int main() {
int n, m, u, v, i, w;
scanf("%d%d", &n, &m);
double ma;
for (i = 1; i <= n; ++i) {
scanf("%lf", f + i);
head[i] = -1;
ma = max(ma, f[i]);
}
e = 0;
for (i = 0; i < m; ++i) {
scanf("%d%d%d", &u, &v, &w);
add_edge(u, v, w);
}
double l = 0, r = ma, mid;
while (r - l > eps) {
mid = (l + r) * 0.5;
for (i = 0; i < m; ++i) {
edge[i].w = f[edge[i].v] - mid * edge[i].t;
}
if (spfa(1, n))
l = mid;
else
r = mid;
}
printf("%.2f\n", mid);
return 0;
}
POJ 3621
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.