Xtu bipartite graph and network flow exercise questions c. Number of squares (1)

Source: Internet
Author: User
C. number of squares (1) Time Limit: 5000 msmemory limit: 32768kb64-bit integer Io format: % i64d Java class name: Main gives you a checkboard with N x n, each grid contains a non-negative number.
Take out several numbers from them so that the lattice of any two numbers does not have a public edge. That is to say, the two grids of the obtained number cannot be adjacent and the sum of the obtained numbers is the largest. Input includes multiple test instances. Each test instance includes an integer N and N x N non-negative (n <= 20). Output for each test instance, maximum possible output and sample input
375 15 21 75 15 28 34 70 5 
Sample output
188

Problem-solving: Some people use status compression DP. I can only knock on others' code .....


1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <algorithm> 6 # include <climits> 7 # include <vector> 8 # include <queue> 9 # include <cstdlib> 10 # include <string> 11 # include <set> 12 # define ll long long13 # define INF 0x3f3f3f14 using namespace STD; 15 const int maxn = 510; 16 int MP [maxn * maxn], TOT, N, SRC, sink; 17 int C [maxn] [maxn]; 18 bool vis [maxn]; 19 int DF S (int u, int low) {20 if (u = sink) return low; 21 if (vis [u]) return 0; 22 vis [u] = true; 23 For (int v = 0, flow; v <= sink; V ++) {24 if (C [u] [v] & (flow = DFS (V, min (low, C [u] [v]) {25 C [u] [v]-= flow; 26 C [v] [u] + = flow; 27 return flow; 28} 29} 30 return 0; 31} 32 int maxflow () {33 int ans = 0, flow; 34 memset (VIS, false, sizeof (VIS); 35 while (flow = DFS (SRC, INF) {36 memset (VIS, false, sizeof (VIS); 37 ans + = flow; 38} 39 return ans; 40} 41 int main () {42 int I, j, temp; 43 while (~ Scanf ("% d", & N) {44 J = N * n; 45 src = tot = 0; 46 sink = J + 1; 47 memset (MP, 0, sizeof (MP); 48 memset (C, 0, sizeof (c); 49 for (I = 1; I <= J; I ++) {50 scanf ("% d", & temp); 51 tot + = temp; 52 if (I <= N) MP [I] =! MP [I-1]; 53 else MP [I] =! MP [I-n]; 54 if (MP [I]) {55 if (I % N) C [I] [I + 1] = inf; // 56 if (I % n! = 1) C [I] [I-1] = inf; // left 57 if (I <= N * (n-1) c [I] [n + I] = inf; // 58 if (I> N) C [I] [I-n] = inf; // 59 C [SRC] [I] = temp; 60} else C [I] [sink] = temp; 61} 62 printf ("% d \ n", tot-maxflow (); 63} 64 return 0; 65}
View code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.