Pagini recente » Cod sursa (job #3266274) | Cod sursa (job #534230) | Cod sursa (job #906793) | Cod sursa (job #2319333) | Cod sursa (job #2710358)
import java.io.*;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.Scanner;
import java.util.Scanner; // Import the Scanner class to read text files
public class Main {
public static void main(String[] args) throws IOException {
File inputFile = new File("maxflow.in");
File outputFile = new File("maxflow.out");
FileInputStream inputStream = new FileInputStream(inputFile);
Scanner scanner = new Scanner(inputStream);
FileOutputStream outputStream = new FileOutputStream(outputFile);
PrintWriter writer = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve( scanner, writer);
writer.close();
outputStream.close();
}
}
class TaskC {
public void solve(Scanner in, PrintWriter out) {
int x , y , z , i , j ;
int n = in.nextInt();
int m = in.nextInt();
int[][] c = new int[n + 2][n + 2];
for ( i = 0 ; i < n ; ++ i ) {
for ( j = 0 ; j < n ; ++ j ) {
c [ i ][ j ] = 0 ;
}
}
for ( i = 0 ; i < m ; ++ i ) {
x = in.nextInt() ;
y = in.nextInt() ;
z = in.nextInt() ;
c [ x ][ y ] = z ;
}
int[][] f = new int[n + 2][n + 2];
out.print(flow(n, c, f, 1, n));
}
private int flow(int n, int[][] c, int[][] f, int s, int t) {
boolean[] mark = new boolean[n];
int res = 0;
while (dfs(s, t, c, f, mark)) {
++res;
Arrays.fill(mark, false);
}
return res;
}
private boolean dfs(int at, int t, int[][] c, int[][] f, boolean[] mark) {
if (at == t) return true;
if (mark[at]) return false;
mark[at] = true;
for (int i = 0; i < c.length; ++i) if (f[at][i] < c[at][i] && dfs(i, t, c, f, mark)) {
++f[at][i];
--f[i][at];
return true;
}
return false;
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}