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 6

Sample 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)\)减最大流
如图:

证明:



(这张图找了半天)

\[\begin{cases}
a+b=A+B&\\
c+d=B+C&\\
a+e+d=b+e+c=A+C
\end{cases}\]

其中一组解为

\[\begin{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的更多相关文章

  1. 2019年杭电多校第二场 1008题Harmonious Army(HDU6598+最小割+建图)

    题目链接 传送门 题意 有\(n\)个士兵,要你给他们分配职业.有\(m\)对关系,对于某一对关系\(u,v\),如果同为勇士则总能力增加\(a\),同法师则增加\(c\),一个勇士一个法师增加\(\ ...

  2. 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, ...

  3. 2019HDU多校赛第二场 H HDU 6598 Harmonious Army(最小割模型)

    参考博客https://blog.csdn.net/u013534123/article/details/97142191 #include<bits/stdc++.h> using na ...

  4. [2019杭电多校第二场][hdu6598]Harmonious Army(最小割)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6598 题意是说一个军队有n人,你可以给他们每个人安排战士或者法师的职业,有m对人有组合技,组合技的信息 ...

  5. 2019 Multi-University Training Contest 2 - 1008 - Harmonious Army - 最大流

    http://acm.hdu.edu.cn/showproblem.php?pid=6598 一开始就觉得是网络流,但是一直都不会怎么建图. 这里要考虑. 每一组边(u,v,a,b,c)建立如下的连接 ...

  6. hdu多校第二场1008(hdu6598) Harmonious Army 最小割

    题意: 一个军队有n人,你可以给他们每个人安排战士或者法师的职业,有m对人有组合技,组合技的信息是a,b,c,代表如果这两个人是两个战士,则组合技威力为a,一个战士一个法师,威力为b,其中b=a/4+ ...

  7. 【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 ...

  8. 2019 HDU 多校赛第二场 HDU 6598 Harmonious Army 构造最小割模型

    题意: 有n个士兵,你可以选择让它成为战士还是法师. 有m对关系,u和v 如果同时为战士那么你可以获得a的权值 如果同时为法师,你可以获得c的权值, 如果一个为战士一个是法师,你可以获得b的权值 问你 ...

  9. @hdu - 6598@ Harmonious Army

    目录 @description@ @solution@ @accepted code@ @details@ @description@ n 个士兵,每个士兵可以选择加入 A 组或 B 组. 有 m 个 ...

随机推荐

  1. 读取txt文件内容,并按一定长度分页显示

    private List<string> SaveContentUpload(FileUpload file) { List<string> list_content = ne ...

  2. LinuxShell——内嵌命令

    LinuxShell——内嵌命令 摘要:本文主要学习了Shell的常用内嵌命令. alias命令 alias命令可以为指定命令定义一个别名. 基本语法 查看所有别名: alias 设置别名: alia ...

  3. Java开发设计——七大原则

    Java开发设计——七大原则 摘要:本文主要介绍了在做面向对象开发时要注意的七个原则. 部分内容来自以下博客: https://www.cnblogs.com/xiyuekamisama/p/1057 ...

  4. Mysql使用ReplicationDriver驱动实现读写分离

    数据库的主从复制环境已经配好,该要解决系统如何实现读写分离功能了.Mysql的jdbc驱动提供了一种实现ReplicationDriver. 1 数据库地址的两种写法 参考:https://dev.m ...

  5. js javascript map函数去重功能的使用实例

    js javascript map函数去重功能的使用实例 先上一个实战例子代码 var map = new Map(); for(var i=0; i<=9; i++){ map.set(i,i ...

  6. 微信小程序自定义tabbar的实现

    微信小程序自定义tabbar的实现 目的:当采用微信的自定义tabbar组件的时候,切换的时候会出现闪屏的效果:当使用微信默认的tabbar的时候,限制了tabbar的数量以及灵活配置. 方案:自己动 ...

  7. curl sftp libcurl 功能使用

    #include <curl/curl.h> #undef DISABLE_SSH_AGENT struct FtpFile { const char *filename; FILE *s ...

  8. Innodb整体架构

    如下图展示了Innodb内存中和磁盘的结构: 内存中结构主要有如下几种: buffer pool change buffer adaptive hash index (自适应的hash索引) Log ...

  9. Django 练习班级管理系统三 -- 分页

    在 user_manager 项目下创建一个 python package:utils,里面创建一个 page.py 文件 page.py 文件内容为: # 创建一个 PagerHelper 类 cl ...

  10. windows及linux环境下修改pip的默认镜像源的方法

    1. 在windows环境下 临时修改 使用清华大学的源安装numpy包. pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple ...