二分图最大匹配 (hdu1281、1528)

(hdu1281)棋盘游戏

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2544    Accepted Submission(s): 1482


Problem Description
小希和Gardon在玩一个游戏:对一个N*M的棋盘,在格子里放尽量多的一些国际象棋里面的“车”,并且使得他们不能互相攻击,这当然很简单,但是Gardon限制了只有某些格子才可以放,小希还是很轻松的解决了这个问题(见下图)注意不能放车的地方不影响车的互相攻击。 
所以现在Gardon想让小希来解决一个更难的问题,在保证尽量多的“车”的前提下,棋盘里有些格子是可以避开的,也就是说,不在这些格子上放车,也可以保证尽量多的“车”被放下。但是某些格子若不放子,就无法保证放尽量多的“车”,这样的格子被称做重要点。Gardon想让小希算出有多少个这样的重要点,你能解决这个问题么?
 

Input
输入包含多组数据, 
第一行有三个数N、M、K(1<N,M<=100 1<K<=N*M),表示了棋盘的高、宽,以及可以放“车”的格子数目。接下来的K行描述了所有格子的信息:每行两个数X和Y,表示了这个格子在棋盘中的位置。
 

Output
对输入的每组数据,按照如下格式输出: 
Board T have C important blanks for L chessmen.
 

Sample Input
   
   
3 3 4 1 2 1 3 2 1 2 2 3 3 4 1 2 1 3 2 1 3 2
 

Sample Output
   
   
Board 1 have 0 important blanks for 2 chessmen. Board 2 have 3 important blanks for 3 chessmen.
 

题意:棋盘中一些格子可以放棋子,但要求棋子不能同行也不能同列。有一些格子若不放棋子,则最大匹配数目减少,则这个格子就是“重要点

二分图的匹配:给定一个二分图G,M为G边集的一个子集,如果M满足当中的任意两条边都不依附于同一个顶点,则称M是一个匹配。 

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"algorithm"
using namespace std;
#define N 105
int g[N][N];
int mark[N],link[N],n,m;
int find(int k)
{
    int i;
    for(i=1;i<=m;i++)
    {
        if(g[k][i]&&!mark[i])
        {
            mark[i]=1;
            if(link[i]==-1||find(link[i]))
            {
                link[i]=k;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int i,j,u,v,k,T=1;
    while(scanf("%d%d%d",&n,&m,&k)!=-1)
    {
        memset(link,-1,sizeof(link));
        memset(g,0,sizeof(g));
        while(k--)
        {
            scanf("%d%d",&u,&v);
            g[u][v]=1;
        }
        int ans=0,cnt=0;
        for(i=1;i<=n;i++)
        {
            memset(mark,0,sizeof(mark));
            ans+=find(i);
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                if(g[i][j])
                {
                    g[i][j]=0;
                    memset(link,-1,sizeof(link));
                    int t=0;
                    for(k=1;k<=n;k++)
                    {
                        memset(mark,0,sizeof(mark));
                        t+=find(k);
                    }
                    g[i][j]=1;
                    if(t<ans)
                        cnt++;
                }
            }
        }
        printf("Board %d have %d important blanks for %d chessmen.\n",T++,cnt,ans);
    }
    return 0;
}

(hdu1528、1962)Card Game Cheater

Card Game Cheater

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1309    Accepted Submission(s): 695


Problem Description
Adam and Eve play a card game using a regular deck of 52 cards. The rules are simple. The players sit on opposite sides of a table, facing each other. Each player gets k cards from the deck and, after looking at them, places the cards face down in a row on the table. Adam’s cards are numbered from 1 to k from his left, and Eve’s cards are numbered 1 to k from her right (so Eve’s i:th card is opposite Adam’s i:th card). The cards are turned face up, and points are awarded as follows (for each i ∈ {1, . . . , k}):


If Adam’s i:th card beats Eve’s i:th card, then Adam gets one point.


If Eve’s i:th card beats Adam’s i:th card, then Eve gets one point.


A card with higher value always beats a card with a lower value: a three beats a two, a four beats a three and a two, etc. An ace beats every card except (possibly) another ace.


If the two i:th cards have the same value, then the suit determines who wins: hearts beats all other suits, spades beats all suits except hearts, diamond beats only clubs, and clubs does not beat any suit. 

For example, the ten of spades beats the ten of diamonds but not the Jack of clubs. 

This ought to be a game of chance, but lately Eve is winning most of the time, and the reason is that she has started to use marked cards. In other words, she knows which cards Adam has on the table before he turns them face up. Using this information she orders her own cards so that she gets as many points as possible.

Your task is to, given Adam’s and Eve’s cards, determine how many points Eve will get if she plays optimally. 

 

Input
There will be several test cases. The first line of input will contain a single positive integer N giving the number of test cases. After that line follow the test cases.

Each test case starts with a line with a single positive integer k <= 26 which is the number of cards each player gets. The next line describes the k cards Adam has placed on the table, left to right. The next line describes the k cards Eve has (but she has not yet placed them on the table). A card is described by two characters, the first one being its value (2, 3, 4, 5, 6, 7, 8 ,9, T, J, Q, K, or A), and the second one being its suit (C, D, S, or H). Cards are separated by white spaces. So if Adam’s cards are the ten of clubs, the two of hearts, and the Jack of diamonds, that could be described by the line

TC 2H JD
 

Output
For each test case output a single line with the number of points Eve gets if she picks the optimal way to arrange her cards on the table.

 

Sample Input
   
   
3 1 JD JH 2 5D TC 4C 5H 3 2H 3H 4H 2D 3D 4D
 

Sample Output
   
   
1 1 2
 


题意:相当于田忌赛马的游戏,只不过换成扑克牌。比较点数大小,点数相同按花色分大小,红桃》黑桃》方块》梅花。

思路:把每张牌和一个数字映射起来,使得映射后的大小和牌中一样。然后建图,把 Eve的牌大于Adam的牌建一条边。

然后,求出最大匹配就OK了。

#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#define N 60
int a[N],b[N],g[N][N];
int mark[N],link[N];
int getch(char*s)    //把牌数转化为数字
{
    int t;
    switch(s[0])
    {
        case 'T':t=9;break;
        case 'J':t=10;break;
        case 'Q':t=11;break;
        case 'K':t=12;break;
        case 'A':t=13;break;
        default :t=s[0]-'0'-1;break;
    }
    t*=4;
    if(s[1]=='S')
        t-=1;
    else if(s[1]=='D')
        t-=2;
    else if(s[1]=='C')
        t-=3;
    //printf("%d\n",t);
    return t;
}
int find(int k)       //匈牙利算法求最大匹配数目
{
    int i;
    for(i=1;i<N;i++)
    {
        if(g[k][i]&&!mark[i])
        {
            mark[i]=1;
            if(link[i]==-1||find(link[i]))
            {
                link[i]=k;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    int i,j,t,n,n1,n2,T;
    char s[5];
    scanf("%d",&T);
    while(T--)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(g,0,sizeof(g));
        scanf("%d",&n);
        n1=n2=0;
        for(i=0;i<n;i++)
        {
            scanf("%s",s);
            t=getch(s);
            a[n1++]=t;
        }
        for(i=0;i<n;i++)
        {
            scanf("%s",s);
            t=getch(s);
            b[n2++]=t;
        }
        for(i=0;i<n1;i++)      //建图
        {
            for(j=0;j<n2;j++)
            {
                if(a[i]<b[j])
                    g[b[j]][a[i]]=1;
            }
        }
        int ans=0;
        memset(link,-1,sizeof(link));
        for(i=1;i<N;i++)
        {
            memset(mark,0,sizeof(mark));
            ans+=find(i);     
        }
        printf("%d\n",ans);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值