方格取数(1)

http://acm.hdu.edu.cn/showproblem.php?pid=1565

题解:网络流+最大流+最小割

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=400+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const ll INF = 0x3f3f3f3f3f3f3f;
int s,t,n,m,k,p,l,r,u,v;
ll w;
ll ans,cnt,flag,temp,sum;
int dis[N];
struct node{
    int u,v;
    ll c;
    node(){};
    node(int form,int to,ll cap):u(form),v(to),c(cap){}
};
vector<node>edge;
vector<int> G[N];
void Addedge(int u,int v,ll cap){
    edge.push_back({u,v,cap});
    edge.push_back({v,u,0});
    int sz=edge.size();//cout<<sz<<endl;
    G[u].push_back(sz-2);
    G[v].push_back(sz-1);
}
bool bfs(int u){
    memset(dis,-1,sizeof(dis));
    dis[u]=0;
    queue<int>q;
    q.push(u);
    while(!q.empty()){
        int u=q.front();
        q.pop();
        for(int i=0;i<G[u].size();i++){
            node e=edge[G[u][i]];//cout<<u<<" "<<e.v<<endl;
            if(dis[e.v]<0&&e.c>0){
                dis[e.v]=dis[u]+1;
                q.push(e.v);
            }
        }
    }
    return dis[t]>0;
}
ll dfs(int u,ll flow){
    if(u==t)
        return flow;
    ll now;
    for(int i=0;i<G[u].size();i++){
        node e=edge[G[u][i]];
        if(e.c>0&&dis[u]+1==dis[e.v]&&(now=dfs(e.v,min(flow,e.c)))){
            edge[G[u][i]].c-=now;
            edge[G[u][i]^1].c+=now;
            return now;
        }
    }
    return 0;
}
void dinic(){
    while(bfs(s)){
        ll res=0;
        while((res=dfs(s,INF))){
            ans+=res;
        }
    }
}
void init(){
    s=0;
    t=n*n+1;
    for(int i=0;i<=t;i++)G[i].clear();
    edge.clear();
    ans=0;
    sum=0;
}
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    //scanf("%d",&t);
    //int T=0;
    while(~scanf("%d",&n)){
        init();
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                scanf("%lld",&w);
                sum+=w;
                int id=(i-1)*n+j;
                if((i+j)%2){
                        Addedge(s,id,w);
                        if(j<n)Addedge(id,id+1,INF);
                        if(j>1)Addedge(id,id-1,INF);
                        if(i<n)Addedge(id,id+n,INF);
                        if(i>1)Addedge(id,id-n,INF);
                }else{
                    Addedge(id,t,w);
                }
            }
        }
        dinic();
        printf("%lld\n",sum-ans);
    }

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

 

全部评论

相关推荐

10-07 23:57
已编辑
电子科技大学 Java
八街九陌:博士?客户端?开发?啊?
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
10-05 10:13
已编辑
HHHHaos:让这些老登来现在秋招一下,简历都过不去
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务