Miku and Generals

Describe

“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 \le T \le 10)\)which is the number of test cases.

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

The second line contains \(N\) integers, and the iii-th integer is \(c_i\), which is the attack power value of the iii-th general \((0 \le c_i \le 5\times 10^4)\).

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

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个数,把他们分成两组使得差值最小,求较大那组权值和。有些数不能在一起,保证有解。

题解

先二分图染色,也可以并查集(我考场用并查集写的,结果WA到结束,所以对并查集产生了阴影qwq)。

然后就是一个比较裸的可行性背包,背包容量为sum/2。

如果不会背包,可以先做这道相似的背包题:https://www.luogu.org/problemnew/show/P1282

(我一开始统计方案数,一直WA,原来是方案数报爆int 了,真是伤心,还是fwl大佬看出来的)

代码

#include<bits/stdc++.h>
using namespace std;
#define N 205
#define M 205
#define V 50050
int n,m,a[N],f[N],val[N][3],bh[N],dp[2][V];
int tot,last[N];
struct Edge{int from,to,s;}edges[M<<1];
template<typename T> void read(T &x)
{
int k=0; char c=getchar();
x=0;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit(0);
while(isdigit(c))x=x*10+c-'0',c=getchar();
x=k?-x:x;
}
void AddEdge(int x,int y)
{
edges[++tot]=Edge{x,y,last[x]};
last[x]=tot;
}
void dfs(int x,int id)
{
f[x]=1;
bh[x]=id;
for(int i=last[x];i;i=edges[i].s)
{
Edge &e=edges[i];
if (!f[e.to])dfs(e.to,-id);
}
}
void work()
{
memset(f,0,sizeof(f));
memset(val,0,sizeof(val));
memset(dp,0,sizeof(dp));
memset(last,0,sizeof(last));//
tot=0;
int sum=0,num=0;
read(n); read(m);
for(int i=1;i<=n;i++)read(a[i]),a[i]/=100,sum+=a[i];
for(int i=1;i<=m;i++)
{
int x,y;
read(x); read(y);
AddEdge(x,y);
AddEdge(y,x);
}
for(int i=1;i<=n;i++)if (!f[i])dfs(i,++num);
for(int i=1;i<=n;i++)val[abs(bh[i])][bh[i]/abs(bh[i])+1]+=a[i];
int selfsum=sum/2,k=0;
dp[k][0]=1;
for(int i=1;i<=num;i++)
{
k^=1;
for(int j=selfsum;j>=0;j--)
{
if (j-val[i][0]>=0&&dp[1-k][j-val[i][0]]>0) dp[k][j]=1;
else
if (j-val[i][2]>=0&&dp[1-k][j-val[i][2]]>0) dp[k][j]=1;
else dp[k][j]=0;
if (i==num&&dp[k][j]>0){printf("%d\n",max(j,(sum-j))*100);return;}
}
} }
int main(){
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
int q;read(q); while(q--)work();}

西安区域赛 D.Miku and Generals 二分图+背包的更多相关文章

  1. 2017西安区域赛A / UVALive - 8512 线段树维护线性基合并

    题意:给定\(a[1...n]\),\(Q\)次询问求\(A[L...R]\)的异或组合再或上\(K\)的最大值 本题是2017的西安区域赛A题,了解线性基之后你会发现这根本就是套路题.. 只要用线段 ...

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

    “Miku is matchless in the world!” As everyone knows, Nakano Miku is interested in Japanese generals, ...

  3. 2019 ACM/ICPC Asia Regional shanxia D Miku and Generals (二分图黑白染色+01背包)

    Miku is matchless in the world!” As everyone knows, Nakano Miku is interested in Japanese generals, ...

  4. UVALive 8519 Arrangement for Contests 2017西安区域赛H 贪心+线段树优化

    题意 等价于给一个数列,每次对一个长度为$K$的连续区间减一 为最多操作多少次 题解: 看样例猜的贪心,10分钟敲了个线段树就交了... 从1开始,找$[i,i+K]$区间的最小值,然后区间减去最小值 ...

  5. UVALive 8513 lovers 2017 西安区域赛 B 贪心+multiset

    UVALive 8513 有2种人,每个人有自己的权值$A_i$ $B_i$ 当$A_i + B_i >=K$时 两个人可以配对 问最多多少人可以配对 解法 : 把$/{ A_i /}$ 排序 ...

  6. 2014年西安区域赛的几道水题(A. F. K)

    A . 问一组数能否全部被3整除 K. S1 = A, S2 = B, Si = |Si-1  -  Si-2|; 一直循环问, 出现了多少不同的数: 多模拟几组数, 可以发现和辗转相除法有很大关系 ...

  7. 14西安区域赛C - The Problem Needs 3D Arrays

    最大密度子图裸题,详情请见胡博涛论文: https://wenku.baidu.com/view/986baf00b52acfc789ebc9a9.html 不加当前弧优化t到死= = //#prag ...

  8. 2017 ICPC西安区域赛 A - XOR (线段树并线性基)

    链接:https://nanti.jisuanke.com/t/A1607 题面:   Consider an array AA with n elements . Each of its eleme ...

  9. 2019 ICPC 上海区域赛总结

    2019上海区域赛现场赛总结 补题情况(以下通过率为牛客提交): 题号 标题 已通过代码 通过率 我的状态 A Mr. Panda and Dominoes 点击查看 5/29 未通过 B Prefi ...

随机推荐

  1. nodejs基础 用http模块 搭建一个简单的web服务器 响应纯文本

    首先说一下,我们平时在浏览器上访问网页,所看到的内容,其实是web服务器传过来的,比如我们访问www.baidu.com.当我们在浏览器地址栏输入之后,浏览器会发送请求到web服务器,然后web服务器 ...

  2. 内存管理4-Autoreleasepool

    自动释放池是OC里面的一种内存回收机制,一般可以将一些临时变量添加到自动释放池中,统一回收释放,当自动释放池销毁时,池里面的所有对象都会调用一次release,也就是计数器会减1,但是自动释放池被销毁 ...

  3. <cmath>库函数

    C++ cmath库中的函数 今天模拟,想调用<cmath>中的函数,然鹅...突然忘了,所以还是总结一下吧 写法 作用 int abs(int i) 返回整型参数i的绝对值 double ...

  4. ubuntu下安装g++

    主要来自ubuntu中文社区http://www.ubuntu.org.cn/support/documentation/doc/VMware 首选,确认你已经安装了build-essential程序 ...

  5. mysql端口3306无法访问

    mysql主备复制,show slave status显示IO一直connecting 一.查看了防火墙,已经处于关闭状态 二.查看使用的复制用户的权限,也已经开放 三.telnet访问另外一台机器端 ...

  6. OpenCV学习笔记(2)——如何用OpenCV处理视频

    如何用OpenCV处理视频 读取视频文件,显示视频,保存视频文件 从摄像头获取并显示视频 1.用摄像头捕获视频 为了获取视频,需要创建一个VideoCapature对象.其参数可以是设备的索引号,也可 ...

  7. 利用sorket实现聊天功能-服务端实现

    工具包 package loaderman.im.util; public class Constants { public static final String SERVER_IP = " ...

  8. kotlin使用中辍标记法调用函数

    fun main(arg: Array<String>) { var str = "hello world" print(str div("l")) ...

  9. C# Socket TcpClient 无法从传输连接中读取数据: 远程主机强迫关闭了一个现有的连接。。

    开始的代码: byte[] data = Encoding.UTF8.GetBytes(sInfo);                    tcpns.Write(data, 0,1024); 修改 ...

  10. 阶段5 3.微服务项目【学成在线】_day05 消息中间件RabbitMQ_17.RabbitMQ研究-与springboot整合-消费者代码

    创建消费者的类 使用@Component把这个类标记成一个Bean 把生产者里面创建的配置文件类复制过来 在原始的消费的方法上面 ,我们是可以拿到channel通道的 message.getBody就 ...