1. 程式人生 > >km板子(二分圖最大權匹配)

km板子(二分圖最大權匹配)

define != tdi push_back air base long temp 匹配

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define fio ios::sync_with_stdio(false);cin.tie(0)
template<typename T>
inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>
inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}

using namespace std;

const double eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=200+10,maxn=50000+10,inf=0x3f3f3f3f;

int w[N][N];
ll lx[N],ly[N]; //頂標
 
int linky[N];
ll pre[N];
bool vis[N];
bool visx[N],visy[N];
ll slack[N];
int n;

void bfs(int k)
{
    ll px,py=0,yy=0,d;
    memset(pre,0,sizeof pre);
    for(int i = 0; i < N; ++i)
        slack[i] = INF;
    linky[py]=k;
    do
    {
        px=linky[py],d=INF,vis[py]=1;
        for(int i=1;i<=n;i++)
            if(vis[i]==0)
            {
                if(slack[i]>lx[px]+ly[i]-w[px][i])
                    slack[i]=lx[px]+ly[i]-w[px][i],pre[i]=py;
                if(slack[i]<d) d=slack[i],yy=i;
            }
        for(int i=0;i<=n;i++)
            if(vis[i]) lx[linky[i]]-=d,ly[i]+=d;
            else slack[i]-=d;
        py=yy;
    }while(linky[py]!=0);
    while(py) linky[py]=linky[pre[py]],py=pre[py];
}
void KM()
{
    memset(lx,0,sizeof lx);
    memset(ly,0,sizeof ly);
    memset(linky,0,sizeof linky);
    for(int i=1;i<=n;i++)
        memset(vis,0,sizeof vis),bfs(i);
}
int main()
{
    int T;
    scanf("%d", &T); 
    for(int cas = 1; cas <= T; ++cas)
    {
        scanf("%d", &n);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&w[i][j]);
                w[i][j] = -w[i][j];
            }            
        }
        KM();
        ll ans = 0;
        for(int i = 1; i <= n; ++i)
        {
            ans += lx[i];
            ans += ly[i];
        }
        printf("Case #%d: %I64d\n",cas, -ans);
    }
    return 0;
}
/********************

********************/

km板子(二分圖最大權匹配)