1. 程式人生 > >P2774 方格取數問題 網絡最大流 割

P2774 方格取數問題 網絡最大流 割

i++ cos 分享圖片 tac class etc iostream pro sizeof

P2774 方格取數問題:https://www.luogu.org/problemnew/show/P2774

題意:  

  給定一個矩陣,取出不相鄰的數字,使得數字的和最大。

思路:

  可以把方格分成兩個部分,橫坐標和縱坐標和為奇數的一組,和為偶數的一組,超級源點向偶數一組連容量為格點數字大小的邊,奇數一組向超級匯點連容量為格點大小的邊。然後兩組間相臨的點連容量為無窮的邊。

  跑出這個圖的最大流,相當於是最小割,就是去掉了最少的部分使得網絡不流通。因此答案就是sum - dinic();

技術分享圖片
#include <algorithm>
#include  <iterator>
#include  
<iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include
<queue> #include <list> #include <map> #include <set> #include <cassert> using namespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define
pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//這是一個大根堆q //priority_queue<int,vector<int>,greater<int> >q;//這是一個小根堆q #define fi first #define se second //#define endl ‘\n‘ #define OKC ios::sync_with_stdio(false);cin.tie(0) #define FT(A,B,C) for(int A=B;A <= C;++A) //用來壓行 #define REP(i , j , k) for(int i = j ; i < k ; ++i) #define max3(a,b,c) max(max(a,b), c); #define min3(a,b,c) min(min(a,b), c); //priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1000000007; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黃金分割點 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<0||ch>9) f|=(ch==-),ch=getchar(); while (ch>=0&&ch<=9) x=x*10+ch-0,ch=getchar(); return x=f?-x:x; } /*-----------------------showtime----------------------*/ const int maxn = 109; int n,m; int mp[maxn][maxn]; struct E{ int u,v,val; int nxt; }edge[maxn*maxn*2]; int head[maxn*maxn],gtot = 0; void addedge(int u,int v,int val){ edge[gtot].u = u; edge[gtot].v = v; edge[gtot].val = val; edge[gtot].nxt = head[u]; head[u] = gtot++; edge[gtot].u = v; edge[gtot].v = u; edge[gtot].val = 0; edge[gtot].nxt = head[v]; head[v] = gtot++; } int dis[maxn*maxn]; bool bfs(int s,int t){ memset(dis, inf, sizeof(dis)); dis[s] = 0; queue<int>que; que.push(s); while(!que.empty()){ int u = que.front(); que.pop(); for(int i = head[u]; ~i; i = edge[i].nxt){ int v = edge[i].v; if(edge[i].val > 0 && dis[v] >= inf){ dis[v] = dis[u] + 1; que.push(v); } } } return dis[t] < inf; } int dfs(int u,int t,int maxflow){ if(u == t || maxflow == 0) return maxflow; for(int i=head[u]; ~i; i = edge[i].nxt){ int v = edge[i].v, val = edge[i].val; if(dis[v] == dis[u] + 1 && val > 0){ int flow = dfs(v, t,min(val, maxflow)); if(flow > 0){ edge[i].val -= flow; edge[i^1].val += flow; return flow; } } } return 0; } int dinic(int s,int t){ int flow = 0; while(bfs(s,t)){ while(int f = dfs(s,t,inf)) flow += f; } return flow; } int cal(int i,int j){ return (i-1) * m + j; } int nx[4][2] = { {1,0},{0,1},{-1,0},{0,-1} }; int main(){ scanf("%d%d", &n, &m); int s = 0, t = n*m+1; int sum = 0; memset(head, -1, sizeof(head)); for(int i=1; i<=n; i++){ for(int j=1; j<=m; j++){ scanf("%d", &mp[i][j]); if((i + j) % 2 == 0) addedge(s, cal(i,j), mp[i][j]); else addedge(cal(i,j), t, mp[i][j]); sum += mp[i][j]; } } for(int i=1; i<=n; i++){ for(int j=1; j<=m; j++){ if((i+j)% 2) continue; for(int k=0; k< 4; k++){ int x = i + nx[k][0]; int y = j + nx[k][1]; if(x < 1 || x >n || y < 1 || y > m)continue; addedge(cal(i,j), cal(x,y), inf); } } } cout<<sum - dinic(s,t)<<endl; return 0; }
View Code

P2774 方格取數問題 網絡最大流 割