牛客国庆集训派对Day6 A Birthday 费用流
牛客国庆集训派对Day6 A Birthday:https://www.nowcoder.com/acm/contest/206/A
题意:
正如往常一样,宇扬在蛋糕上插了n支蜡烛,并把蛋糕分为m个区域。因为某种原因,他必须把第i根蜡烛插在第ai个区域或第bi个区域。区域之间是不相交的。宇扬在一个区域内同时摆放x支蜡烛就要花费x2的时间。宇扬布置蛋糕所用的总时间是他在每个区域花的时间的和。
宇扬想快些见到恬恬,你能告诉他布置蛋糕最少需要多少时间吗?
思路:
建立图,左边n个点,表示不同的蜡烛,右边m个点,表示不同的区域,根据ai和bi从左边向右边连容量为1,费用为0的边,右边每个m点都向汇点连n条边,容量为1,费用为多一个蜡烛的增量。左边还要从源点拉来容量为1,费用为0的边。
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <cctype>
#include <queue>
#include <cmath>
#include <list>
#include <map>
#include <set>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int ,pii> p3;
//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFFLL; //
const ll nmos = 0x80000000LL; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3fLL; //
const double PI=acos(-1.0); template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
}
// #define _DEBUG; //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*-----------------------show time----------------------*/ const int maxn = 1e6+; struct Edge
{
int to,val,cost,nxt;
}gEdge[maxn];
int gHead[maxn],gPre[maxn];
int gPath[maxn],gDist[maxn];
bool in[maxn];
int gcount = ;
int n,m,k,w;
int fac[maxn];
bool spfa(int s,int t){ memset(gPre, -, sizeof(gPre));
memset(gDist,inf,sizeof(gDist));
memset(in, false , sizeof(in));
gDist[s] = ; in[s] = true;
queue<int>q;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop(); in[u] = false;
for(int e = gHead[u]; e!=-; e = gEdge[e].nxt){
int v = gEdge[e].to, w = gEdge[e].cost;
if(gEdge[e].val > && gDist[v] > gDist[u] + w){
gDist[v] = gDist[u] + gEdge[e].cost;
gPre[v] = u;
gPath[v] = e;
if(!in[v]){
q.push(v);in[v] = true;
}
}
}
}
if(gPre[t] == -)return false;
return true;
}
int MinCostFlow(int s,int t){
int cost = ,flow = ;
while(spfa(s,t)){
int f = inf;
for(int u = t; u != s; u = gPre[u]){
if(gEdge[gPath[u]].val < f){
f =gEdge[gPath[u]].val;
}
}
flow += f;
cost += gDist[t] * f;
for(int u=t; u!=s; u = gPre[u]){
gEdge[gPath[u]].val -= f;
gEdge[gPath[u] ^ ].val += f;
}
}
return cost;
} void addedge(int u,int v,int val, int cost){
gEdge[gcount].to = v;
gEdge[gcount].val = val;
gEdge[gcount].cost = cost;
gEdge[gcount].nxt = gHead[u];
gHead[u] = gcount++; gEdge[gcount].to = u;
gEdge[gcount].val = ;
gEdge[gcount].cost = -cost;
gEdge[gcount].nxt = gHead[v];
gHead[v] = gcount++;
} struct eee
{
int l,r,w,op;
}e[maxn];
/*
是大源点,m+m+1是ci源点,m+m+2是终点。
*/
void solve(){
memset(gHead,-,sizeof(gHead));
gcount = ;
scanf("%d%d", &n, &m);
for(int i=; i<=n; i++) fac[i] = i*i;
for(int i=n; i>=; i--) fac[i] = fac[i] - fac[i-]; for(int i=; i<=n; i++){
addedge(,i,,);
} for(int i=; i<=n; i++){
int le,ri;
scanf("%d%d", &le, &ri);
addedge(i,le + n, , );
addedge(i,ri + n, , );
} for(int i=; i<=m; i++){
for(int j=; j<=n; j++){
addedge(i+n,n+m+,,fac[j]);
}
} printf("%d\n",MinCostFlow(,n+m+));
} int main(){
solve();
return ;
}
牛客国庆集训派对Day6 A Birthday 费用流的更多相关文章
- 牛客国庆集训派对Day6 B.Board
链接 [https://www.nowcoder.com/acm/contest/206/B] 分析 只要在n*n范围内随便找一个斜对角的一个格子去计算就知道了 具体看代码体会吧 代码 #includ ...
- 牛客国庆集训派对Day6 Solution
A Birthday 思路:设置一个源点,一个汇点,每次对$源点对a_i, b_i , a_i 对 b_i 连一条流为1,费用为0的边$ 每个点都再连一条 1, 3, 5, 7, ....的边到 ...
- 牛客国庆集训派对Day6 && CCPC-WannaFly-Camp #1 F. kingdom(DP)
题目链接:https://www.nowcoder.com/acm/contest/206/F 题意:一棵 n 个点的树,根为 1,重儿子到父亲的费用为 0,其余为 1,问所有点到 1 的最大总费用是 ...
- 减2或减3(很搞的贪心)2019牛客国庆集训派对day6
题意:https://ac.nowcoder.com/acm/contest/1111/D 问你先减二x次的情况下,最少减几次3. 思路: %3不为0的要先减2,然后%3为0的要先减大的(比如9 3 ...
- 2019牛客国庆集训派对day5
2019牛客国庆集训派对day5 I.Strange Prime 题意 \(P=1e10+19\),求\(\sum x[i] mod P = 0\)的方案数,其中\(0 \leq x[i] < ...
- 牛客国庆集训派对Day1 L-New Game!(最短路)
链接:https://www.nowcoder.com/acm/contest/201/L 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...
- 牛客国庆集训派对Day4 J-寻找复读机
链接:https://www.nowcoder.com/acm/contest/204/J 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...
- 牛客国庆集训派对Day4 I-连通块计数(思维,组合数学)
链接:https://www.nowcoder.com/acm/contest/204/I 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...
- 牛客国庆集训派对Day1-C:Utawarerumono(数学)
链接:https://www.nowcoder.com/acm/contest/201/C 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1048576K,其他语言20 ...
随机推荐
- Nginx服务器安全加固tips整理
公司各业务网站大多用到Nginx,花了点时间整理了一下Nginx服务器安全加固的各类tips. 默认配置文件和Nginx端口 /usr/local/nginx/conf/-Nginx配置文件目录,/u ...
- 02 | 健康之路 kubernetes(k8s) 实践之路 : 生产可用环境及验证
上一篇< 01 | 健康之路 kubernetes(k8s) 实践之路 : 开篇及概况 >我们介绍了我们的大体情况,也算迈出了第一步.今天我们主要介绍下我们生产可用的集群架设方案.涉及了整 ...
- 跟着阿里p7一起学java高并发 - 第19天:JUC中的Executor框架详解1,全面掌握java并发核心技术
这是java高并发系列第19篇文章. 本文主要内容 介绍Executor框架相关内容 介绍Executor 介绍ExecutorService 介绍线程池ThreadPoolExecutor及案例 介 ...
- jdk1.8源码解析:HashMap底层数据结构之链表转红黑树的具体时机
本文从三个部分去探究HashMap的链表转红黑树的具体时机: 一.从HashMap中有关“链表转红黑树”阈值的声明: 二.[重点]解析HashMap.put(K key, V value)的源码: 三 ...
- 【Python-django后端开发】logging日制配置详解!!!
官方文档请查看:https://docs.djangoproject.com/en/1.11/topics/logging/ 1. 配置工程日志,在setting.py里,如下 LOGGING = { ...
- 【有容云案例系列】基于Jenkins和Kubernetes的CI工作流
嘉宾介绍 黄文俊 有容云资深系统架构师 主要负责容器云平台产品架构及设计. 8年工作经验, 有着企业级存储, 云计算解决方案相关理解. 关注于微服务设计思考, 开发流程优化, docker及kuber ...
- spark shuffle写操作三部曲之BypassMergeSortShuffleWriter
前言 再上一篇文章 spark shuffle的写操作之准备工作 中,主要介绍了 spark shuffle的准备工作,本篇文章主要介绍spark shuffle使用BypassMergeSortSh ...
- Apache 80端口可以访问,8080却不可访问
RT, 记录一下,后面看是否有解决方案.
- 阿里P8Java大牛仅用46张图让你弄懂JVM的体系结构与GC调优。
本PPT从JVM体系结构概述.GC算法.Hotspot内存管理.Hotspot垃圾回收器.调优和监控工具六大方面进行讲述.图文并茂不生枯燥. 此PPT长达46页,全部展示篇幅过长,本文优先分享前十六页 ...
- CSS: hack 方式一览
本文引自:http://blog.csdn.net/freshlover/article/details/12132801 什么是CSS hack 由于不同厂商的流览器或某浏览器的不同版本(如IE6- ...