2813 One fihgt one //MaxMatch

One fihgt one

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 998    Accepted Submission(s): 302

Problem Description
Lv Bu and his soldiers are facing a cruel war——Cao Cao had his best generals just miles away.

There’s little time , but Lv Bu is unaware of how to arrange his warriors , what he know is that he have n brave generals while Cao Cao has m , and he has k fights to choose from , he’d like to make all his n warriors participate in the battle but get the least injuries . Lv Bu is happy because there is always a good solution . So , now is your task to tell Lv Bu the least injuries his troop would get.
No one could take part in two fights.
 

 

Input
Multiple cases. For each case ,there are three integers in the first line , namely n,m (1<=n<=m<=200)and k (n<=k<=m*n).
The next k lines are the information about k possible fights , for each line are two strings (no more than 20 characters ) and an integer. The first string indicates Lv Bu’s general and the second , of course , Cao Cao’s , and the integer is the injury Lv Bu’s general would get if this fight were chosen.
 

 

Output
One integer , the least injuries Lv Bu’s generals would get.
 

 

Sample Input
  
  
2 3 5 LvBu ZhangFei 6 LvBu GuanYu 5 LvBu XuChu 4 ZhangLiao ZhangFei 8 ZhangLiao XuChu 3
 

 

Sample Output
  
  
8
 

 

Author
shǎ崽
 

 

Source
 

 

Recommend
lcy
 

 

Statistic |  Submit |  Discuss | Back

 

 

#include<cstdio>
#include<string.h>
#include<iostream>
#include<map>
#include<string>
using namespace std;
const int INF=1<<25-1;
int mat[250][250],lx[250],ly[250];//顶点标号
bool sx[250],sy[250];//是否已经搜索过
int link[250],stack[250],cas1,cas2,n,m;
bool path(int k)//从x[k]寻找增广路
{
    sx[k]=true;
    for(int i=0; i<m; i++)
    {
        if(mat[k][i]!=-INF&&!sy[i])
        {
            int t=ly[i]+lx[k]-mat[k][i];
            if(t==0)
            {
                sy[i]=1;
                if(link[i]==-1||path(link[i]))
                {
                    link[i]=k;
                    return true;
                }
            }
            else if(stack[i]>t) stack[i]=t;
        }
    }
    return false;
}
int BestMatch()//求解最小权匹配
{
    int d,sum;
    memset(ly,0,sizeof(ly));
    memset(link,-1,sizeof(link));
    for(int i=0; i<n; i++)
    {
        lx[i]=-1<<24; //x中顶点i的编号为与i关联的Y中边的最大权重
        for(int j=0; j<m; j++) if(lx[i]<mat[i][j]&&mat[i][j]!=-INF)  lx[i]=mat[i][j];
    }
    for(int k=0; k<n; k++)
    {
        for (int i=0; i<m; i++) stack[i]=10000000;
        while(1)
        {
            memset(sx,0,sizeof(sx));
            memset(sy,0,sizeof(sy));
            if(path(k))  break;//匹配成功
            d=10000000;
            for(int i=0; i<m; i++)if (!sy[i]&&stack[i]<d) d=stack[i];
            for(int i=0; i<n; i++) if(sx[i]) lx[i]-=d;
            for(int i=0; i<m; i++) if(sy[i]) ly[i]+=d;
                else  stack[i]-=d;
        }
    }
    sum=0;
    for(int i=0; i<m; i++)
    if(link[i]!=-1)
    sum+=mat[link[i]][i];
//注意这里本来是求的最大权匹配,这里取反即为最小权匹配(因为临街矩阵中的边的权值也取反了)
    return -sum;
}
int main()
{
    int k;
    string s1,s2;
    int c,j1,j2;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        memset(mat,0,sizeof(mat));
        map<string,int>lv,cao;
        map<string,int>::iterator it;
        cas1=0,cas2=0;
        for(int i=0;i<n;i++)
          for(int j=0;j<m;j++)
          mat[i][j]=-INF;
        for(int i=0; i<k; i++)
        {
            cin>>s1>>s2>>c;
            it=lv.find(s1);
            if(it==lv.end()) lv[s1]=cas1,j1=cas1++;
            else  j1=it->second;
            it=cao.find(s2);
            if(it==cao.end()) cao[s2]=cas2,j2=cas2++;
            else  j2=it->second;
            mat[j1][j2]=-c;
        }
        printf("%d/n",BestMatch());
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值