本文主要是介绍HDU 3657 Game 网络流 方格取数类型,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
用模板就是爽!
#include<cstdio>
#include<cstring>
#include<iostream>
#include<iomanip>
#include<queue>
#include<cmath>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int int_max = 0x07777777;
const int int_min = 0x80000000;
const int maxn = 55*55;struct Edge{int from, to, cap, flow;Edge(int _from, int _to, int _cap, int _flow):from(_from),to(_to),cap(_cap),flow(_flow){}
};
vector<Edge> es;
vector<int> g[maxn];
int s,t,N,M,K;
int p[maxn],d[maxn],gap[maxn],cur[maxn];
void addedge (int from, int to, int cap){es.push_back(Edge(from, to, cap, 0));es.push_back(Edge(to, from, 0,0));int num = es.size();g[from].push_back(num-2);g[to].push_back(num-1);
}
void BFS (){queue<int> q;memset(d, 0, sizeof(d));d[t] = 0;q.push(t);while(!q.empty()){int u = q.front();q.pop();for(int i = 0; i < g[u].size(); i++){Edge& e = es[g[u][i]];if(e.cap==0 && d[e.to]==0){d[e.to] = d[u]+1;q.push(e.to);}}}
}
int augment (){int x = t;int a = int_max;while(x!=s){Edge& e = es[p[x]];a = (a < e.cap-e.flow ? a : e.cap-e.flow);x = es[p[x]].from;}x = t;while(x!=s){es[p[x]].flow += a;es[p[x]^1].flow -= a;x = es[p[x]].from;}return a;
}
int sap (){int flow = 0;BFS();memset(gap, 0, sizeof(gap));for(int i = 0; i <= t; i++) gap[d[i]]++;int x = s;memset(cur, 0, sizeof(cur));while(d[s] < t+1){if(x==t){flow += augment();x = s;}int ok = 0;for(int i = 0; i < g[x].size(); i++){Edge& e = es[g[x][i]];if(e.cap > e.flow && d[x]==d[e.to]+1){ok = 1;p[e.to] = g[x][i];cur[x] = i;x = e.to;break;}}if(!ok){int mm = t;for(int i = 0; i < g[x].size(); i++){Edge& e = es[g[x][i]];if(e.cap > e.flow) mm = (mm > d[e.to] ? d[e.to] : mm);}if(--gap[d[x]] == 0) break;gap[d[x]=mm+1]++;cur[x] = 0;if(x!=s) x = es[p[x]].from;}}return flow;
}int main(int argc, const char * argv[])
{while(scanf("%d %d %d", &N, &M, &K)!=EOF){int mymap[55][55];es.clear();for(int i = 0; i < maxn; i++) g[i].clear();s = N*M;t = s+1;int total = 0;for(int i = 0; i < N; i++){for(int j = 0; j < M; j++) {scanf("%d", &mymap[i][j]);total += mymap[i][j];}}int bixu[55][55];memset(bixu, 0, sizeof(bixu));for(int i = 0; i < K; i++){int x, y;scanf("%d %d", &x, &y);bixu[x-1][y-1] = 1;}int dir[4][2] = {-1,0,0,1,1,0,0,-1};for(int i = 0; i < N; i++){for(int j = 0; j < M; j++){if((i+j)&1){if(bixu[i][j]){addedge(s, i*M+j, int_max);}else{addedge(s, i*M+j, mymap[i][j]);}for(int u = 0; u < 4; u++){int ii = i + dir[u][0];int jj = j + dir[u][1];if(ii>=0 && ii<N && jj>=0 && jj<M){addedge(i*M+j, ii*M+jj, 2*(mymap[i][j]&mymap[ii][jj]));}}}else{if(bixu[i][j]){addedge(i*M+j, t, int_max);}else{addedge(i*M+j, t, mymap[i][j]);}}}}printf("%d\n", total-sap());}
}
这篇关于HDU 3657 Game 网络流 方格取数类型的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!