【最小费用最大流】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美元,问当所有的人都归位了之 ...
随机推荐
- vs2013转为vs2010项目
1.首先用记事本之类的工具打开.sln文件 打开后会看到如下信息 Format Version 12.00 就是指VS2013 VisualStudioVersion = 12.0.21005.1 指 ...
- 小程序java解密
<dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk16< ...
- 在服务端C#如何利用NPOI构建Excel模板
目前本人接触过两种模板导出的方式:(1)C#利用NPOI接口制作Excel模板,在服务端用数据渲染模板(2)在前端利用前人搭建好的框架,利用office编写xml制作模板,在客户端进行数据的渲染,导出 ...
- chrome inspect出现白屏的解决方案
点inspect后 弹出框,可是里面一片白色 PS:原效果不是这样,只是图找不到随便p的 原因可以看这个:http://www.cnblogs.com/slmk/p/7591126.html 大概意思 ...
- COGS 74. [NOIP2006] 明明的随机数 (Splay小练习。。)
☆ 输入文件:random.in 输出文件:random.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] 明明想在学校中请一些同学一起做一项问卷调查,为了 ...
- smile domain name www.bn-nd.com for sell. Please contact boyanzheng at foxmail.com 微笑的域名。请联系邮箱。
- JVM_Bind问题的解决方案
心得:删除javaw.exe进程即可. 以下是网络的解决方案: JVM_Bind问题出现通常有两种情况. 一种是原来的javaw.exe没有结束掉而又新创建了一个javaw.exe进程.这本无可厚非, ...
- core下的routelink
core mvc中 routelink返回和 framework mvc中返回的不一样,core中返回 IHtmlContent, 而 fw 中返回 MvcHtmlString 在写分页方法中用到了r ...
- 线段树、KMP、HASH模板
线段树 #include<cstdio> using namespace std; int n,p,a,b,m,x,y,ans; struct node { int l,r,w,f; }t ...
- More Effective C++ - 章节二 : 操作符(operators)
5. 对定制的 "类型转换函数" 保持警觉 允许编译器执行隐式类型转换,害处多过好处,不要提供转换函数,除非你确定需要. class foo { foo(int a = 0, in ...