Harmonious Army
Harmonious Army
Now, Bob is playing an interesting game in which he is a general of a harmonious army. There are n soldiers in this army. Each soldier should be in one of the two occupations, Mage or Warrior. There are m pairs of soldiers having combination ability. There are three kinds of combination ability. If the two soldiers in a pair are both Warriors, the army power would be increased by a. If the two soldiers in a pair are both Mages, the army power would be increased by c. Otherwise the army power would be increased by b, and b=a/4+c/3, guaranteed that 4|a and 3|c. Your task is to output the maximum power Bob can increase by arranging the soldiers' occupations.
Note that the symbol a|b means that a divides b, e.g. , 3|12 and 8|24.Input
There are multiple test cases.
Each case starts with a line containing two positive integers n(n≤500) and m(m≤\(10^4\)).
In the following m lines, each line contains five positive integersu,v,a,b,c (1≤u,v≤n,u≠v,1≤a,c≤4×\(10^6\),b=a/4+c/3), denoting soldiers u and v have combination ability, guaranteed that the pair (u,v) would not appear more than once.
It is guaranteed that the sum of n in all test cases is no larger than 5×\(10^3\), and the sum of m in all test cases is no larger than 5×\(10^4\).Output
For each test case, output one line containing the maximum power Bob can increase by arranging the soldiers' occupations.
Sample Input
3 2
1 2 8 3 3
2 3 4 3 6Sample Output
12
思路:
把\(s\)连向\(u,v\)连容量为\(\frac{a+b}{2}\)的有向边\(,\)把\(u,v\)向\(t\)连容量为\(\frac{b+c}{2}\)的有向边\(,\)在\(u,v\)间连条\(\frac{a+c}{2}-b\),用\(\sum_{i=1}^n(a_i+b_i+c_i)\)减最大流
如图:
证明:
(这张图找了半天)
a+b=A+B&\\
c+d=B+C&\\
a+e+d=b+e+c=A+C
\end{cases}\]
其中一组解为
a=b=\frac{A+B}{2}&\\
c=d=\frac{B+C}{2}&\\
e=\frac{A+C}{2}-B
\end{cases}\]
便得到了上面建边的由来
\(\mathfrak{Talk\ is\ cheap,show\ you\ the\ code.}\)
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
# define read read1<int>()
# define Type template<typename T>
Type inline T read1(){
T n=0;
char k;
bool fl=0;
do (k=getchar())=='-'&&(fl=1);while('9'<k||k<'0');
while(47<k&&k<58)n=(n<<3)+(n<<1)+(k^48),k=getchar();
return fl?-n:n;
}
class Flow{
# define Big 502
# define Big2 70000
# define Imax 2e9
private:
int cur[Big+1],q[Big+1],lev[Big+1];
class Pic{
public:
class Edge{
public:
int u,t;
double v;
}G[Big2<<1|1];
int f[Big+1],tot;
Pic(){for(int i=0;i<=Big;++i)f[i]=0;tot=0;}
void add(int u,int v,double t){
G[++tot].u=v;
G[tot].t=f[u];
f[u]=tot;
G[tot].v=t;
}
}G;
public:
int S,T,n;
void Clear(){memset(G.f,0,sizeof(G.f));G.tot=0;}
Flow(){n=Big;}
Flow(int big,int s,int t){n=big;S=s;T=t;}
void Into(int s,int t){S=s;T=t;}
void add(int u,int v,double t){
G.add(u,v,t);G.add(v,u,0);
}
bool bfs(){
for(int i=0;i++^n;lev[i]=0);
lev[S]=1;int t=0;
q[t++]=S;
for(int i=0;i^t;++i){
int u=q[i];
//printf("->%d\n",u);
for(int i=G.f[u];i;i=G.G[i].t){
if(!lev[G.G[i].u]&&G.G[i].v>0){
int to=G.G[i].u;
lev[to]=lev[u]+1;
q[t++]=to;
if(to==T)return 1;
}
}
}
return 0;
}
double dfs(int u,double f){
if(u==T)return f;
double tag=0,c;
for(int &i=cur[u];i;i=G.G[i].t){
int to=G.G[i].u;
if(G.G[i].v>0&&lev[to]==lev[u]+1){
c=dfs(to,min(f-tag,G.G[i].v));
G.G[i].v-=c;
G.G[(i-1^1)+1].v+=c;
tag+=c;
if(tag==f)return tag;
}
}
return tag;
}
double Dinic(){
double ans=0;
while(bfs()){
for(int i=0;i++^n;)cur[i]=G.f[i];
ans+=dfs(S,2e9);
}
return ans;
}
# undef Big
# undef Big2
}G;
# define fre(k) freopen(k".in","r",stdin);freopen(k".out","w",stdout)
# define f(i,l,r) for(int i=l;i<=r;++i)
int main(){
G.Into(1,2);
int n,m;
while(~scanf("%d %d",&n,&m)){
double t=0;
G.Clear();
for(int i=0;i++^m;){
int u=read,v=read;
double A=read,B=read,C=read;
t+=A+B+C;
G.add(1,u+2,(A+B)/2);
G.add(1,v+2,(A+B)/2);
G.add(u+2,2,(B+C)/2);
G.add(v+2,2,(B+C)/2);
G.add(u+2,v+2,(A+C)/2-B);
G.add(v+2,u+2,(A+C)/2-B);
}
printf("%.0f\n",t-G.Dinic());
}
return 0;
}
Harmonious Army的更多相关文章
- 2019年杭电多校第二场 1008题Harmonious Army(HDU6598+最小割+建图)
题目链接 传送门 题意 有\(n\)个士兵,要你给他们分配职业.有\(m\)对关系,对于某一对关系\(u,v\),如果同为勇士则总能力增加\(a\),同法师则增加\(c\),一个勇士一个法师增加\(\ ...
- Hdu 6598 Harmonious Army 最小割
N个人 每个人可以是战士/法师 M个组合 每个组合两个人 同是战士+a 同是法师+c 否则+b 对于每一个u,v,a,b,c 建(S,u,a) (u,v,a+c-2*b) (v,T,c) (S,v, ...
- 2019HDU多校赛第二场 H HDU 6598 Harmonious Army(最小割模型)
参考博客https://blog.csdn.net/u013534123/article/details/97142191 #include<bits/stdc++.h> using na ...
- [2019杭电多校第二场][hdu6598]Harmonious Army(最小割)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6598 题意是说一个军队有n人,你可以给他们每个人安排战士或者法师的职业,有m对人有组合技,组合技的信息 ...
- 2019 Multi-University Training Contest 2 - 1008 - Harmonious Army - 最大流
http://acm.hdu.edu.cn/showproblem.php?pid=6598 一开始就觉得是网络流,但是一直都不会怎么建图. 这里要考虑. 每一组边(u,v,a,b,c)建立如下的连接 ...
- hdu多校第二场1008(hdu6598) Harmonious Army 最小割
题意: 一个军队有n人,你可以给他们每个人安排战士或者法师的职业,有m对人有组合技,组合技的信息是a,b,c,代表如果这两个人是两个战士,则组合技威力为a,一个战士一个法师,威力为b,其中b=a/4+ ...
- 【HDOJ6598】Harmonious Army(最小割)
题意:有n个人,每个人可以从A,B两种职业中选择一种 有m对两人组,如果两个人都是A能获得p的收益,一个A一个B能获得q的收益,都是B能获得r的收益,其中q=p/4+r/3,保证p%4=0,r%3=0 ...
- 2019 HDU 多校赛第二场 HDU 6598 Harmonious Army 构造最小割模型
题意: 有n个士兵,你可以选择让它成为战士还是法师. 有m对关系,u和v 如果同时为战士那么你可以获得a的权值 如果同时为法师,你可以获得c的权值, 如果一个为战士一个是法师,你可以获得b的权值 问你 ...
- @hdu - 6598@ Harmonious Army
目录 @description@ @solution@ @accepted code@ @details@ @description@ n 个士兵,每个士兵可以选择加入 A 组或 B 组. 有 m 个 ...
随机推荐
- Java生鲜电商平台-库存管理设计与架构
Java生鲜电商平台-库存管理设计与架构 WMS的功能: 1.业务批次管理 该功能提供完善的物料批次信息.批次管理设置.批号编码规则设置.日常业务处理.报表查询,以及库存管理等综合批次管理功能,使企业 ...
- Android App自动化测试实战(基于Python)(三)
1.Native App自动化测试及Appuim框架介绍 android平台提供了一个基于java语言的测试框架uiautomator,它一个测试的Java库,包含了创建UI测试的各种API和执行自动 ...
- Java自学-集合框架 泛型Generic
ArrayList上使用泛型 步骤 1 : 泛型 Generic 不指定泛型的容器,可以存放任何类型的元素 指定了泛型的容器,只能存放指定类型的元素以及其子类 package property; pu ...
- PHP MySQLi 参考手册
PHP MySQLi函数 PHP MySQLi是MySQL的增强版本,PHP7 已经废弃了MySQL扩展,全面推荐使用MySQLi或者PDO. MySQLi安装>>>>> ...
- redis笔记1
存储结构 字符类型 散列类型 列表类型 集合类型 有序集合 可以为每个key设置超时时间: 可以通过列表类型来实现分布式队列的操作 支持发布订阅的消息模式 提供了很多命令与redis进行交互 数据缓存 ...
- Excel解析工具easyexcel全面探索
1. Excel解析工具easyexcel全面探索 1.1. 简介 之前我们想到Excel解析一般是使用POI,但POI存在一个严重的问题,就是非常消耗内存.所以阿里人员对它进行了重写从而诞生了eas ...
- SQLServer修改表名、修改列名
基本语法 修改表名:EXEC sp_rename ‘原有表名’, '新表名'; 修改列名:EXEC sp_rename ‘表名.[原有列名]’, ‘新列名' , 'COLUMN'; EXEC sp_r ...
- Fundebug后端Node.js插件更新至0.2.0,支持监控Express慢请求
摘要: 性能问题也是BUG,也需要监控. Fundebug后端Node.js异常监控服务 Fundebug是专业的应用异常监控平台,我们Node.js插件fundebug-nodejs可以提供全方位的 ...
- Centos7部署ejforum论坛(Java+tomcat+mysql)
前面搭建Java环境和tomcat环境. 下面进行实战,搭建ejforum论坛 ejforum论坛源码:https://www.lanzous.com/i45rcoh Centos7安装MySQL数据 ...
- kolla部署openstack allinone,报错 ImportError: cannot import name decorate
使用 kolla-ansible 部署 opnenstack:stein,最后无法导入变量脚本,报错信息如下: [root@kolla ~]# . /etc/kolla/admin-openrc.sh ...