• ACM-ICPC 2019 西安邀请赛 D.Miku and Generals(二分图+可行性背包)


    “Miku is matchless in the world!” As everyone knows, Nakano Miku is interested in Japanese generals, so Fuutaro always plays a kind of card game about generals with her. In this game, the players pick up cards with generals, but some generals have contradictions and cannot be in the same side. Every general has a certain value of attack power (can be exactly divided by 100), and the player with higher sum of values will win. In this game all the cards should be picked up.

    This day Miku wants to play this game again. However, Fuutaro is busy preparing an exam, so he decides to secretly control the game and decide each card's owner. He wants Miku to win this game so he won't always be bothered, and the difference between their value should be as small as possible.To make Miku happy, if they have the same sum of values, Miku will win. He must get a plan immediately and calculate it to meet the above requirements, how much attack value will Miku have?

    As we all know, when Miku shows her loveliness, Fuutaro's IQ will become 0. So please help him figure out the answer right now!

    Input

    Each test file contains several test cases. In each test file:

    The first line contains a single integer T(1≤T≤10) which is the number of test cases.

    For each test case, the first line contains two integers: the number of generals N(2N200) and thenumber of pairs of generals that have contradictions⁡ M(0M200).

    The second line contains N integers, and the i-th integer is ci, which is the attack power value of the i-th general (0ci5×104).

    The following M lines describe the contradictions among generals. Each line contains two integers A and B , which means general A and B cannot be on the same side (1A,BN).

    The input data guarantees that the solution exists.

    Output

    For each test case, you should print one line with your answer.

    Hint

    In sample test case, Miku will get general 2 and 3.

    样例输入

    1
    4 2
    1400 700 2100 900 
    1 3
    3 4

    样例输出

    2800

    题意

    N个数选任意个给A,其余的给B,M对冲突,A和B里的数都不能冲突,问A和>=B和,并且使A-B差值最小,输出A的值。保证有解。

    题解

    由于A集合里的数不能冲突,考虑二分图01染色,对于每个连通块。

    差值为|0的个数-1的个数|,dp[i]表示A-B差值为i是否可行。

    知道差值i,总和sum,那么A=(sum+i)/2。

    代码

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 int a[205],color[205];
     5 bool dp[100005],dp1[100005];
     6 vector<int>G[205];
     7 int sumc[2];
     8 void dfs(int u,int col)
     9 {
    10     color[u]=col;
    11     sumc[col]+=a[u];
    12     for(int i=0;i<G[u].size();i++)
    13     {
    14         int v=G[u][i];
    15         if(color[v]==-1)
    16             dfs(v,col^1);
    17     }
    18 }
    19 int main()
    20 {
    21     int t;
    22     scanf("%d",&t);
    23     while(t--)
    24     {
    25         int n,m,u,v,sum=0;
    26         scanf("%d%d",&n,&m);
    27         for(int i=1;i<=n;i++)G[i].clear(),color[i]=-1;
    28         for(int i=1;i<=n;i++)scanf("%d",&a[i]),a[i]/=100,sum+=a[i];
    29         for(int i=1;i<=m;i++)
    30         {
    31             scanf("%d%d",&u,&v);
    32             G[u].push_back(v);
    33             G[v].push_back(u);
    34         }
    35         for(int i=0;i<=sum;i++)dp[i]=0;
    36         dp[0]=1;
    37         for(int i=1;i<=n;i++)
    38             if(color[i]==-1)
    39             {
    40                 sumc[0]=sumc[1]=0;
    41                 dfs(i,0);
    42                 int ca=abs(sumc[0]-sumc[1]);
    43                 for(int j=sum;j>=0;j--)
    44                 {
    45                     if(dp[j])
    46                     {
    47                         if(abs(j+ca)<=sum)dp1[abs(j+ca)]=1;
    48                         dp1[abs(j-ca)]=1;
    49                     }
    50                 }
    51                 for(int j=sum;j>=0;j--)
    52                     dp[j]=dp1[j],dp1[j]=0;
    53             }
    54         for(int i=0;i<=sum;i++)
    55             if(dp[i])
    56             {
    57                 printf("%d
    ",(sum+i)/2*100);
    58                 break;
    59             }
    60     }
    61     return 0;
    62 }
  • 相关阅读:
    POJ 1222 高斯消元更稳
    Maven与Eclipse使用中遇到的问题解决之道
    Java并发之任务的描述和执行
    XMemcached简单使用示例
    Java的容器类小结
    Java系统属性与Preferences API的简单介绍
    你知道自己执行的是哪个jre吗?
    Log4J配置文件详解
    Java日志工具之SLF4J
    Java日志工具之Log4J
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/10927641.html
Copyright © 2020-2023  润新知