Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import static java.lang.Math.min;
- import java.util.*;
- import java.io.*;
- public class C implements Runnable {
- public static void main(String args[]) {
- new Thread(new C()).start();
- }
- BufferedReader br;
- StringTokenizer st;
- PrintWriter out;
- private String FNAME = "cut";
- boolean eof = false;
- @Override
- public void run() {
- try {
- Locale.setDefault(Locale.US);
- br = new BufferedReader(new FileReader(FNAME + ".in"));
- out = new PrintWriter(FNAME + ".out");
- solve();
- br.close();
- out.close();
- } catch (Exception e) {
- e.printStackTrace();
- System.exit(566);
- }
- }
- String nextToken() {
- while (st == null || !st.hasMoreTokens()) {
- try {
- st = new StringTokenizer(br.readLine());
- } catch (IOException e) {
- eof = true;
- return "0";
- }
- }
- return st.nextToken();
- }
- int nextInt() {
- return Integer.parseInt(nextToken());
- }
- long nextLong() {
- return Long.parseLong(nextToken());
- }
- double nextDouble() {
- return Double.parseDouble(nextToken());
- }
- void My_Assert(boolean b, String s) {
- if (!b)
- throw new Error("Assertion " + s);
- }
- long[][] f, c;
- boolean[] was;
- int n;
- final long INF = (long) 1e12;
- private void solve() {
- n = nextInt();
- int m = nextInt();
- f = new long[n][n];
- c = new long[n][n];
- for (int i = 0; i < m; i++) {
- int a = nextInt();
- int b = nextInt();
- c[a - 1][b - 1] += nextLong();
- }
- was = new boolean[n];
- Queue<Integer> q;
- int[] p;
- // for (int it = 0; it < 2; it++) {
- while (true) {
- Arrays.fill(was, false);
- q = new LinkedList<Integer>();
- p = new int[n];
- q.add(0);
- was[0] = true;
- while (!q.isEmpty()) {
- int u = q.poll();
- for (int i = 0; i < n; i++)
- if (!was[i] && c[u][i] > f[u][i]) {
- was[i] = true;
- p[i] = u;
- q.add(i);
- }
- }
- if (!was[n - 1])
- break;
- long minc = INF;
- int t = n - 1;
- int u;
- while (t != 0) {
- u = p[t];
- minc = min(minc, c[u][t] - f[u][t]);
- t = u;
- }
- // System.err.println(minc);
- t = n - 1;
- while (t != 0) {
- f[p[t]][t] += minc;
- f[t][p[t]] -= minc;
- t = p[t];
- }
- }
- Arrays.fill(was, false);
- q = new LinkedList<Integer>();
- q.add(0);
- was[0] = true;
- while (!q.isEmpty()) {
- int u = q.poll();
- for (int i = 0; i < n; i++)
- if (!was[i] && c[u][i] > f[u][i]) {
- was[i] = true;
- q.add(i);
- }
- }
- int ans = 0;
- for (int i = 0; i < n; i++)
- if (was[i])
- ans++;
- out.println(ans);
- for (int i = 0; i < n; i++)
- if (was[i])
- out.print(i + 1 + " ");
- }
- }
Add Comment
Please, Sign In to add comment