hdu 1853 | hdu 3488 | hdu3435
二分图的最优匹配。求最小费用流。要求所有点匹配下的最小费用,直接套用KM算法的即可。。
下面是 1853 AC代码:
[cpp]
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 305;
const int INF = (1<<30)-1;
int g[maxn][maxn];
int lx[maxn],ly[maxn];
int match[maxn];
bool visx[maxn],visy[maxn];
int slack[maxn];
int n;
bool dfs(int cur){
visx[cur] = true;
for(int y=1;y<=n;y++){
if(visy[y]) continue;
int t=lx[cur]+ly[y]-g[cur][y];
if(t==0){
visy[y] = true;
if(match[y]==-1||dfs(match[y])){
match[y] = cur;
return true;
}
}
else if(slack[y]>t){
slack[y]=t;
}
}
return false;
}
int KM(){
memset(match,-1,sizeof(match));
memset(ly,0,sizeof(ly));
for(int i=1 ;i<=n;i++){
lx[i]=-INF;
for(int j=1;j<=n;j++)
if(g[i][j]>lx[i]) lx[i]=g[i][j];
}
for(int x=1;x<=n;x++){
for(int i=1;i<=n;i++) slack[i]=INF;
while(true){
memset(visx,false,sizeof(visx));
memset(visy,false,sizeof(visy));
if(dfs(x)) break;
int d=INF;
for(int i=1;i<=n;i++){
if(!visy[i]&&d>slack[i]) d=slack[i];
}
for(int i=1;i<=n;i++){
if(visx[i]) lx[i]-=d;
}
for(int i=1;i<=n;i++){
if(visy[i]) ly[i]+=d;
else slack[i]-=d;
}
}
}
int result = 0;
for(int i = 1; i <=n; i++){
if(match[i]==-1||g[match[i]][i]==-INF)
return 1;
if(match[i]>-1)
result += g[match[i]][i];
}
return result;
}
int main(){
int m;
int a,b,c;
while(scanf("%d%d",&n,&m)!=EOF){
memset(g,0,sizeof(g));
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
g[i][j]=-INF;
for(int i=0;i<m;i++) {
scanf("%d%d%d",&a,&b,&c);
if(g[a][b]<-c)
g[a][b]=-c;
}
int ans=-KM();
printf("%d\n",ans);
}
return 0;
}
下面是3488AC代码:
[cpp]
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 205;
const int INF = (1<<30)-1;
int g[maxn][maxn];
int lx[maxn],ly[maxn];
int match[maxn];
bool visx[maxn],visy[maxn];
int slack[maxn];
int n;
bool dfs(int cur){
visx[cur] = true;
for(int y=1;y<=n;y++){
if(visy[y]) continue;
int t=lx[cur]+ly[y]-g[cur][y];
if(t==0){
visy[y] = true;
if(match[y]==-1||dfs(match[y])){
match[y] = cur;
return true;
}
}
else if(slack[y]>t){
slack[y]=t;
}
&n
补充:软件开发 , C++ ,