【洛谷 P4134】 [BJOI2012]连连看(费用流)
题目链接
首先是可以\(O(n^2)\)枚举出所有符合要求的点对的,然后考虑建图。
还是拆点把每个点拆成入点和出点,源点连入点,出点连汇点,流量都是1,费用都是0。
然后对于没对符合要求的\((x,y)\),连接\((x_{in},y_{out}),(y_{in},x_{out})\),费用均为\(x+y\),流量均为\(1\)。
然后跑出最大费用最大流,最大流除以2就是第一问,最大费用除以2就是第二问。
为什么要双向连边然后答案除以2?单向连边去试试就知道了
PS:会重复。
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstring>
#define INF 2147483647
using namespace std;
const int MAXN = 5010;
const int MAXM = 200010;
queue <int> q;
int s, t, now, n, m;
struct Edge{
int from, next, to, rest, cost;
}e[MAXM];
int head[MAXN], num = 1, dis[MAXN], vis[MAXN], Flow[MAXN], pre[MAXN];
inline void Add(int from, int to, int flow, int cost){
e[++num] = (Edge){ from, head[from], to, flow, cost }; head[from] = num;
e[++num] = (Edge){ to, head[to], from, 0, -cost }; head[to] = num;
}
int RoadsExist(){
q.push(s);
memset(dis, 127, sizeof dis);
dis[s] = 0; Flow[s] = INF; pre[t] = 0;
while(!q.empty()){
now = q.front(); q.pop(); vis[now] = 0;
for(int i = head[now]; i; i = e[i].next)
if(e[i].rest && dis[e[i].to] > dis[now] + e[i].cost){
dis[e[i].to] = dis[now] + e[i].cost;
pre[e[i].to] = i;
Flow[e[i].to] = min(Flow[now], e[i].rest);
if(!vis[e[i].to]){
vis[e[i].to] = 1;
q.push(e[i].to);
}
}
}
return pre[t];
}
int a, b, c, d, maxflow, mincost;
int gcd(int a, int b){
return b ? gcd(b, a % b) : a;
}
int main(){
scanf("%d%d", &a, &b); s = 4999; t = 5000;
for(int i = a; i <= b; ++i){
Add(s, i, 1, 0);
Add(i + 1000, t, 1, 0);
for(int j = a; j < i; ++j){
c = i * i - j * j;
d = sqrt(c);
if(d * d == c && gcd(d, j) == 1){
Add(i, j + 1000, 1, -i - j);
Add(j, i + 1000, 1, -i - j);
}
}
}
while(RoadsExist()){
maxflow += Flow[t];
mincost += Flow[t] * dis[t];
for(int i = t; i != s; i = e[pre[i]].from){
e[pre[i]].rest -= Flow[t];
e[pre[i] ^ 1].rest += Flow[t];
}
}
printf("%d %d\n", maxflow >> 1, -mincost >> 1);
return 0;
}
【洛谷 P4134】 [BJOI2012]连连看(费用流)的更多相关文章
- 洛谷.4015.运输问题(SPFA费用流)
题目链接 嗯..水题 洛谷这网络流二十四题的难度评价真神奇.. #include <queue> #include <cstdio> #include <cctype&g ...
- 洛谷P4015 运输问题(费用流)
传送门 源点向仓库连费用$0$,流量为储量的边,商店向汇点连费用$0$,流量为需求的边,然后仓库向商店连流量$inf$,费用对应的边,跑个费用流即可 //minamoto #include<io ...
- 洛谷P4014 分配问题(费用流)
传送门 可以把原图看做一个二分图,人在左边,任务在右边,求一个带权的最大和最小完美匹配 然而我并不会二分图做法,所以只好直接用费用流套进去,求一个最小费用最大流和最大费用最大流即可 //minamot ...
- 洛谷 - P4452 - 航班安排 - 费用流
https://www.luogu.org/problemnew/show/P4452 又一道看题解的费用流. 注意时间也影响节点,像题解那样建边就少很多了. #include<bits/std ...
- 洛谷.3381.[模板]最小费用最大流(zkw)
题目链接 Update:我好像刚知道多路增广就是zkw费用流.. //1314ms 2.66MB 本题优化明显 #include <queue> #include <cstdio&g ...
- 洛谷P3381 (最小费用最大流模板)
记得把数组开大一点,不然就RE了... 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define int long long 4 ...
- BZOJ 2661: [BeiJing wc2012]连连看 费用流
2661: [BeiJing wc2012]连连看 Description 凡是考智商的题里面总会有这么一种消除游戏.不过现在面对的这关连连看可不是QQ游戏里那种考眼力的游戏.我们的规则是,给出一个闭 ...
- [BZOJ2661][BeiJing wc2012]连连看 费用流
2661: [BeiJing wc2012]连连看 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1349 Solved: 577[Submit][ ...
- 洛谷P3639 [APIO2013] 道路费用 [生成树的特殊算法]
题目传送门 道路费用 格式难调,题面就不放了. 分析: 这是一道要细(yan)心(jing)的生成树的好(gui)题. 首先我们看到$k$的范围非常小,那么我们就可以直接$2^k$枚举每一条加边是否选 ...
随机推荐
- PAT 甲级 1054 The Dominant Color
https://pintia.cn/problem-sets/994805342720868352/problems/994805422639136768 Behind the scenes in t ...
- hibernate.cfg.xml案例
一.概念. hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库.既然学习Hibernate那么第 ...
- Building microservices with ASP.NET Core (without MVC)(转)
There are several reasons why it makes sense to build super-lightweight HTTP services (or, despite a ...
- [LeetCode] Search in Rotated Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- Android定位测试(深坑)
问题:我们是一个海外app,市场部去马来西亚打开那边的市场,发现了一个问题,就是我们的app定位有问题,还是成都的定位,主要原因是在马来西亚使用这个app,请求中带的经纬度参数是成都的,导致服务器返回 ...
- 第144天:PS切图方法总结
一.切图方法分类 PhotoShop从CS版本演变到现在的CC版本,切图功能发生了比较大的变化,我们可以把PhotoShop CS版本时的切图功能称为传统切图,而从PhotoShop CC版本开始PS ...
- jquery中的append功能相当于剪切的作用 将原来的元素剪切走
jquery中的append功能相当于剪切的作用 将原来的元素剪切走
- 【bzoj4244】邮戳拉力赛 背包dp
题目描述 IOI铁路是由N+2个站点构成的直线线路.这条线路的车站从某一端的车站开始顺次标号为0...N+1. 这条路线上行驶的电车分为上行电车和下行电车两种,上行电车沿编号增大方向行驶,下行电车沿编 ...
- 【Java并发编程】之四:守护线程与线程阻塞的四种情况
守护线程 Java中有两类线程:User Thread(用户线程).Daemon Thread(守护线程) 用户线程即运行在前台的线程,而守护线程是运行在后台的线程. 守护线程作用是为其他前台线程 ...
- A Magic Lamp HDU - 3183(RMQ返回下标)
原文地址:https://blog.csdn.net/acdreamers/article/details/8692384 题意: 对于一个序列A[1...N],一共N个数,除去M个数使剩下的数组成的 ...