【最小费用最大流】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美元,问当所有的人都归位了之 ...
随机推荐
- mybatis通过插件方式实现读写分离
原理:通过自定义mybatis插件,拦截Executor的update和query方法,检查sql中有select就用读的库,其它的用写的库(如果有调用存储过程就另当别论了) @Intercepts( ...
- AJPFX总结面向对象中成员变量和成员方法的定义
//面向对象中成员变量和成员方法的定义格式:========================================= 成员变量定义在类中方法外,可以被该类中所有方法使用. ...
- bat判断服务是否启动
sc query|find "tomcat6" && echo yes || echo nosc query|find "eventlog" & ...
- UVA 11971 Polygon 多边形(连续概率)
题意: 一根长度为n的木条,随机选k个位置将其切成k+1段,问这k+1段能组成k+1条边的多边形的概率? 思路: 数学题.要求的是概率,明显与n无关. 将木条围成一个圆后再开切k+1刀,得到k+1段. ...
- 51nod 1272 最大距离
题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 给出一个长度为N的整数数组A,对于每一个数组元素,如果他后面存在大于等 ...
- SEO 第二章
SEO第二章 1. 掌握搜索引擎工作原理(重点) 2. 了解百度算法 3. 关键词的分类 一.什么是搜索引擎? 搜索引擎是用来实现搜索服务的,说白了搜索引擎也属于一种网站. 浏览器是用来加载网站 ...
- pagehelper 分页
分页jar包: <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pa ...
- 解决selenium.common.exception.WebDriverException:Message:'chromedriver' executable needs to be in Path
'chromedriver' executable needs to be in Path 声明:本人萌新,刚学python不久记录一下自己的坑,发出来若能帮助到一些人尽早解决问题那便是极好的,( ̄▽ ...
- AspNetCore容器化(Docker)部署(一) —— 入门
一.docker注册安装 Windows Docker Desktop https://www.docker.com/products/docker-desktop Linux Docker CE h ...
- 02.28 day03
print(1 or 3 > 2 and 4 < 5 or 6 and 2 < 7)## while True:# print(11)# print(22)# # break# # ...