hdu 2813 KM匹配

传送门




One fihgt one

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


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   |   We have carefully selected several similar problems for you:   2811  2816  2809  2810  2812 
 
题意:一看就知道是匹配。水题赶紧过过用map处理给他的每一个string相应的点就好了

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;

#define pi acos(-1)
#define endl '\n'
#define srand() srand(time(0));
#define me(x,y) memset(x,y,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while
#define sgn(x) ((x) < 0 ? -1 : (x) > 0)
#define bug printf("***********\n");
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,1,-1,-1,1};
const int dy[]={0,1,0,-1,-1,1,-1,1};
const int maxn=1000;
const int maxx=1e7+100;
const double EPS=1e-7;
const int MOD=10000007;
#define mod(x) ((x)%MOD);
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
inline int Scan()
{
    int Res=0,ch,Flag=0;
    if((ch=getchar())=='-')Flag=1;
    else if(ch>='0' && ch<='9')Res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')Res=Res*10+ch-'0';
    return Flag ? -Res : Res;
}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.out" , "w" , stdout );
//cerr << "run time is " << clock() << endl;

int g[maxn][maxn];     //二分图表示.
int link[maxn],lx[maxn],ly[maxn];   //y的匹配关系. x,y的标杆状态.
bool visx[maxn],visy[maxn];
int nx,ny,d;   //两边的点数. 以及需要寻找的最小的d.
bool Find(int x)
{
    visx[x] = true;
    for(int i=1;i<=ny;i++)
    {
        if(visy[i]) continue;
        int tmp = lx[x] + ly[i] - g[x][i];
        if(!tmp)
        {   //tmp=0表示在当前图匹配了的边, 即看现在匹配是否合理,
            visy[i] = true;
            if(link[i] == -1 || Find(link[i]))
            {
                link[i] = x;
                return true;
            }
        }
        else d=min(d,tmp); //就是在S集合中的x,和不在T集合中的y找一个最小的d.
    }
    return false;
}

int KM()
{
    me(link,-1);
    me(ly,0); me(lx,0);
    for(int i=1;i<=nx;i++)
    {
        lx[i]=-INF;
        for(int j=1;j<=ny;j++)
        {
            if(g[i][j] > lx[i])
                lx[i] = g[i][j];
        }
    }
    for(int i=1;i<=nx;i++)
    {
        while(1)
        {
            me(visx,false);
            me(visy,false);
            d = INF;
            if(Find(i)) break;
            //若成功(找到了增广轨),则该点增广完成,进入下一个点的增广
            //若失败(没有找到增广轨),则需要改变一些点的标号,使得图中可行边的数量增加。
            //方法为:将所有在增广轨中(就是在增广过程中遍历到)的X方点的标号全部减去一个常数d,
            //所有在增广轨中的Y方点的标号全部加上一个常数d
           // if(d == INF) return -1;    //如果可以确定左边的点都会被匹配完,则就可以不用加这条语
 //句.    如果不能就要加上这句话. (所以一般在题目中给了一定完美匹配的话,就可以不用这句话)
 //而有些题目会问你是否是完美匹配,不是的话输出-1,是的话在输出答案,所以这个时候就要加上这句话.
 //大多数题目是可以不用加的.
            for(int j = 1 ; j<=nx ; j++)
                if(visx[j]) lx[j] -= d;
            for(int j = 1 ; j<=ny ; j++)
                if(visy[j]) ly[j] += d;
        }
    }
    int res = 0;
    for(int i=1;i<=ny;i++)
    {
        if(link[i] != -1)   //这些都根据题意来加.
            res += g[link[i]][i];
    }
    return res;
}    //这道题是保证了一定有最大二分匹配的!!! 所以一些东西可以去掉.
int k,u;
map<string,int>mp1,mp2;
char s1[25],s2[25];
void solve()
{
    while(scanf("%d%d%d",&nx,&ny,&k)!=EOF)
    {
        mp1.clear();mp2.clear();
        int cnt1=1,cnt2=1;
        for(int i=1;i<=nx;i++)
            for(int j=1;j<=ny;j++)
                g[i][j]=-INF;
        for(int i=1;i<=k;i++)
        {
            scanf("%s",s1);
            scanf("%s",s2);
            scanf("%d",&u);
            if(!mp1[s1]) mp1[s1]=cnt1++;
            if(!mp2[s2]) mp2[s2]=cnt2++;
            g[mp1[s1]][mp2[s2]]=-u;
        }
        cout<<-KM()<<endl;
    }
}


int main()
{
    int cas;
    cas=1;
    //cin>>cas;
    while(cas--)
        solve();
}


One fihgt one

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


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   |   We have carefully selected several similar problems for you:   2811  2816  2809  2810  2812 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值