import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; public class kug { public static void main(String[] args) throws IOException { kug solver = new kug(); solver.init(); solver.solve(); } private void init() { } private static class Segment implements Comparable<Segment> { int from; int to; int cost; public Segment(int from, int to, int cost) { super(); this.from = from; this.to = to; this.cost = cost; } @Override public int compareTo(Segment s) { return this.cost - s.cost; } } boolean[][] visited; int[] left; int[] right; private void solve() throws IOException { Reader in = new Reader(System.in); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int n = in.nextInt(); Segment[] segs = new Segment[n * (n+1) / 2]; int k = 0; visited = new boolean[n+1][n+1]; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { segs[k++] = new Segment(i,j+1,in.nextInt()); } } Arrays.sort(segs); long r = 0; left = new int[n+1]; right = new int[n+1]; Arrays.fill(left, -1); Arrays.fill(right, -1); for (Segment s : segs) { if (check(s.from, s.to)) { r += s.cost; } } out.println(r); out.flush(); out.close(); } private boolean check(int from, int to) { if (visited[from][to]) return false; if (left[from] != -1) { if (left[from] < to) { visited[from][to] = true; return check(left[from], to); } else { int toNew = left[from]; right[left[from]] = -1; left[from] = -1; check(from,to); check(to, toNew); return true; } } else if (right[to] != -1) { if (right[to] > from) { visited[from][to] = true; return check(from, right[to]); } else { int fromNew = right[to]; left[right[to]] = -1; right[to] = -1; check(fromNew, from); check(from, to); return true; } } else { visited[from][to] = true; left[from] = to; right[to] = from; return true; } } private static class Reader { BufferedReader reader; StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ Reader(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } public void skipLine() throws IOException { reader.readLine(); } /** get next word */ public String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt( next() ); } public double nextDouble() throws IOException { return Double.parseDouble( next() ); } public long nextLong() throws IOException { return Long.parseLong(next()); } } }
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; public class kug { public static void main(String[] args) throws IOException { kug solver = new kug(); solver.init(); solver.solve(); } private void init() { } private static class Segment implements Comparable<Segment> { int from; int to; int cost; public Segment(int from, int to, int cost) { super(); this.from = from; this.to = to; this.cost = cost; } @Override public int compareTo(Segment s) { return this.cost - s.cost; } } boolean[][] visited; int[] left; int[] right; private void solve() throws IOException { Reader in = new Reader(System.in); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int n = in.nextInt(); Segment[] segs = new Segment[n * (n+1) / 2]; int k = 0; visited = new boolean[n+1][n+1]; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { segs[k++] = new Segment(i,j+1,in.nextInt()); } } Arrays.sort(segs); long r = 0; left = new int[n+1]; right = new int[n+1]; Arrays.fill(left, -1); Arrays.fill(right, -1); for (Segment s : segs) { if (check(s.from, s.to)) { r += s.cost; } } out.println(r); out.flush(); out.close(); } private boolean check(int from, int to) { if (visited[from][to]) return false; if (left[from] != -1) { if (left[from] < to) { visited[from][to] = true; return check(left[from], to); } else { int toNew = left[from]; right[left[from]] = -1; left[from] = -1; check(from,to); check(to, toNew); return true; } } else if (right[to] != -1) { if (right[to] > from) { visited[from][to] = true; return check(from, right[to]); } else { int fromNew = right[to]; left[right[to]] = -1; right[to] = -1; check(fromNew, from); check(from, to); return true; } } else { visited[from][to] = true; left[from] = to; right[to] = from; return true; } } private static class Reader { BufferedReader reader; StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ Reader(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } public void skipLine() throws IOException { reader.readLine(); } /** get next word */ public String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt( next() ); } public double nextDouble() throws IOException { return Double.parseDouble( next() ); } public long nextLong() throws IOException { return Long.parseLong(next()); } } } |