问题描述

LG1344


题解

我太菜了,我一开始竟然没有看出这是个最小割裸题。。。

两个询问。

第一个询问,直接跑最小割就好了。

第二个询问,建图的时候边权建 \(1\) ,代表割掉这条边需要 \(1\) 的代价。


\(\mathrm{Code}\)

#include<bits/stdc++.h>
using namespace std;

template <typename Tp>
void read(Tp &x){
    x=0;char ch=1;int fh;
    while(ch!='-'&&(ch>'9'||ch<'0')) ch=getchar();
    if(ch=='-') ch=getchar(),fh=-1;
    else fh=1;
    while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    x*=fh;
}

const int maxn=37;
const int maxm=2007;

int n,m,S,T;

struct Graph{
    int Head[maxn],to[maxm],Next[maxm],w[maxm],tot=1;
    Graph(){
        memset(Head,0,sizeof(Head));
        memset(Next,0,sizeof(Next));
    }
    void add(int x,int y,int z){
        to[++tot]=y,Next[tot]=Head[x],Head[x]=tot,w[tot]=z;
    }
}G[2];

int d[maxn];

bool bfs(int gp){
    memset(d,0,sizeof(d));
    queue<int>q;q.push(S);d[S]=1;
    while(!q.empty()){
        int x=q.front();q.pop();
        for(int i=G[gp].Head[x];i;i=G[gp].Next[i]){
            int y=G[gp].to[i];
            if(d[y]||!G[gp].w[i]) continue;
            q.push(y);d[y]=d[x]+1;
            if(y==T) return true;
        }
    }
    return false;
}

int dfs(int x,int flow,int gp){
    if(x==T) return flow;
    int rest=flow;
    for(int i=G[gp].Head[x];i&&rest;i=G[gp].Next[i]){
        int y=G[gp].to[i];
        if(d[y]!=d[x]+1||!G[gp].w[i]) continue;
        int k=dfs(y,min(rest,G[gp].w[i]),gp);
        if(!k) d[y]=0;
        else{
            G[gp].w[i]-=k,G[gp].w[i xor 1]+=k;
            rest-=k;
        }
    }
    return flow-rest;
}

int ans;

int main(){
    read(n);read(m);
    for(int i=1,x,y,z;i<=m;i++){
        read(x);read(y);read(z);
        G[1].add(x,y,z);G[1].add(y,x,0);
        G[0].add(x,y,1);G[0].add(y,x,0);
    }
    S=1,T=n;
    while(bfs(1)){
        int t;
        while(t=dfs(S,0x3f3f3f3f,1)) ans+=t;
    }
    printf("%d ",ans);ans=0;
    while(bfs(0)){
        int t;
        while(t=dfs(S,0x3f3f3f3f,0)) ans+=t;
    }
    printf("%d\n",ans);
    return 0;
}