关于我的Dinic
查看原帖
关于我的Dinic
1000298
Vsinger_洛天依楼主2024/9/10 08:38

我打了一个 Dinic ,但是我打的疑似有问题

就是说这个在跑最小割的时候总是比答案大,但是我拿这个去跑模板题又能A,谁能帮我调一下

#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define int long long
#define ull unsigned long long
#define re register
#define for_(a,b,P) for(int a=b;a<=P;a++)
#define _for(a,b,P) for(int a=b;a>=P;a--)
#define firein(a) freopen(a".in","r",stdin)
#define fireout(a) freopen(a".out","W",stdout)
#define fire(a) (firein(a),fireout(a))
#define lc(q) (q<<1)
#define rc(q) (q<<1|1)
#define mid(l,r) ((l+r)>>1)

using namespace std;

namespace IO{
    inline int read(){
        char ch=getchar();
        int s=0,f=1;
        while(ch<'0'||'9'<ch) {
            if(ch == '-'){
                f = -1;
            }
            ch = getchar();
        }
        while('0'<ch&&ch<'9'){
            s = s*10 + ch - '0';
            ch = getchar();
        }
        return s * f;
    }

    inline void write(int x){
        if(x < 0){
            putchar('-');
            x *= -1;
        }
        write(x/10);
        putchar(x%10 + '0'); 
    }

    inline void print(int x,char s){
        write(x);
        putchar(s);
    }

    inline double rnd(){
        char P = getchar();
        int f = 1,s = 0,x = 0;
        double t = 0;
        
        while((P<'0'||P>'9')&&P!='.'){
            if(P == '-')
                f *= -1;
            P = getchar();
        }
        while(P>='0'&&P<='9'&&P!='.'){
            x = x*10 + (P^48);
            P = getchar();
        }

        if(P=='.'){
            P=getchar();
        }
        else{ 
            return x*f;
        }

        while(P>='0'&&P<='9'){
            s+=1;
            t=t*10+(P^48);
            P=getchar();
        }
        while(s--){
            t/=10.0;
            x=(x+t)*f;
        }

        return x;
    }
}
using namespace IO;

const int N = 10005;
const int M = 10000005;
const ll INF=INT_MAX;
const ll inf=LLONG_MAX;

int Head[N],ver[N],Edge[N],nxt[N],d[N];
int now[N],tot,n,m,s,t,maxflow,x;

inline void Add(int x,int y,int z){
    ver[++tot] = y;
    Edge[tot] = z;
    nxt[tot] = Head[x];
    Head[x] = tot; 

    ver[++tot] = x;
    Edge[tot] = 0;
    nxt[tot] = Head[y];
    Head[y] = tot; 
}

queue<int> q;
inline bool BFS(){
    memset(d,0,sizeof(d));
    while(!q.empty()) q.pop();
    q.push(s);
    d[s] = 1; now[s] = Head[s];
    while(q.size()){
        int x = q.front(); q.pop();
        for(int i=Head[x] ; i ; i=nxt[i]){
            if(Edge[i] && !d[ver[i]]){
                q.push(ver[i]);
                now[ver[i]] = Head[ver[i]];
                d[ver[i]] = d[x] + 1;
                if(ver[i] == t) return 1;
            }
        }
    }
    return 0;
}

inline int Dinic(int x,int f){
    if(x==t) return f;
    int rest = f;
    for(int i = now[x];i && rest;i = nxt[i]){
        now [x] = i;
        if(Edge[i] && d[ver[i]] == d[x] + 1){
            int k = Dinic(ver[i],min(rest,Edge[i]));
            if(!k) d[ver[i]] = 0;
            Edge[i] -= k;
            Edge[i^1] += k;
            rest -= k;
        }
    }
    return f - rest;
}

const int dir[4][2] = {{0, 1},{0, -1},{1, 0},{-1, 0}};
int sum,S,T;
inline void In(){
    cin >> m >> n;
    S=0;T=n*m+1;
    tot=1;

	for_(i,1,n)
		for_(j,1,m){
			int val;
            cin >> val;
			sum += val;
			if ((i+j)%2==0) {
				Add(S,(i-1)*m+j,val);
				
				for_(k,0,3){
					int x = i + dir[k][0], y = j + dir[k][1];
					if (1<=x && x<=n && 1<=y && y<=m) {
						Add((i-1)*m+j, (x-1)*m+y, inf);
					}
				}				
			}
			else { 
				Add((i-1)*m+j, T, val);
			}
		}
	
	int maxflow = 0;
	while (BFS())
		maxflow += Dinic(S, inf);
    cout << sum - maxflow << endl;
    exit(0);
}


signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

	In();
}


2024/9/10 08:38
加载中...