-
Notifications
You must be signed in to change notification settings - Fork 0
/
CF-653D.cpp
84 lines (77 loc) · 1.96 KB
/
CF-653D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 55;
long long cap[N][N], C[N][N];
int n, m, x;
bool vis[N];
int path[N], path_len;
long long getPath(int src, int target, int len, long long maxCapacity){
path[len] = src;
vis[src] = 1;
if(src == target){
path_len = len + 1;
return maxCapacity;
}
int ret = 0;
for(int i = 0; i < n; i++){
if(vis[i] || cap[src][i] <= 0)
continue;
ret = getPath(i, target, len + 1,
min(maxCapacity, cap[src][i]));
if(ret) break;
}
return ret;
}
int maxFlow(int src, int sink){
long long total_flow = 0;
while(1){
memset(vis, 0, sizeof vis);
long long new_flow = getPath(src, sink, 0, INT_MAX);
if(new_flow == 0) break;
for(int i = 1; i < path_len; i++){
int u = path[i - 1], v = path[i];
cap[u][v] -= new_flow;
cap[v][u] += new_flow;
}
total_flow += new_flow;
}
return total_flow;
}
double EPS = 1e-7;
bool ok(double X){
if(X == 0)
return 1;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cap[i][j] = (C[i][j]+EPS) / X;
}
}
return maxFlow(0, n - 1) >= x;
}
void solveTestCase(){
cin>>n>>m>>x;
for(int i = 0; i < m; i++){
int u, v, c;
cin>>u>>v>>c; --u, --v;
C[u][v] = c;
}
double L = 0, R = INT_MAX, ans;
for(int itr = 0; itr < 100; itr++){
double md = (L + R) * 0.5;
if(ok(md)){
ans = md;
L = md;
}else
R = md;
}
cout<<x * ans<<"\n";
}
int main(){
cin.tie(0);
cin.sync_with_stdio(0);
cout<<fixed<<setprecision(10);
int T = 1;
while(T--)
solveTestCase();
}