【最小费用最大流】N. April Fools' Problem (medium)
http://codeforces.com/contest/802/problem/N
【题解】


方法一:
#include<bits/stdc++.h>
using namespace std; typedef long long LL; #define N 200020 int to[N], head[N], nxt[N], cost[N], cap[N], cnt; void init(){ memset(head, -, sizeof head); } void add_Edge(int S, int T, int c, int w){
nxt[cnt] = head[S], cap[cnt] = c, cost[cnt] = w, to[cnt] = T, head[S] = cnt ++;
nxt[cnt] = head[T], cap[cnt] = , cost[cnt] = -w, to[cnt] = S, head[T] = cnt ++;
} const LL INF = 1e14; LL dist[N];
int prv[N];
bool vis[N]; LL SPFA(int S, int T, int vet){
queue <int> Q;
fill(prv, prv + vet, -);
fill(dist, dist + vet, INF);
Q.push(S); dist[S] = , vis[S] = true;
while(!Q.empty()){
int x = Q.front(); Q.pop(), vis[x] = false;
for(int id = head[x]; ~id; id = nxt[id]) if( cap[id] ){
int y = to[id];
if(dist[y] > dist[x] + cost[id]){
dist[y] = dist[x] + cost[id];
prv[y] = id;
if(!vis[y]) Q.push(y), vis[y] = true;
}
}
}
if(!~prv[T]) { return INF; } for(int cur = T; cur != S; cur = to[cur^]){
cur = prv[cur];
cap[cur] --;
cap[cur ^ ] ++;
}
return dist[T];
} int a[N], b[N], n, k; int main(){
//freopen("in.txt", "r", stdin);
scanf("%d %d", &n, &k);
for(int i = ; i <= n; i ++) scanf("%d", a + i);
for(int i = ; i <= n; i ++) scanf("%d", b + i); int S = , T = * n + ;
init();
for(int i = ; i <= n; i ++){
add_Edge(S, i, , a[i]);
add_Edge(i + n, T, , b[i]);
add_Edge(i, i + n, n, );
if(i < n) add_Edge(i + n, i + n + , n - i, );
}
LL ans = ;
for(int i = ; i <= k; i ++) ans += SPFA(S, T, T + );
cout << ans << endl;
}
方法二:
#include <bits/stdc++.h>
using namespace std; #define oo 0x3f3f3f3fLL
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SUM(x,y) accumulate(x,y,0)
#define gcd(x,y) (__gcd(x,y))
#define LOWBIT(x) (x&(-x))
#define CLR(x,y) memset(x,y,sizeof(x))
#define PD(x) printf("%d\n",(x))
#define PF(f) printf("%lf\n",(f))
#define PS(s) puts(s) typedef pair<int, int> pii;
typedef long long LL; template <class T> inline bool chkmin(T& x, T y) { return x > y ? x = y, true : false; }
template <class T> inline bool chkmax(T& x, T y) { return x < y ? x = y, true : false; } template <class T> T reads() {
T x = ;
static int f;
static char c;
for (f = ; !isdigit(c = getchar()); ) if (c == '-') f = -;
for (x = ; isdigit(c); c = getchar()) x = x * + c - ;
return x * f;
}
#define read reads<int>
#define readll reads<LL> int n, k, v, a[], b[];
LL l, r, res, m, ans; int main(void) {
scanf("%d%d", &n, &k);
for (int i = ; i < n; ++i) a[i] = read();
for (int i = ; i < n; ++i) b[i] = read();
for(int i=;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
for(int i=;i<n;i++)
{
cout<<b[i]<<" ";
}
cout<<endl;
l = ; r = 1e10; res = 1e18;
while (l < r) {
ans = ; m = (l + r) >> ; v = ;
priority_queue<LL> qa, qb; for (int i = ; i < n; ++i) {
qa.push(-a[i]); LL ga = -qa.top() + b[i] - m;
LL gb = qb.empty() ? LL(1e18) : b[i] - qb.top();
if (ga <= gb && ga <= ) {
ans += ga; v++;
qa.pop(); qb.push(b[i]);
} else if (ga > gb && gb < ) {
ans += gb;
qb.pop(); qb.push(b[i]);
}
} if (v >= k) { r = m - ; res = ans + k * m; }
else l = m + ;
}
return cout << res << endl, ;
}
【最小费用最大流】N. April Fools' Problem (medium)的更多相关文章
- HIT 2739 - The Chinese Postman Problem - [带权有向图上的中国邮路问题][最小费用最大流]
题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2739 Time limit : 1 sec Memory limit : 64 M A Chinese ...
- HIT2739 The Chinese Postman Problem(最小费用最大流)
题目大概说给一张有向图,要从0点出发返回0点且每条边至少都要走过一次,求走的最短路程. 经典的CPP问题,解法就是加边构造出欧拉回路,一个有向图存在欧拉回路的充分必要条件是基图连通且所有点入度等于出度 ...
- HDU - 6437 Problem L.Videos 2018 Multi-University Training Contest 10 (最小费用最大流)
题意:M个影片,其属性有开始时间S,结束时间T,类型op和权值val.有K个人,每个人可以看若干个时间不相交的影片,其获得的收益是这个影片的权值val,但如果观看的影片相邻为相同的属性,那么收益要减少 ...
- CSU 1506 Problem D: Double Shortest Paths(最小费用最大流)
题意:2个人从1走到n,假设一条路第一次走则是价值di,假设第二次还走这条路则须要价值di+ai,要你输出2个人到达终点的最小价值! 太水了!一条边建2次就OK了.第一次价值为di,第二次为ai+di ...
- POJ 2195:Going Home(最小费用最大流)
http://poj.org/problem?id=2195 题意:有一个地图里面有N个人和N个家,每走一格的花费是1,问让这N个人分别到这N个家的最小花费是多少. 思路:通过这个题目学了最小费用最大 ...
- 【BZOJ】1221: [HNOI2001] 软件开发(最小费用最大流)
http://www.lydsy.com/JudgeOnline/problem.php?id=1221 先吐槽一下,数组依旧开小了RE:在spfa中用了memset和<queue>的版本 ...
- BZOJ 1927 星际竞速(最小费用最大流)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1927 题意:一个图,n个点.对于给出的每条边 u,v,w,表示u和v中编号小的那个到编号 ...
- BZOJ 1061 志愿者招募(最小费用最大流)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1061 题意:申奥成功后,布布经过不懈努力,终于 成为奥组委下属公司人力资源部门的主管.布 ...
- POJ 2195 Going Home(最小费用最大流)
http://poj.org/problem?id=2195 题意 : N*M的点阵中,有N个人,N个房子.让x个人走到这x个房子中,只能上下左右走,每个人每走一步就花1美元,问当所有的人都归位了之 ...
随机推荐
- poj2455Secret Milking Machine(二分+最大流)
链接 二分距离,小于当前距离的边容量+1,使最后流>=t 注意 会有重边 #include <iostream> #include<cstdio> #include< ...
- archsummit_bj2014
http://bj2014.archsummit.com/schedule.html 大会日程 时间 2014年12月19日 会议室 二号会议厅 7:45 入场注册 8:45 开场致辞 9:30 论高 ...
- css3 blur模糊解决ie6-ie9兼容
css3 blur模糊是css3的新特性,但是不兼容ie6-ie9,以下代码可以解决此问题: filter: progid:DXImageTransform.Microsoft.Blur(Pixel ...
- 洛谷 P2680 运输计划
题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航道连通了 L 国的所有星球. 小 P 掌管一家 ...
- 如何使用capedit分割数据包文件
wireshark是一个网络数据包的分析工具,主要用来捕获网卡上的数据包并显示数据包的详细内容.在处理一些大的数据包文件时,如果直接用wireshark图形工具打开一些大文件的数据包会出现响应慢甚至没 ...
- SDUT_2146:最小子序列和
题目描述 给你一个长为n(10<=n<=10000)的数组,数组中的每一个数大于等于1小于等于1000000.请你找出一个长为k(1<=k<=1000)的子序列.找序列时,假如 ...
- python制作二维码
1.安装MyQR pip install MyQR 2.打开pycharm,新建项目文件 from MyQR import myqr 2.1黑白二维码 2.2,彩色动态二维码 3.当前项目本地 ...
- Vue.js系列之vue-router(上) (转载自向朔1992)
概述 Vue非常适用于实践单页面应用程序也就是平时大家说的比较多的SPA(single page application),这点应该了解过Vue的应该都知道吧.一般的单页面应用是基于路由或页面之间的链 ...
- Oracle清空数据库中数据表数据的方法
一.简介最近在项目发版测试的时候,导出dmp的时候不小心把开发库中的一些脏数据导出来了,测试那边导入进去之后一堆不规范的数据,为了不影响测试结果,于是总结了一个快速清空数据库数据表所有数据的方法. 二 ...
- 前段开发 react native tab功能
import React, { Component } from 'react'; import { StyleSheet, Text, View, Image, } from 'react-nati ...