玲珑OJ 1047 - Best couple 【最小费用流orKM】

本文介绍了一种解决最佳情侣配对问题的算法实现,通过构造一个由男孩和女孩组成的二分图,并使用最小费用流算法来寻找那些拥有最大总价值配对的方法。问题设定为在不同的城市中,男孩和女孩间存在一定的距离,目标是找到配对方案使得所有情侣的距离之和最大。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1047 - Best couple

Time Limit:1s Memory Limit:64MByte

Submissions:273Solved:77

DESCRIPTION

There are nn boys and mm girls, and they live in different n+mn+m cities.
The distance between city ii and city jj is L[i][j]L[i][j](if L[i][j]==1L[i][j]==−1, It means that there is no road between city ii and city jj ).
A girl and a boy can make up of a couple, and the value of this couple is the shortest distance between them by the city roads.
Boys live in city [1,n][1,n], and girls live in city [n+1,n+m][n+1,n+m].It ensures that it is an undirected graph.
Your mission is to arrange some of them to be some couples so that the sum of all couple's value is biggest.
Note: you just need the max couple’s value of couples, and it maybe not the max number of couples.

INPUT
There are multiple test cases.The first line is a number T ( T 10T ≤10), which means the number of cases.For each case, the first line contains two integers n,m,(1n,m100)n,m,(1≤n,m≤100).and the next is a (n+m)×(n+m)(n+m)×(n+m) matrix LL ( 1L[i][j]100−1≤L[i][j]≤100), which show the distances.
OUTPUT
one line --- print the biggest sum of couple's value.
SAMPLE INPUT
21 20 1 31 0 23 2 02 20 -1 2 2-1 0 7 22 7 0 -12 2 -1 0
SAMPLE OUTPUT
38


明显裸的二分图最大权匹配

没学过KM,只好跑最小费用流

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include <string.h>
#include<math.h>

using namespace std;
#define ll long long
#define pii pair<int,int>

const int inf=1e9+7;
const int INF=inf;
const int N = 200+5;//点
const int M = N*N*10;//边

struct Edge{
    int to,c,w,next;//c:流量,w:费用
}edge[M];
int head[N];

int nume=0;
inline void addEdge(int k,int u,int v,int c,int w){
    edge[k].to=v,
            edge[k].c=c,
            edge[k].w=w,
            edge[k].next=head[u];
    head[u]=k;
}

inline void addEdge(int u,int v,int c,int w){
    addEdge(nume++,u,v,c,w);
    addEdge(nume++,v,u,0,-w);
}

void init(int n){
    fill(head,head+n+1,-1);
    nume=0;
}

bool used[N];
int dis[N],load[N],p[N];//距离 ,前驱边,前驱点
bool spfa(int s,int e,int n){
    deque<int>que;
    for(int i=0;i<=n;++i){
        dis[i]=INF;
        load[i]=p[i]=-1;
        used[i]=false;
    }
    que.push_back(s);
    dis[s]=0;
    used[s]=true;
    while(!que.empty()){
        int u=que.front();
        que.pop_front();
        used[u]=false;
        for(int i=head[u];i!=-1;i=edge[i].next){
            if(edge[i].c>0){
                int v=edge[i].to;
                if(dis[v]>dis[u]+edge[i].w){
                    dis[v]=dis[u]+edge[i].w;
                    p[v]=u;
                    load[v]=i;
                    if(used[v]==false){
                        used[v]=true;
                        que.push_back(v);
                    }
                }
            }
        }
    }
    return dis[e]!=INF;
}

int min_cost_flow(int s,int t,int n){
    int ansflow=0,anscost=0;//最大流,最小费用
    while(spfa(s,t,n)){
        int u=t;
        int f=INF;
        while(p[u]!=-1){
            f=min(f,edge[load[u]].c);
            u=p[u];
        }
        u=t;
        while(p[u]!=-1){
            edge[load[u]].c-=f;
            edge[load[u]^1].c+=f;
            u=p[u];
        }
        anscost+=dis[t]*f;
        ansflow+=f;
    }
    return anscost;
}

int disCity[N][N];
void floyd(int d[][N],int n){
    for(int k=1;k<=n;++k){
        for(int i=1;i<=n;++i){
            for(int j=1;j<=n;++j){
                d[i][j]=min(d[i][k]+d[k][j],d[i][j]);
            }
        }
    }
}

void slove(int n,int m){
    fill(head,head+n+m+3,-1);
    const int S=n+m+1,T=S+1;
    for(int i=1;i<=n;++i){
        addEdge(S,i,1,0);
    }
    for(int i=1;i<=n;++i){
        for(int j=n+1;j<=n+m;++j){
            if(disCity[i][j]!=inf){
                addEdge(i,j,1,-disCity[i][j]);
            }
            addEdge(i,j,1,0);
        }
    }
    for(int j=n+1;j<=n+m;++j){
        addEdge(j,T,1,0);
    }
    printf("%d\n",-min_cost_flow(S,T,n+m+2));
}

int main(int argc, char *argv[])
{
    //freopen("/home/lu/Documents/r.txt","r",stdin);
    int n,m,T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n+m;++i){
            for(int j=1;j<=n+m;++j){
                scanf("%d",&disCity[i][j]);
                if(disCity[i][j]==-1){
                    disCity[i][j]=inf;
                }
            }
        }
        floyd(disCity,n+m);
        slove(n,m);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值