hdu 3488
可以作为KM 二分图最大权匹配模板
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
const int N=210;
const int inf=0x2fffffff;
const int Max=20000;
int match[N],n,m,lack,w[N][N],lx[N],ly[N];
bool vx[N],vy[N];
bool dfs(int x)
{
vx[x]=1;
for(int i=1;i<=n;i++)
{
if(vy[i]) continue;
int t=(lx[x]+ly[i]-w[x][i]);
if(t==0)
{
vy[i]=1;
if(match[i]==-1||dfs(match[i]))
{
match[i]=x;
return 1;
}
}
else if(lack>t) lack=t;
}
return 0;
}
void KM()
{
memset(lx,0,sizeof(lx));
memset(ly,0,sizeof(ly));
memset(match,-1,sizeof(match));
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(w[i][j]>lx[i])
lx[i]=w[i][j];
for(int i=1;i<=n;i++)
{
while(1)
{
memset(vx,0,sizeof(vx));
memset(vy,0,sizeof(vy));
lack=inf;
if(dfs(i)) break;
for(int i=1;i<=n;i++)
{
if(vx[i]) lx[i]-=lack;
if(vy[i]) ly[i]+=lack;
}
}
}
}
int main()
{
int cas,i,j;
int sum;
scanf("%d",&cas);
while(cas--)
{
scanf("%d%d",&n,&m);
memset(w,0,sizeof(w));
for(i=0; i<m; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(Max-c>w[a][b])w[a][b]=Max-c;
}
KM();
sum=0;
for( i=1; i<=n; i++)
sum+=Max-w[match[i]][i];
printf("%d\n",sum);
}
return 0;
}