public static void main(String[] args) throws FileNotFoundException {
   Scanner sc = new Scanner(new File("J.in"));
   for (int z = 1; ; z++) {
     int R = sc.nextInt();
     int T = sc.nextInt();
     if (R == 0 && T == 0) break;
     int[][] flow = new int[R + 3][R + 3];
     int s = flow.length - 2;
     int e = flow.length - 1;
     for (int i = 0; i < T; i++) {
       int a = sc.nextInt();
       int b = sc.nextInt();
       flow[a][b]++;
       flow[b][a]++;
     }
     flow[0][e] = 100000000;
     int[] cost = new int[R + 1];
     for (int i = 0; i < cost.length; i++) {
       if (i == 0) cost[0] = Integer.MAX_VALUE;
       else {
         int[][] fl = new int[R + 3][R + 3];
         for (int j = 0; j < fl.length; j++)
           for (int k = 0; k < fl.length; k++) fl[j][k] = flow[j][k];
         fl[s][i] = 100000000;
         cost[i] = maxflow(s, e, fl);
       }
     }
     // System.out.println(Arrays.toString(cost));
     int f = dijkstra(1, 0, cost, flow);
     System.out.println("Case " + z + ": " + f + "\n");
   }
 }
Exemple #2
0
  public void solve() throws Exception {
    int n = in.nextInt();
    int[] a = new int[n];
    int sum = 0;

    for (int i = 0; i < n; i++) {
      a[i] = in.nextInt();
      sum += a[i];
    }

    sum /= 2;

    boolean[] dp = new boolean[sum + 1];
    dp[0] = true;

    for (int tmp : a) {
      for (int i = sum; i >= tmp; i--) {
        if (dp[i - tmp]) {
          dp[i] = true;
        }
      }
    }

    String ans = dp[sum] ? "YES" : "NO";
    out.println(ans);
  }
Exemple #3
0
 /*setup the game */
 private void setup() {
   t0 = System.currentTimeMillis(); /*Times the beginning of the game */
   // int size;
   Scanner in = new Scanner(System.in);
   // System.out.println("Enter the size of available");
   // size = in.nextInt();
   initiateScores();
   placeWalls();
   placeStates();
   placeLabels();
   randomizeOrderRent(); /*randomizes the order of the equipment for the On Rent state */
   System.out.println(
       "How many High Runners at price $" + EQUIPMENTCOSTS[0] + " do you want to buy?");
   int type1Equip = in.nextInt();
   System.out.println(
       "How many Medium Runners at price $" + EQUIPMENTCOSTS[1] + " do you want to buy?");
   int type2Equip = in.nextInt();
   System.out.println(
       "How many Low Runners at price $" + EQUIPMENTCOSTS[2] + " do you want to buy?");
   int type3Equip = in.nextInt();
   capitalInvested =
       EQUIPMENTCOSTS[0] * type1Equip
           + EQUIPMENTCOSTS[1] * type2Equip
           + EQUIPMENTCOSTS[2] * type3Equip;
   capitalLabel.setLabel("Capital Invested: $" + capitalInvested);
   fillAvailable(type1Equip, type2Equip, type3Equip); // fills with the proper number of equipment
   // fillStates(size,INITRENT,INITSHOP);
   placeEquipments();
 }
Exemple #4
0
  void run() {
    Scanner sc = new Scanner(System.in);
    int oo = sc.nextInt();
    for (int o = 1; o <= oo; o++) {
      que = new PriorityQueue<E>();
      int n = sc.nextInt();
      tank = new Tank();
      count = 0;
      int[] h = new int[n + 3], b = new int[n + 3];
      h[0] = 100;
      b[0] = 0;
      h[n + 1] = 50;
      b[n + 1] = 100;
      h[n + 2] = 50;
      b[n + 2] = INF;
      for (int i = 1; i <= n; i++) {
        b[i] = sc.nextInt();
        h[i] = sc.nextInt();
      }
      n += 3;
      for (int i = 1; i < n; i++) {
        tank.bs.add(new Box(b[i - 1], b[i], h[i - 1], h[i], 0, 0));
      }
      int m = sc.nextInt();
      for (int i = 0; i < m; i++) {
        int f = sc.nextInt();
        double a = sc.nextDouble() / 30;
        for (Box box : tank.bs) {
          if (box.b1 < f && f < box.b2) box.f += a;
        }
      }
      debug(tank.bs);

      int l = sc.nextInt();
      res = new double[l];
      for (int i = 0; i < l; i++) {
        que.offer(new W(sc.nextInt(), sc.nextDouble(), i));
      }
      que.offer(tank.nextEvent());
      debug(que.size());
      while (!que.isEmpty()) {
        if (res[0] < 0) System.exit(1);
        que.poll().go();
        debug(que.peek().time());
        debug(tank.bs);
        debug(res);
        debug();
        if (count == l) break;
      }
      for (double r : res) {
        System.out.println(r);
      }
    }
  }
Exemple #5
0
  public void solve() {
    while (input.hasNext()) {
      String s = input.nextLine();
      s = s.replaceAll("[0\\.\\)]", "");
      String[] t = s.split("\\(");
      long a = Long.valueOf(t[0]);
      long b = Long.valueOf(t[1]);

      long x = a * ((long) pow(10, t[1].length()) - 1) + b;
      long y = ((long) pow(10, t[1].length()) - 1) * (long) pow(10, t[0].length());
      System.out.println(x);
      System.out.println(y);
      System.out.printf((x / gcd(x, y)) + "/" + (y / gcd(x, y)));
    }
  }
Exemple #6
0
 public int[] readIntArray(int n) {
   int[] ret = new int[n];
   for (int i = 0; i < n; i++) {
     ret[i] = sc.nextInt();
   }
   return ret;
 }
Exemple #7
0
  public void solve() throws Exception {
    int x = sc.nextInt();
    int y = sc.nextInt();
    StringBuilder sb = new StringBuilder();
    String p = null;
    String m = null;
    if (x >= 0) {
      p = "E";
      m = "W";
    } else {
      p = "W";
      m = "E";
    }
    x = abs(x);

    for (int i = 0; i < x; i++) {
      sb.append(m + p);
    }
    if (y >= 0) {
      p = "N";
      m = "S";
    } else {
      p = "S";
      m = "N";
    }
    y = abs(y);
    for (int i = 0; i < y; i++) {
      sb.append(m + p);
    }
    out.println(sb);
  }
Exemple #8
0
  public static void main(String[] args) throws Exception {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    boolean[] seen = new boolean[n];
    cycle = new boolean[n];
    f = new int[n];
    for (int i = 0; i < n; i++) {
      int num = in.nextInt() - 1;
      seen[num] = true;
      f[i] = num;
    }

    boolean[] min_S = new boolean[n];
    int min = 0;
    for (int i = 0; i < n; i++)
      if (!seen[i]) {
        min++;
        int x = i;
        while (!min_S[x]) {
          min_S[x] = true;
          x = f[x];
        }
      }
    for (int i = 0; i < n; i++) {
      if (!min_S[i]) {
        min++;
        int x = i;
        while (!min_S[x]) {
          min_S[x] = true;
          x = f[x];
        }
      }
    }

    S = new boolean[n];
    for (int i = 0; i < n; i++) {
      if (!S[i]) dfs(i);
    }

    int max = cycle_cnt;
    for (int i = 0; i < n; i++) if (!cycle[i]) max++;

    System.out.println(min + " " + max);
  }
Exemple #9
0
 public void run() {
   // Scanner sc = new Scanner(System.in);
   Scanner sc = new Scanner();
   StringBuilder sb = new StringBuilder(1000000);
   // System.setOut(new PrintStream(new BufferedOutputStream(System.out)));
   final int N = sc.nextInt();
   final int M = sc.nextInt();
   final int K = (int) floor(sqrt(N));
   final int[] A = new int[N + 1];
   A[0] = 1;
   for (int i = 1; i <= N; i++) A[i] = sc.nextInt();
   final int[] next = new int[N + 1];
   final int[] step = new int[N + 1];
   final int[] last = new int[N + 1];
   for (int i = N; i > 0; i--) {
     int j = i + A[i];
     if (j > N || j / K > i / K) {
       last[i] = i;
       step[i] = 1;
       next[i] = j;
     } else {
       last[i] = last[j];
       step[i] = step[j] + 1;
       next[i] = next[j];
     }
   }
   for (int t = 0; t < M; t++)
     if (sc.nextInt() == 1) {
       int i = sc.nextInt();
       int j = 0;
       int k = 0;
       while (i <= N) {
         j += step[i];
         k = last[i];
         i = next[i];
       }
       sb.append(k).append(' ').append(j).append('\n');
       // System.out.println(k + " " + j);
     } else {
       int k = sc.nextInt();
       int b = k / K * K;
       A[k] = sc.nextInt();
       for (int i = min(b + K - 1, N); i >= b; i--) {
         int j = i + A[i];
         if (j > N || j / K > i / K) {
           last[i] = i;
           step[i] = 1;
           next[i] = j;
         } else {
           last[i] = last[j];
           step[i] = step[j] + 1;
           next[i] = next[j];
         }
       }
     }
   // System.out.flush();
   System.out.print(sb);
 }
Exemple #10
0
    protected String solve(Scanner sc) {
      int N = sc.nextInt();
      int K = sc.nextInt();
      char[][] board = new char[N][];
      for (int i = 0; i < N; i++) board[i] = sc.next().toCharArray();
      char[][] rot = new char[N][N];
      for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) rot[i][j] = board[N - j - 1][i];
      char[][] grav = new char[N][N];
      for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) grav[i][j] = '.';
      for (int i = 0; i < N; i++)
        for (int j = N - 1, c = 0; j >= 0; j--)
          if (rot[j][i] == '.') c++;
          else grav[j + c][i] = rot[j][i];
      //			for (char[] cs : grav)
      //				debug(new String(cs));
      int[][][] R = new int[N][N][4];
      int[][][] B = new int[N][N][4];
      for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
          switch (grav[i][j]) {
            case 'B':
              {
                for (int d = 0; d < 4; d++)
                  B[i][j][d] = (in(i + di[d], j + dj[d], N) ? B[i + di[d]][j + dj[d]][d] : 0) + 1;
                break;
              }
            case 'R':
              {
                for (int d = 0; d < 4; d++)
                  R[i][j][d] = (in(i + di[d], j + dj[d], N) ? R[i + di[d]][j + dj[d]][d] : 0) + 1;
                break;
              }
          }
      boolean Rwin = false, Bwin = false;
      for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
          for (int d = 0; d < 4; d++) {
            if (R[i][j][d] >= K) Rwin = true;
            if (B[i][j][d] >= K) Bwin = true;
          }

      return Rwin ? Bwin ? "Both" : "Red" : Bwin ? "Blue" : "Neither";
    }
Exemple #11
0
 void run() {
   System.out.println("INTERSECTING LINES OUTPUT");
   for (N = input.nextInt(); N != 0; --N) {
     P p[] = new P[4];
     for (int i = 0; i < 4; ++i) {
       double x = input.nextDouble();
       double y = input.nextDouble();
       p[i] = new P(x, y);
     }
     P ans = isLL(p[0], p[1], p[2], p[3]);
     if (ans != null) {
       System.out.printf("POINT %.2f %.2f", ans.x, ans.y);
       System.out.println();
     } else {
       System.out.println(oneLine(p) ? "LINE" : "NONE");
     }
   }
   System.out.println("END OF OUTPUT");
 }
Exemple #12
0
  public void solve() {
    int t = input.nextInt();
    init();
    while (t-- > 0) {
      int n = input.nextInt();
      int ans = 0;
      int b[] = new int[n];
      Arrays.fill(b, 0);
      for (int i = 2; i <= n; i++) {
        if (a[i] == 1 && n % a[i] == 0) {
          int t = i;
          while (t <= n) {
            b[t] = 1;
            t += i;
          }
        } else ans++;
      }

      System.out.println(ans);
    }
  }
Exemple #13
0
 void run() {
   Scanner sc = new Scanner(System.in);
   int gx = sc.nextInt();
   int gy = sc.nextInt();
   int p = sc.nextInt();
   boolean[][][] mat = new boolean[2][gx + 1][gy + 1];
   for (int i = 0; i < p; i++) {
     int x1 = sc.nextInt();
     int y1 = sc.nextInt();
     int x2 = sc.nextInt();
     int y2 = sc.nextInt();
     mat[x1 == x2 ? 1 : 0][min(x1, x2)][min(y1, y2)] = true;
   }
   int[][] dp = new int[gx + 1][gy + 1];
   dp[0][0] = 1;
   for (int x = 0; x <= gx; x++)
     for (int y = 0; y <= gy; y++) {
       if (x > 0 && !mat[0][x - 1][y]) dp[x][y] += dp[x - 1][y];
       if (y > 0 && !mat[1][x][y - 1]) dp[x][y] += dp[x][y - 1];
     }
   if (dp[gx][gy] == 0) System.out.println("Miserable Hokusai!");
   else System.out.println(dp[gx][gy]);
 }
Exemple #14
0
  public void solve() throws Exception {
    long a = in.nextLong();
    long b = in.nextLong();

    long ans = 0;
    while (a != 0 && b != 0) {
      if (a > b) {
        ans += a / b;
        a = a % b;
      } else {
        ans += b / a;
        b = b % a;
      }
    }

    out.println(ans);
  }
Exemple #15
0
  /** @param args */
  public static void main(String[] args) throws Exception {
    File file = new File("B-small-practice.in");
    if (file.exists()) {
      System.setIn(new BufferedInputStream(new FileInputStream(file)));
    }
    sc = new Scanner(System.in);
    FileWriter fw = new FileWriter(new File("output.txt"));
    out = new PrintWriter(fw);

    Bsmall b = new Bsmall();
    int T = sc.nextInt();
    int t = 1;
    while (t <= T) {
      out.print("Case #" + t + ": ");
      b.solve();
      t++;
    }
    out.close();
    fw.close();
  }
Exemple #16
0
  /** @param args */
  public static void main(String[] args) throws Exception {
    out = System.out;
    File file = new File("input.txt");
    if (file.exists()) {
      System.setIn(new BufferedInputStream(new FileInputStream("input.txt")));
    }
    sc = new Scanner(System.in);
    POJ3109 p = new POJ3109();

    while (true) {
      try {
        N = sc.nextInt();
        if (N == 0) {
          break;
        }
      } catch (Exception ex) {
        break;
      }
      p.solve();
    }
  }
Exemple #17
0
    public void diff(int id, String small, String large) {
      try {
        Scanner sm = new Scanner(new File(small));
        Scanner lg = new Scanner(new File(large));
        int line = 0;
        while (sm.hasNextLine() && lg.hasNextLine()) {
          String s = sm.nextLine();
          String l = lg.nextLine();
          if (!s.equals(l)) {
            System.out.println(line + ">" + s);
            System.out.println(line + "<" + l);
          }
          line++;
        }

      } catch (Exception e) {
        e.printStackTrace();
      }
    }
Exemple #18
0
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    for (int N = sc.nextInt(); N > 0; N = sc.nextInt()) {
      int M = sc.nextInt();
      int L = sc.nextInt();
      boolean[][] map = new boolean[N][N];
      int[][] D = new int[N][N];
      int[][] E = new int[N][N];
      while (M-- > 0) {
        int A = sc.nextInt() - 1;
        int B = sc.nextInt() - 1;
        map[A][B] = map[B][A] = true;
        D[A][B] = D[B][A] = sc.nextInt();
        E[A][B] = E[B][A] = sc.nextInt();
      }

      boolean[][] visited = new boolean[N][L + 1];
      Queue<Edge> q = new PriorityQueue<Edge>();
      q.offer(new Edge(0, 0, L));
      while (!q.isEmpty()) {
        Edge e = q.poll();
        if (visited[e.to][e.money]) continue;
        visited[e.to][e.money] = true;
        if (e.to == N - 1) {
          System.out.println(e.enemy);
          break;
        }

        for (int i = 0; i < N; i++)
          if (map[e.to][i]) {
            if (D[e.to][i] <= e.money) q.offer(new Edge(e.enemy, i, e.money - D[e.to][i]));
            q.offer(new Edge(e.enemy + E[e.to][i], i, e.money));
          }
      }
    }
  }
Exemple #19
0
  public void solve() throws Exception {
    n = in.nextInt();
    m = in.nextInt();

    graph = new char[n][m];
    cracked = new boolean[n][m];

    for (int i = 0; i < n; i++) {
      graph[i] = in.next().trim().toCharArray();
    }
    sx = in.nextInt() - 1;
    sy = in.nextInt() - 1;

    gx = in.nextInt() - 1;
    gy = in.nextInt() - 1;

    cracked[sx][sy] = true;

    if (sx == gx && sy == gy) {
      if (graph[sx][sy] == 'X') {}
    }

    int scount = 0;
    for (int i = 0; i < 4; i++) {
      int nx = sx + dx[i];
      int ny = sy + dy[i];
      if (nx < 0 || nx > n - 1 || ny < 0 || ny > m - 1) {
        continue;
      }

      if (graph[nx][ny] == '.') {
        scount++;
      }
    }

    int gcount = 0;
    for (int i = 0; i < 4; i++) {
      int nx = gx + dx[i];
      int ny = gy + dy[i];
      if (nx < 0 || nx > n - 1 || ny < 0 || ny > m - 1) {
        continue;
      }

      if (graph[nx][ny] == '.') {
        gcount++;
      }
    }

    boolean isNeighbors = false;

    for (int i = 0; i < 4 && !isNeighbors; i++) {
      int nx = sx + dx[i];
      int ny = sy + dy[i];
      if (nx < 0 || nx > n - 1 || ny < 0 || ny > m - 1) {
        continue;
      }
      if (nx == gx && ny == gy) {
        isNeighbors = true;
      }
    }

    if (sx == gx && sy == gy) {
      if (graph[sx][sy] == 'X') {
        if (scount >= 1) {
          out.println("YES");
        } else {
          out.println("NO");
        }
      } else {
        if (scount >= 2) {
          out.println("YES");
        } else {
          out.println("NO");
        }
      }
    } else if (isNeighbors) {
      if (graph[gx][gy] == 'X') {
        out.println("YES");
      } else {
        if (gcount >= 1) {
          out.println("YES");
        } else {
          out.println("NO");
        }
      }
    } else if (dfs(sx, sy)) {
      if (graph[gx][gy] == 'X') {
        out.println("YES");
      } else {
        if (gcount >= 2) {
          out.println("YES");
        } else {
          out.println("NO");
        }
      }
    } else {
      out.println("NO");
    }
  }
Exemple #20
0
 public void run() {
   Scanner sc = new Scanner(System.in);
   int T = sc.nextInt();
   sc.nextLine();
   for (int t = 1; t <= T; t++) System.out.printf("Case #%s: %s%n", t, solve(sc));
 }
Exemple #21
0
  public void solve() throws Exception {
    P[] ps = new P[N];
    Map<Integer, Set<Integer>> map = new HashMap<Integer, Set<Integer>>();
    for (int i = 0; i < N; i++) {
      P p = new P();
      p.x = sc.nextInt();
      p.y = sc.nextInt();
      ps[i] = p;
      if (!map.containsKey(p.x)) {
        Set<Integer> set = new HashSet<Integer>();
        map.put(p.x, set);
      }
      map.get(p.x).add(p.y);
    }
    Arrays.sort(
        ps,
        new Comparator<P>() {
          @Override
          public int compare(P p1, P p2) {
            if (p1.x != p2.x) {
              return p1.x - p2.x;
            }
            return p1.y - p2.y;
          }
        });

    List<Pair> yp = new ArrayList<Pair>();
    for (int i = 0; i < ps.length - 1; i++) {
      if (ps[i].x != ps[i + 1].x) continue;
      int lidx = i + 1;
      while (lidx + 1 < ps.length && ps[i].x == ps[lidx + 1].x) {
        lidx++;
      }
      Pair pair = new Pair();
      pair.s = ps[i].y;
      pair.l = ps[lidx].y;
      pair.base = ps[i].x;
      yp.add(pair);
      i = lidx;
    }

    Arrays.sort(
        ps,
        new Comparator<P>() {
          @Override
          public int compare(P p1, P p2) {
            if (p1.y != p2.y) {
              return p1.y - p2.y;
            }
            return p1.x - p2.x;
          }
        });
    List<Pair> xp = new ArrayList<Pair>();
    for (int i = 0; i < ps.length - 1; i++) {
      if (ps[i].y != ps[i + 1].y) continue;
      int lidx = i + 1;
      while (lidx + 1 < ps.length && ps[i].y == ps[lidx + 1].y) {
        lidx++;
      }
      Pair pair = new Pair();
      pair.s = ps[i].x;
      pair.l = ps[lidx].x;
      pair.base = ps[i].y;
      xp.add(pair);
      i = lidx;
    }

    int ans = 0;
    for (int i = 0; i < yp.size(); i++) {
      int xnow = yp.get(i).base;
      int sy = yp.get(i).s;
      int ly = yp.get(i).l;
      for (int j = 0; j < xp.size(); j++) {
        int y = xp.get(j).base;
        if (y < sy || ly < y) continue;
        if (xp.get(j).s > xnow || xp.get(j).l < xnow) continue;
        try {
          if (!map.get(xnow).contains(y)) {
            ans++;
            map.get(xnow).add(y);
          }
        } catch (Exception ex) {

        }
      }
    }
    ans += ps.length;
    out.println(ans);
  }
Exemple #22
0
 public static void main(String[] args) {
   int sec = sc.nextInt() + sc.nextInt() + sc.nextInt() + sc.nextInt();
   System.out.println(sec / 60);
   System.out.println(sec % 60);
 }