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 个 ... 
随机推荐
- Flask--g属性
			目录 Flask之g属性 使用 session,flash,g的区别 Flask之g属性 专门用来存储用户信息的g对象,g的全称的为global g对象在一次请求中的所有的代码的地方,都是可以使用的 ... 
- cent OS 7 安装谷歌浏览器
			我直接写一个shell 脚本, install_google.sh, bash 命令直接运行就好, 脚本内容如下: (切换root用户执行) set -e # 出错即退出 echo " ... 
- SAP 同一个序列号可以同时出现在2个不同的HU里?
			SAP 同一个序列号可以同时出现在2个不同的HU里? 答案是可以的. 如下图示,HU 180141003288里的序列号11810010540121, 而序列号11810010540121已经出现在另 ... 
- Kali无法使用Chrome原因及解决方法
			Kali安装好后,默认的浏览器是Firefox-ESR(Extended Support Release 长期支持)版本. 作为Chrome的死忠粉,当然是要下Chrome用用的. 直到我 ... 
- 利用Python调用pastebin.com API自动创建paste
			在上一篇文章中,已经实现了模拟pastebin.com的账号登录,并且获取了api_dev_key,这一篇文章主要讲一下调用API创建paste 登录之后,进入API页面,发现网站已经提供了几个API ... 
- 【原创】Airflow 简介&如何部署一个健壮的 apache-airflow 调度系统
			声明 本文摘录了很多前辈的文章,原文如下: https://www.jianshu.com/p/2ecef979c606 Airflow 简介 Airflow是一个可编程,调度和监控的工作流平台,基于 ... 
- Druid连接池使用
			转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/11280540.html 一:DRUID连接池简介 阿里出品的“为监控而生”的数据库连接池,在功能.性能.扩展 ... 
- [MySQL] 为什么要给表加上主键
			1.一个没加主键的表,它的数据无序的放置在磁盘存储器上,一行一行的排列的很整齐. 2.一个加了主键的表,并不能被称之为「表」.如果给表上了主键,那么表在磁盘上的存储结构就由整齐排列的结构转变成了树状结 ... 
- lvm调整卷大小
			lvreduce -L 10240M /dev/rhel/home pvchange -xn /dev/sdb1 pvmove -i /dev/sdb1 vgreduce rhel /dev/sdb1 ... 
- 使用GCP_EC2云服务器搭部署网络服务
			首先,在此阿里云/腾讯云/华为云购买一个云服务器推荐使用阿里云的 首先链接你的VPS,可以使用X-shell / Putty / SecureCRTPortable 等SSH链接工具 注意:如果不知道 ... 
