2019年杭电多校第二场 1008题Harmonious Army(HDU6598+最小割+建图)
题目链接
题意
有\(n\)个士兵,要你给他们分配职业。有\(m\)对关系,对于某一对关系\(u,v\),如果同为勇士则总能力增加\(a\),同法师则增加\(c\),一个勇士一个法师增加\(\frac{a}{4}+\frac{c}{3}\),要你求最大的总能力。
思路
这位大佬的博客讲的很详细,大家可以看这篇博客~
在他的基础上加了点优化:源与某个点可能会连很多条边,因此我们可以汇总起来最后一次连边,汇同理,中间的反向边我们可以不用建\(0\)的边。
代码实现如下
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)
const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 500 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
int cnt1[maxn], cnt2[maxn];
int n, m, u, v, a, b, c;
struct Dinic {
queue<int> q;
int tot, s, t;
LL maxflow;
int head[maxn], d[maxn];
void init() {
tot = maxflow = 0;
memset(head, -1, sizeof(head));
}
struct edge {
int v, next;
LL w;
}ed[200007];
void add(int u, int v, LL w) {
ed[tot].v = v;
ed[tot].w = w;
ed[tot].next = head[u];
head[u] = tot++;
}
bool bfs() {
memset(d, 0, sizeof(d));
d[s] = 1;
while(!q.empty()) q.pop();
q.push(s);
int x;
while(!q.empty()) {
x = q.front();
q.pop();
for(int i = head[x]; ~i; i = ed[i].next) {
if(ed[i].w && !d[ed[i].v]) {
d[ed[i].v] = d[x] + 1;
q.push(ed[i].v);
if(ed[i].v == t) return 1;
}
}
}
return 0;
}
LL dinic(int x, LL flow) {
if(x == t) return flow;
LL res = flow, k, v;
for(int i = head[x]; ~i && res; i = ed[i].next) {
v = ed[i].v;
if(ed[i].w && d[v] == d[x] + 1) {
k = dinic(v, min(res, ed[i].w));
if(!k) d[v] = 0;
ed[i].w -= k;
ed[i^1].w += k;
res -= k;
}
}
return flow - res;
}
LL work() {
LL flow = 0;
while(bfs()) {
while(flow = dinic(s, inf)) maxflow += flow;
}
return maxflow;
}
}f;
int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif
while(~scanf("%d%d", &n, &m)) {
f.s = 0, f.t = n + 1;
f.init();
LL sum = 0;
for(int i = 1; i <= m; ++i) {
scanf("%d%d%d%d%d", &u, &v, &a, &b, &c);
sum += (a + b + c) * 2;
cnt1[u] += 5 * a / 4 + c / 3;
cnt1[v] += 5 * a / 4 + c / 3;
cnt2[u] += a / 4 + 4 * c / 3;
cnt2[v] += a / 4 + 4 * c / 3;
f.add(u, v, a / 2 + c / 3);
f.add(v, u, a / 2 + c / 3);
}
for(int i = 1; i <= n; ++i) {
if(cnt1[i]) f.add(f.s, i, cnt1[i]), f.add(i, f.s, 0);
if(cnt2[i]) f.add(i, f.t, cnt2[i]), f.add(f.t, i, 0);
cnt1[i] = cnt2[i] = 0;
}
printf("%lld\n", (sum - f.work()) / 2);
}
return 0;
}
2019年杭电多校第二场 1008题Harmonious Army(HDU6598+最小割+建图)的更多相关文章
- 2019年杭电多校第二场 1012题Longest Subarray(HDU6602+线段树)
题目链接 传送门 题意 要你找一个最长的区间使得区间内每一个数出现次数都大于等于\(K\). 思路 我们通过固定右端点考虑每个左端点的情况. 首先对于每个位置,我们用线段树来维护它作为\(C\)种元素 ...
- 2019年杭电多校第二场 1002题Beauty Of Unimodal Sequence(LIS+单调栈)
题目链接 传送门 思路 首先我们对\(a\)正反各跑一边\(LIS\),记录每个位置在前一半的\(LIS\)中应该放的位置\(ans1[i]\),后一半的位置\(ans2[i]\). 对于字典序最小的 ...
- Rikka with Travels(2019年杭电多校第九场07题+HDU6686+树形dp)
目录 题目链接 题意 思路 代码 题目链接 传送门 题意 定义\(L(a,b)\)为结点\(a\)到结点\(b\)的路径上的结点数,问有种\(pair(L(a,b),L(c,d))\)取值,其中结点\ ...
- 2019年杭电多校第一场 1009题String(HDU6586+模拟+单调栈)
题目链接 传送门 题意 给你一个字符串,要你构造一个长为\(k\)的子串使得每个字母出现的次数在\([L_i,R_i](0\leq i\leq26)\)间且字典序最小. 思路 做这种题目就是要保持思路 ...
- 2019年杭电多校第一场 1004题Vacation(HDU6581+数学)
题目链接 传送门 题意 有\(n+1\)辆车要过红绿灯,告诉你车的长度.与红绿灯的起点(题目假设红绿灯始终为绿).车的最大速度,问你第\(0\)辆车(距离最远)车头到达红绿灯起点的时间是多少(每辆车最 ...
- 2019年杭电多校第一场 1002题Operation(HDU6579+线性基)
题目链接 传送门 题意 初始时有\(n\)个数,现在有\(q\)次操作: 查询\([l,r]\)内选择一些数使得异或和最大: 在末尾加入一个数. 题目强制在线. 思路 对于\(i\)我们记录\([1, ...
- 2019 HDU 多校赛第二场 HDU 6598 Harmonious Army 构造最小割模型
题意: 有n个士兵,你可以选择让它成为战士还是法师. 有m对关系,u和v 如果同时为战士那么你可以获得a的权值 如果同时为法师,你可以获得c的权值, 如果一个为战士一个是法师,你可以获得b的权值 问你 ...
- 2019杭电多校第二场hdu6601 Keen On Everything But Triangle
Keen On Everything But Triangle 题目传送门 解题思路 利用主席树求区间第k小,先求区间内最大的值,再求第二大,第三大--直到找到连续的三个数可以构成一个三角形.因为对于 ...
- 2019杭电多校第二场hdu6602 Longest Subarray(线段树)
Longest Subarray 题目传送门 解题思路 本题求一个最大的子区间,满足区间内的数字要么出现次数大于等于k次,要么没出现过.给定区间内的数字范围是1~c. 如果r为右边界,对于一种数字x, ...
随机推荐
- how to compile and replace ubuntu kernel
how to compile and replace ubuntu kernel 0. environment -ubuntu 1804 64bit 1. prepare source code su ...
- 【Activiti学习之二】Activiti API(一)
环境 JDK 1.8 MySQL 5.6 Tomcat 7 Eclipse-Luna activiti 6.0 一.Activiti数据查询准备数据: package com.wjy.act; imp ...
- .net core中使用efcore
官网:https://docs.microsoft.com/zh-cn/aspnet/core/data/ef-mvc/intro?view=aspnetcore-2.2#register-the-s ...
- 微信小程序之判断页面来源
1. 对非首页,使用 getCurrentPages 函数获取当前页面栈 onLoad: function (options) { let pages = getCurrentPages() if ( ...
- 用eclipse开发需要准备什么?
1.到eclipse的官网上,https://www.eclipse.org/ 下载好eclipse,安装好eclipse,修改eclipse.ini文件,把内存改大点,避免出现内存溢出的情况. [ ...
- laravel门面与服务提供者区别
laravel门面模式与服务提供者区别 以 Laravel 自带的文件系统为例,在 config/app.php 的配置文件的 providers 数组中,注册了一个服务提供者: Illuminate ...
- 类的练习3——python编程从入门到实践
9-13 使用OrderedDict: 在练习6-4中,使用一个标准字典来表示词汇表.使用OrderedDict类来重写这个程序,并确认输出的顺序与在字典中添加的键值对的顺序一致. from coll ...
- 破解Charles4.2.7版本
软件下载地址:请点我 密码:sats dmg软件解密密码是:xclient.info 激活方法 1.将 Charles.app 拖至 应用程序 文件夹 2.将 keygen.jar 拖至 桌面 3.打 ...
- SQL分类之DML:增删改表中的数据
DML:增删改表中的数据 1.添加数据: 语法: insert into 表名(列名1,列名2,...列名n) values(值1,值2,...值n): 注意: 1.列名和值要一一对应. 2.如果表名 ...
- Java学习:数据库连接池技术
本节内容 数据库连接池 Spring JDBC : JDBC Template 数据库连接池 1.概念:其实就是一个容器(集合),存放数据库连接的容器 当系统初始化好后,容器中会申请一些连接对象,当用 ...