-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDownloadSpeed.java
More file actions
78 lines (77 loc) · 1.76 KB
/
DownloadSpeed.java
File metadata and controls
78 lines (77 loc) · 1.76 KB
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
import java.io.*;
import java.util.*;
public class DownloadSpeed{
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(System.out);
static StringTokenizer st = new StringTokenizer("");
static int N, M, K;
static int vE[], eNxt[], eTo[], eCap[], eCnt = 0, par[];
static void addE(int u, int v, int c){
eNxt[eCnt] = vE[u];
eTo[eCnt] = v;
eCap[eCnt] = c;
vE[u] = eCnt++;
}
static int bfs(int k){
Queue<Integer> q = new ArrayDeque<>();
Arrays.fill(par, -1);
par[0] = 0;
q.add(0);
while(!q.isEmpty()){
int v = q.poll();
for(int i = vE[v]; i != -1; i = eNxt[i]){
if(par[eTo[i]] == -1 && eCap[i]>>k<<k > 0){
q.add(eTo[i]);
par[eTo[i]] = i;
}
}
}
if(par[N-1] == -1) return 0;
int z = N-1;
int flow = 1000000000;
while(z != 0){
flow = Math.min(flow, eCap[par[z]]);
z = eTo[par[z]^1];
}
z = N-1;
while(z != 0){
eCap[par[z]] -= flow;
eCap[par[z]^1] += flow;
z = eTo[par[z]^1];
}
return flow;
}
public static void main(String[] args) throws IOException{
N = nextInt(); M = nextInt();
vE = new int[N];
Arrays.fill(vE, -1);
eNxt = new int[2*M];
eTo = new int[2*M];
eCap = new int[2*M];
par = new int[N];
for(int i = 0; i < M; i++){
int u = nextInt(), v = nextInt(), c = nextInt();
u--; v--;
addE(u, v, c);
addE(v, u, 0);
}
long flow = 0;
for(int k = 30; k >= 0; k--){
int kflow;
while((kflow = bfs(k)) > 0){
flow += kflow;
}
}
pw.println(flow);
br.close(); pw.close();
}
static String next() throws IOException{
while(!st.hasMoreTokens()){
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
static int nextInt() throws IOException{
return Integer.parseInt(next());
}
}