题目链接
我们二分每个人携带的数量, 然后每个边的容量就相当于min(权值/二分的值, x). x是人的数量。
然后判断是否满流就可以。
这么裸的网络流为竟然没看出来。
注意写fsbs(r-l)>eps会挂掉...
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 1e5;
int q[maxn*2], head[maxn*2], dis[maxn/10], s, t, num;
struct node
{
int to, nextt, c;
node(){}
node(int to, int nextt, int c):to(to), nextt(nextt), c(c){}
}e[maxn*2];
void init() {
num = 0;
mem1(head);
}
void add(int u, int v, int c) {
e[num] = node(v, head[u], c); head[u] = num++;
e[num] = node(u, head[v], 0); head[v] = num++;
}
int bfs() {
mem(dis);
dis[s] = 1;
int st = 0, ed = 0;
q[ed++] = s;
while(st<ed) {
int u = q[st++];
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(!dis[v]&&e[i].c) {
dis[v] = dis[u]+1;
if(v == t)
return 1;
q[ed++] = v;
}
}
}
return 0;
}
int dfs(int u, int limit) {
if(u == t) {
return limit;
}
int cost = 0;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(e[i].c&&dis[v] == dis[u]+1) {
int tmp = dfs(v, min(limit-cost, e[i].c));
if(tmp>0) {
e[i].c -= tmp;
e[i^1].c += tmp;
cost += tmp;
if(cost == limit)
break;
} else {
dis[v] = -1;
}
}
}
return cost;
}
int dinic() {
int ans = 0;
while(bfs()) {
ans += dfs(s, inf);
}
return ans;
}
struct edge
{
int u, v, w;
}ed[maxn];
int main()
{
int n, m, x;
int maxx = 0;
cin>>n>>m>>x;
for(int i = 0; i<m; i++) {
scanf("%d%d%d", &ed[i].u, &ed[i].v, &ed[i].w);
maxx = max(maxx, ed[i].w);
}
double l = 0, r = maxx;
int j = 0;
while(j<100) {
double mid = (l+r)/2;
s = 1, t = n;
init();
for(int i = 0; i<m; i++) {
add(ed[i].u, ed[i].v, int(min(ed[i].w/mid, x*1.0+1e-3)));
}
if(dinic() >= x) {
l = mid;
} else {
r = mid;
}
j++;
}
printf("%.8f
", l*x);
return 0;
}