匈牙利匹配 fzuoj 2232 炉石传说

来源:互联网 发布:objective-c和java 编辑:程序博客网 时间:2024/04/28 02:53

Problem Description

在简化版的炉石传说中:

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

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

Input

第一行为T,表示有T组数据。T<=100。

每组数据第一行为n,表示随从数量(1 <= n <= 100)

接下来一行2 * n个数字a1, b1, a2, b2, ... , an, bn (1 <= ai, bi <= 100)

表示GG学长的n个随从,ai表示随从生命,bi表示随从攻击力

接下来一行2 * n个数字c1, d1, c2, d2, ... , cn, dn (1 <= ci, di <= 100)

表示对手的n个随从,ci表示随从生命,di表示随从攻击力。

Output

每组数据,根据GG是否能完成他的目标,输出一行”Yes”或”No”。

Sample Input

2
3
4 4 5 5 6 6
1 1 2 2 3 3
3
4 4 5 5 6 6
1 4 2 4 3 4

Sample Output

Yes
No

典型的匈牙利算法:
代码:
#include <cstdio>#include <cstdlib>#include <ctype.h>#include <string>#include <iostream>#include <climits>#include <cmath>#include <set>#include <map>#include <vector>#include <string.h>#include <algorithm>#define ll long longusing namespace std;const int N = 1007;struct solder{    int f, a;};solder x[N], y[N];bool mp[N][N], mk[N];int match[N], n;bool dfs(int s){    for(int i=0; i<n; i++)    {        if(mp[s][i] && mk[i] == false)        {            mk[i] = true;            if(match[i] == -1 || dfs(match[i]))            {                match[i] = s;                return true;            }        }    }    return false;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        for(int i=0; i<n; i++)            scanf("%d%d",&x[i].f, &x[i].a);        for(int i=0; i<n; i++)            scanf("%d%d",&y[i].f, &y[i].a);        memset(mp, false, sizeof(mp));        for(int i=0; i<n; i++)        {            for(int j=0; j<n; j++)            {                if(x[i].f > y[j].a && x[i].a >= y[j].f)                    mp[i][j] = true;            }        }        memset(match, -1, sizeof(match));        int ans = 0;        for(int i=0; i<n; i++)        {            memset(mk, false, sizeof(mk));            if(dfs(i))                ans ++;        }        if(ans == n)            printf("Yes\n");        else            printf("No\n");    }}


0 0