二分圖最大/小權匹配(kbin)

二分圖最大/小權匹配

二分圖的最小權匹配只要把邊權全部取相反數,再對答案取相反數即可

例題1:HDU2255

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<queue>
#include<map>
#define ll long long
#define pb push_back
#define rep(x,a,b) for (int x=a;x<=b;x++)
#define repp(x,a,b) for (int x=a;x<b;x++)
#define W(x) printf("%d\n",x)
#define WW(x) printf("%lld\n",x)
#define pi 3.14159265358979323846
#define mem(a,x) memset(a,x,sizeof a)
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
using namespace std;
const int maxn=3e2+7;
const int INF=1e9;
const ll INFF=1e18;
int mapp[maxn][maxn];//二分圖權值描述
int linker[maxn],lx[maxn],ly[maxn];//y中匹配狀態,x,y中的點標號
int slack[maxn],n,nx,ny;//兩邊的點數
bool visx[maxn],visy[maxn];
bool dfs(int x)
{
    visx[x]=true;
    rep(y,1,ny)
    {
        if (visy[y])continue;
        int tmp=lx[x]+ly[y]-mapp[x][y];
        if (tmp==0)
        {
            visy[y]=true;
            if (linker[y]==-1||dfs(linker[y]))
            {
                linker[y]=x;
                return true;
            }
        }
        else if (slack[y]>tmp)slack[y]=tmp;
    }
    return false;
}
int KM()
{
    mem(linker,-1);
    mem(ly,0);
    rep(i,1,nx)
    {
        lx[i]=-INF;
        rep(j,1,ny)
        {
            if (mapp[i][j]>lx[i])lx[i]=mapp[i][j];
        }
    }
    rep(x,1,nx)
    {
        rep(i,1,ny)slack[i]=INF;
        while(1)
        {
            mem(visx,false);
            mem(visy,false);
            if (dfs(x))break;
            int d=INF;
            rep(i,1,ny)
            {
                if (!visy[i]&&d>slack[i])d=slack[i];
            }
            rep(i,1,nx)
            {
                if (visx[i])lx[i]-=d;
            }
            rep(i,1,ny)
            {
                if (visy[i])ly[i]+=d;
                else slack[i]-=d;
            }
        }
    }
    int res=0;
    rep(i,1,ny)
        if (linker[i]!=-1)
            res+=mapp[linker[i]][i];
    return res;
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        rep(i,1,n)
            rep(j,1,n)
                scanf("%d",&mapp[i][j]);
        nx=ny=n;
        W(KM());
    }
    return 0;
}





例題2:POJ 2195

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<queue>
#include<map>
#define ll long long
#define pb push_back
#define rep(x,a,b) for (int x=a;x<=b;x++)
#define repp(x,a,b) for (int x=a;x<b;x++)
#define W(x) printf("%d\n",x)
#define WW(x) printf("%lld\n",x)
#define pi 3.14159265358979323846
#define mem(a,x) memset(a,x,sizeof a)
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
using namespace std;
const int maxn=2e2+7;
const int INF=1e9;
const ll INFF=1e18;
struct node
{
    int x,y;
}p1[maxn],p2[maxn];
int mapp[maxn][maxn];
char s[maxn][maxn];
int linker[maxn],lx[maxn],ly[maxn];
int slack[maxn],m1,m2,n,m;
bool visx[maxn],visy[maxn];
bool dfs(int x)
{
    visx[x]=true;
    rep(y,0,m2-1)
    {
        if (visy[y])continue;
        int tmp=lx[x]+ly[y]-mapp[x][y];
        if (tmp==0)
        {
            visy[y]=true;
            if (linker[y]==-1||dfs(linker[y]))
            {
                linker[y]=x;
                return true;
            }
        }
        else if (slack[y]>tmp)slack[y]=tmp;
    }
    return false;
}
int KM()
{
    mem(linker,-1);
    mem(ly,0);
    rep(i,0,m1-1)
    {
        lx[i]=-INF;
        rep(j,0,m2-1)
        {
            if (mapp[i][j]>lx[i])lx[i]=mapp[i][j];
        }
    }
    rep(x,0,m1-1)
    {
        rep(i,0,m2-1)slack[i]=INF;
        while(1)
        {
            mem(visx,false);
            mem(visy,false);
            if (dfs(x))break;
            int d=INF;
            rep(i,0,m2-1)
            {
                if (!visy[i]&&d>slack[i])d=slack[i];
            }
            rep(i,0,m1-1)
            {
                if (visx[i])lx[i]-=d;
            }
            rep(i,0,m2-1)
            {
                if (visy[i])ly[i]+=d;
                else slack[i]-=d;
            }
        }
    }
    int res=0;
    rep(i,0,m2-1)
    {
        if (linker[i]!=-1)
            res+=mapp[linker[i]][i];
    }
    return res;
}
int main()
{
    while(~scanf("%d%d",&n,&m)&&n&&m)
    {
        m1=0,m2=0;
        rep(i,1,n)
        {
            scanf("%s",s[i]);
            rep(j,0,strlen(s[i])-1)
            {
                if (s[i][j]=='m')p1[m1].x=i,p1[m1++].y=j;
                else if (s[i][j]=='H')p2[m2].x=i,p2[m2++].y=j;
            }
        }
        for (int i=0;i<m1;i++)
            for (int j=0;j<m2;j++)
                mapp[i][j]=-abs(p1[i].x-p2[j].x)-abs(p1[i].y-p2[j].y);
        W(-KM());
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章