# 题解

$Minimize\ \lambda = \frac{\sum W_{i, i+1}}{k}$

$\lambda * k = \sum W_{i, i+1}$

$g(\lambda) = Min(\lambda * k - \sum W_{i, i+1})$

# 代码

#include <bits/stdc++.h>
using namespace std;
const int maxn = 3005 * 2;
#define exp 1e-10
const double inf = 1000000000;
int n, m;
struct edge {
int to;
double value;
};
vector<edge> G[maxn];
vector<edge> rg[maxn];
int vis[maxn];
int flag;
double dist[maxn];
inline void spfa(int x) {
int i;
vis[x] = false;
for (i = 0; i < rg[x].size(); i++) {
edge &e = rg[x][i];
if (dist[e.to] > dist[x] + e.value)
if (!vis[e.to]) {
flag = true;
break;
} else {
dist[e.to] = dist[x] + e.value;
spfa(e.to);
}
}
vis[x] = true;
}
bool check(double lambda) {
for (int i = 1; i <= n; i++) {
rg[i].clear();
for (int j = 0; j < G[i].size(); j++) {
rg[i].push_back((edge){G[i][j].to, (double)G[i][j].value - lambda});
}
}
memset(vis, 1, sizeof(vis));
memset(dist, 0, sizeof(dist));
flag = false;
for (int i = 1; i <= n; i++) {
spfa(i);
if (flag)
return true;
}
return false;
}
int main() {
// freopen("input", "r", stdin);
scanf("%d %d", &n, &m);
int tot = 0;
for (int i = 1; i <= m; i++) {
int x, y;
double z;
scanf("%d %d %lf", &x, &y, &z);
G[x].push_back((edge){y, z});
tot += z;
}
double l = -inf, r = inf;
while (l + exp < r) {
double mid = (l + r) / 2;
if (check(mid))
r = mid;
else
l = mid;
}
printf("%.8f", l);
}

posted on 2017-02-22 09:32 蒟蒻konjac 阅读(...) 评论(...) 编辑 收藏

• 随笔 - 172
• 文章 - 0
• 评论 - 30