Advertisement
Guest User

aropan_pairs_java_solution

a guest
Jun 12th, 2013
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.58 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import static java.lang.Math.abs;
  4.  
  5. public class pairs_in_matrix_ar
  6. {
  7.     static int MAXN = 81;
  8.     static int MAXT = MAXN * MAXN;
  9.     static int MAXK = MAXT * 8;
  10.     static int INF = (int)1e+9 + 1;
  11.  
  12.     static int a[][] = new int[MAXN][MAXN];
  13.  
  14.     static int next[] = new int[MAXK];
  15.     static int dst[] = new int[MAXK];
  16.     static int src[] = new int[MAXK];
  17.     static int flow[] = new int[MAXK];
  18.     static int dist[] = new int[MAXK];
  19.     static int last[] = new int[MAXT];
  20.     static int phi[] = new int[MAXT];
  21.     static int d[] = new int[MAXT];
  22.     static int p[] = new int[MAXT];
  23.  
  24.     static int frs[] = new int[MAXK];
  25.     static int lst[] = new int[MAXK];
  26.     static int nxt[] = new int[MAXK];
  27.     static int val[] = new int[MAXK];
  28.  
  29.     static int n, m, k, t, c = 0;
  30.     static int ans;
  31.     static int cnt;
  32.  
  33.     public static void adde(int x, int y, int f, int c)
  34.     {
  35.         k += 1;
  36.         src[k] = x;
  37.         dst[k] = y;
  38.         flow[k] = f;
  39.         dist[k] = c;
  40.         next[k] = last[x];
  41.         last[x] = k;
  42.     }
  43.  
  44.     public static void adde(int x, int y, int c)
  45.     {
  46.         adde(x, y, 1, c);
  47.         adde(y, x, 0, -c);
  48.     }
  49.    
  50.     public static void eadd(int i, int v)
  51.     {
  52.         c += 1;
  53.         if (frs[i] == 0) frs[i] = c;
  54.         if (lst[i] != 0) nxt[lst[i]] = c;
  55.         val[c] = v;
  56.         lst[i] = c;
  57.     }
  58.  
  59.     public static int dijkstra(int st)
  60.     {
  61.         for (int i = 1; i <= t; i++)
  62.         {
  63.             d[i] = INF;
  64.             p[i] = -1;
  65.         }
  66.         for (int i = 0; i <= c; i++)
  67.         {
  68.             frs[i] = 0;
  69.             lst[i] = 0;
  70.             nxt[i] = 0;
  71.         }
  72.         c = 0;
  73.  
  74.         p[st] = d[st] = 0;
  75.         eadd(0, st);
  76.         for (int l = 0; l < t; l++)
  77.             for (int j = frs[l]; j > 0; j = nxt[j])
  78.             {
  79.                 int x = val[j];
  80.                 if (d[x] != l)
  81.                     continue;
  82.                 for (int i = last[x]; i > 0; i = next[i])
  83.                 {
  84.                     cnt += 1;
  85.                     if (flow[i] == 0)
  86.                         continue;
  87.                     int y = dst[i];
  88.                     int v = l + dist[i] + phi[x] - phi[y];
  89.                     if (v < d[y])
  90.                     {
  91.                         d[y] = v;
  92.                         p[y] = i;
  93.                         eadd(v, y);
  94.                     }
  95.                 }
  96.             }
  97.         int res = d[t] + phi[t] - phi[st];
  98.         for(int i = 1; i <= t; ++i)
  99.             phi[i] += p[i] != -1? d[i] : d[t];
  100.         return res;
  101.     }
  102.  
  103.     public static void main(String[] args) throws FileNotFoundException
  104.     {
  105.         InputStream inputStream = System.in;
  106.         OutputStream outputStream = System.out;
  107.         InputReader in = new InputReader(inputStream);
  108.         OutputWriter out = new OutputWriter(outputStream);
  109.         int n = in.readInt();
  110.         int m = in.readInt();
  111.         for (int i = 1; i <= n; i++)
  112.             for (int j = 1; j <= m; j++)
  113.                 a[i][j] = in.readInt();
  114.         k = 1;
  115.         for (int i = 1; i <= n; i++)
  116.             for (int j = 1; j <= m; j++)
  117.                 if (((i + j) & 1) != 0)
  118.                     for (int dx = -1; dx <= 1; dx += 1)
  119.                     {
  120.                         for (int dy = dx == 0? -1 : 0; dy <= 1; dy += 2)
  121.                         {
  122.                             int x = (i - 1) * m + j;
  123.                             int y = (i + dx - 1) * m + j + dy;
  124.                             i += dx;
  125.                             j += dy;
  126.                             if (1 <= i && i <= n && 1 <= j && j <= m)
  127.                             {
  128.                                 int c = a[i - dx][j - dy] != a[i][j]? 1 : 0;
  129.                                 adde(x, y, c);
  130.                             }
  131.                             i -= dx;
  132.                             j -= dy;
  133.                         }
  134.                     }
  135.         t = n * m + 2;
  136.         for (int i = 1; i <= n; i++)
  137.             for (int j = 1; j <= m; j++)
  138.                 if (((i + j) & 1) != 0)
  139.                     adde(t - 1, (i - 1) * m + j, 0);
  140.                 else
  141.                     adde((i - 1) * m + j, t, 0);
  142.  
  143.  
  144.         for (int i = 1; i <= n * m / 2; i++)
  145.         {
  146.             int v = dijkstra(t - 1);
  147.     //      cout << v << endl;
  148.             ans += v;
  149.             int x = t;
  150.  
  151.             while (x != t - 1)
  152.             {
  153.                 flow[p[x] ^ 0] -= 1;
  154.                 flow[p[x] ^ 1] += 1;
  155.                 x = src[p[x]];
  156.             }
  157.         }
  158.         out.printLine(ans);
  159.         out.close();
  160.     }
  161. }
  162.  
  163. class InputReader {
  164.  
  165.     private InputStream stream;
  166.     private byte[] buf = new byte[1024];
  167.     private int curChar;
  168.     private int numChars;
  169.     private SpaceCharFilter filter;
  170.  
  171.     public InputReader(InputStream stream) {
  172.         this.stream = stream;
  173.     }
  174.  
  175.     public int read() {
  176.         if (numChars == -1)
  177.             throw new InputMismatchException();
  178.         if (curChar >= numChars) {
  179.             curChar = 0;
  180.             try {
  181.                 numChars = stream.read(buf);
  182.             } catch (IOException e) {
  183.                 throw new InputMismatchException();
  184.             }
  185.             if (numChars <= 0)
  186.                 return -1;
  187.         }
  188.         return buf[curChar++];
  189.     }
  190.  
  191.     public int readInt() {
  192.         int c = read();
  193.         while (isSpaceChar(c))
  194.             c = read();
  195.         int sgn = 1;
  196.         if (c == '-') {
  197.             sgn = -1;
  198.             c = read();
  199.         }
  200.         int res = 0;
  201.         do {
  202.             if (c < '0' || c > '9')
  203.                 throw new InputMismatchException();
  204.             res *= 10;
  205.             res += c - '0';
  206.             c = read();
  207.         } while (!isSpaceChar(c));
  208.         return res * sgn;
  209.     }
  210.  
  211.     public long readLong() {
  212.         int c = read();
  213.         while (isSpaceChar(c))
  214.             c = read();
  215.         int sgn = 1;
  216.         if (c == '-') {
  217.             sgn = -1;
  218.             c = read();
  219.         }
  220.         long res = 0;
  221.         do {
  222.             if (c < '0' || c > '9')
  223.                 throw new InputMismatchException();
  224.             res *= 10;
  225.             res += c - '0';
  226.             c = read();
  227.         } while (!isSpaceChar(c));
  228.         return res * sgn;
  229.     }
  230.  
  231.     public boolean isSpaceChar(int c) {
  232.         if (filter != null)
  233.             return filter.isSpaceChar(c);
  234.         return isWhitespace(c);
  235.     }
  236.  
  237.     public static boolean isWhitespace(int c) {
  238.         return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  239.     }
  240.  
  241.     public interface SpaceCharFilter {
  242.         public boolean isSpaceChar(int ch);
  243.     }
  244. }
  245.  
  246. class OutputWriter {
  247.     private final PrintWriter writer;
  248.  
  249.     public OutputWriter(OutputStream outputStream) {
  250.         writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
  251.     }
  252.  
  253.     public OutputWriter(Writer writer) {
  254.         this.writer = new PrintWriter(writer);
  255.     }
  256.  
  257.     public void print(Object...objects) {
  258.         for (int i = 0; i < objects.length; i++) {
  259.             if (i != 0)
  260.                 writer.print(' ');
  261.             writer.print(objects[i]);
  262.         }
  263.     }
  264.  
  265.     public void printLine(Object...objects) {
  266.         print(objects);
  267.         writer.println();
  268.     }
  269.  
  270.     public void close() {
  271.         writer.close();
  272.     }
  273.  
  274.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement