经典易懂的二分图最大匹配法, 一个炉石告诉你

Description

If you have played Hearthstone, you will figure out this problem easily.

Zhoulaohui is a shut-in so he play Hearthstone everyday. But on the deadline, if he do not study, he would not pass the final exam. So he decides to make out a water problem. He developed a simple Hearthstone and played well. Meanwhile he seemed to have some kind of compulsive disorder that he wants to wipe out all opponents and do not want his entourages to die.

One entourage or opponent has his health point and attack. If A fell on B, A’s health point will minus B’s attack and B’s health point will minus A’s attack. If someone’s health point is not greater than zero, his life will not exist. And one Zhoulaohui’s entourage only can fell on one opponent.

Now the number of Zhoulaohui’s entourages is equal to his opponents, he wants to know whether he can do it. Can you help him?

Input

the first line is the number of test cases T. (T<=100)

for each test case, first line will be an integer N. (1<=N<=100)

Following N lines, each line consists of 2 integers, Zhoulaohui’s entourage’s health point X and attack Y. (1<=X,Y<=100)

Then follow N lines, each line consists of 2 integers, Zhoulaohui’s opponent’s health point X and attack Y. (1<=X,Y<=100)

Output

If you think Zhoulaohui can achieve his goal, output “Sorry about that!”(without quotes).

Otherwise, output “Tell you a joke~”(without quotes).

Sample Input

134 45 56 61 12 23 3

Sample Output

Sorry about that!

题目大意 每个随从只有生命值和攻击力,并且在你的回合下,你的每只随从在本回合下只能选择一个敌方随从进行攻击。当两个随从a,b交战时,a的生命值将减去b的攻击力,b的生命值将减去a的攻击力,(两个伤害没有先后顺序,同时结算)。如果a或b的生命值不大于0,该随从将死亡。

某一次对局中, Zhoulaohui和对手场面上均有n个随从,并且是 Zhoulaohui学长的回合。由于 Zhoulaohui学长是个固执的boy,他一定要在本回合杀死对方所有随从,并且保证自己的随从全部存活。他想知道能否做到。

借鉴大佬的经典

匈牙利算法:

匈牙利算法几乎是二分图匹配的核心算法,除了二分图多重匹配外均可使用

匈牙利算法实际上就是一种网络流的思想,其核心就是寻找增广路。具体操作就是嗯。。拉郎配

注:以下转自 http://blog.csdn.net/dark_scope/article/details/8880547

匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名。匈牙利算法是基于Hall定理中充分性证明的思想,它是部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图最大匹配的算法。

-------等等,看得头大?那么请看下面的版本:

通过数代人的努力,你终于赶上了剩男剩女的大潮,假设你是一位光荣的新世纪媒人,在你的手上有N个剩男,M个剩女,每个人都可能对多名异性有好感(惊讶-_-||暂时不考虑特殊的性取向),如果一对男女互有好感,那么你就可以把这一对撮合在一起,现在让我们无视掉所有的单相思(好忧伤的感觉快哭了),你拥有的大概就是下面这样一张关系图,每一条连线都表示互有好感。


本着救人一命,胜造七级浮屠的原则,你想要尽可能地撮合更多的情侣,匈牙利算法的工作模式会教你这样做:

===============================================================================

 先试着给1号男生找妹子,发现第一个和他相连的1号女生还名花无主,got it,连上一条蓝线


===============================================================================

接着给2号男生找妹子,发现第一个和他相连的2号女生名花无主,got it


===============================================================================

接下来是3号男生,很遗憾1号女生已经有主了,怎么办呢?

我们试着给之前1号女生匹配的男生(也就是1号男生)另外分配一个妹子。

(黄色表示这条边被临时拆掉)

与1号男生相连的第二个女生是2号女生,但是2号女生也有主了,怎么办呢?我们再试着给2号女生的原配(发火发火)重新找个妹子(注意这个步骤和上面是一样的,这是一个递归的过程)


此时发现2号男生还能找到3号女生,那么之前的问题迎刃而解了,回溯回去

2号男生可以找3号妹子~~~                  1号男生可以找2号妹子了~~~                3号男生可以找1号妹子

所以第三步最后的结果就是:


===============================================================================

 接下来是4号男生,很遗憾,按照第三步的节奏我们没法给4号男生出来一个妹子,我们实在是无能为力了……香吉士同学走好。

===============================================================================

这就是匈牙利算法的流程,其中找妹子是个递归的过程,最最关键的字就是“ ”字

其原则大概是:有机会上,没机会创造机会也要上

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
using namespace std;
int mapp[105][105];
int n;
int visit[105];
int match[105];
struct per
{
    int x;
    int y;
}a[105], b[105];
int dfs( int x )
{
    int i;
    for( i = 0; i<n; i++)
    {
        if( mapp[x][i] == 1 && !visit[i] )  两者也有好感 而且不是和我匹配过的妹子
        {
            visit[i] =1;
            int t = match[i];
            if( t== -1 || dfs( t ))  
            {
                match[i] = x;  hhh  i 属于x我了

                return 1;
            }
        }
    }
    return 0;
}
int solve()
{
    int  i ;
    for( i = 0; i<n; i++)
    {
      match[i]  = -1;
    }
    int cnt = 0 ;
    for( i = 0; i<n; i++)
    {
        memset(visit, 0 , sizeof( visit));
        if( dfs(i ))
            cnt++;
    }
    return cnt;
}
int main()
{
    int t;
    scanf("%d", &t);
    while( t-- )
    {
        int  i, j;
        memset( mapp, 0, sizeof( mapp ));
        memset( match, 0, sizeof( match));
        scanf("%d", &n);
        for( i  = 0; i<n; i++)
        {
            scanf("%d%d", &a[i].x , &a[i].y);
        }
        for(i = 0; i<n; i++)
        {
            scanf("%d%d", &b[i].x, &b[i].y );
        }
        for( i = 0 ; i<n; i++)
        {
            for( j = 0; j<n; j++)
            {
                if( a[i].x > b[j].y && a[i].y >= b[j].x)
                    mapp[i][j]  = 1;  两者匹配
            }
        }
        if( solve( ) == n)
            printf("Sorry about that!\n");
        else
            printf("Tell you a joke~\n");
    }


}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值