二分圖的最優匹配。求最小費用流。要求所有點匹配下的最小費用,直接套用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;
}
}
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,T;
int a,b,c;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
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;
}
下面是 3435 AC 代碼:
[cpp]
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 1005;
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,T,ca=1;
int a,b,c;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
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;
if(g[b][a]<-c)
g[b][a]=-c;
}
int ans=-KM();
if(ans==-1)
printf("Case %d: NO\n",ca++);
else
printf("Case %d: %d\n",ca++,ans);
}
return 0;
}