题目链接

首先是可以\(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]连连看(费用流)的更多相关文章

  1. 洛谷.4015.运输问题(SPFA费用流)

    题目链接 嗯..水题 洛谷这网络流二十四题的难度评价真神奇.. #include <queue> #include <cstdio> #include <cctype&g ...

  2. 洛谷P4015 运输问题(费用流)

    传送门 源点向仓库连费用$0$,流量为储量的边,商店向汇点连费用$0$,流量为需求的边,然后仓库向商店连流量$inf$,费用对应的边,跑个费用流即可 //minamoto #include<io ...

  3. 洛谷P4014 分配问题(费用流)

    传送门 可以把原图看做一个二分图,人在左边,任务在右边,求一个带权的最大和最小完美匹配 然而我并不会二分图做法,所以只好直接用费用流套进去,求一个最小费用最大流和最大费用最大流即可 //minamot ...

  4. 洛谷 - P4452 - 航班安排 - 费用流

    https://www.luogu.org/problemnew/show/P4452 又一道看题解的费用流. 注意时间也影响节点,像题解那样建边就少很多了. #include<bits/std ...

  5. 洛谷.3381.[模板]最小费用最大流(zkw)

    题目链接 Update:我好像刚知道多路增广就是zkw费用流.. //1314ms 2.66MB 本题优化明显 #include <queue> #include <cstdio&g ...

  6. 洛谷P3381 (最小费用最大流模板)

    记得把数组开大一点,不然就RE了... 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define int long long 4 ...

  7. BZOJ 2661: [BeiJing wc2012]连连看 费用流

    2661: [BeiJing wc2012]连连看 Description 凡是考智商的题里面总会有这么一种消除游戏.不过现在面对的这关连连看可不是QQ游戏里那种考眼力的游戏.我们的规则是,给出一个闭 ...

  8. [BZOJ2661][BeiJing wc2012]连连看 费用流

    2661: [BeiJing wc2012]连连看 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1349  Solved: 577[Submit][ ...

  9. 洛谷P3639 [APIO2013] 道路费用 [生成树的特殊算法]

    题目传送门 道路费用 格式难调,题面就不放了. 分析: 这是一道要细(yan)心(jing)的生成树的好(gui)题. 首先我们看到$k$的范围非常小,那么我们就可以直接$2^k$枚举每一条加边是否选 ...

随机推荐

  1. Spring Security 入门详解

    序:本文主要参考 spring实战 对里面的知识做一个梳理 1.Spring Security介绍 Spring Security是基于spring的应用程序提供声明式安全保护的安全性框架,它提供了完 ...

  2. redis 入手

    redis是数据库,数据库肯定是处理处理数据.既然是处理数据,无非就是增删查改 redis用于操作键的命令基本上分为两大类 对任何键都可执行的: DEL 命令. EXPIRE 命令. RENAME 命 ...

  3. this.$http & vue

    this.$http & vue https://github.com/pagekit/vue-resource Alias axios to Vue.prototype.$http http ...

  4. 第129天:node.js安装方法

    node.js安装方法 第一步:双击node.js安装包开始安装,注意64位和32位,按照自己的进行安装 第二步:在安装过程中一直选择next,在选择安装目录时,大多数默认安装在C盘,我安装在了D盘, ...

  5. 出现脚本错误或者未正确调用 Page()

    pages/xxxx/xxxx.js 出现脚本错误或者未正确调用 Page() 自己创建的小程序出现上面报错,可能是因为 xxxx.js是一个空文件,所以才会出现未正确调用: 如果是空文件的话,解决办 ...

  6. 【bzoj4027】[HEOI2015]兔子与樱花 树形dp+贪心

    题目描述 很久很久之前,森林里住着一群兔子.有一天,兔子们突然决定要去看樱花.兔子们所在森林里的樱花树很特殊.樱花树由n个树枝分叉点组成,编号从0到n-1,这n个分叉点由n-1个树枝连接,我们可以把它 ...

  7. [二十三]SpringBoot 之 redis

    本文章牵涉到的技术点比较多:spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对以上这些技术点有一定的了解或者也可以先看看这篇文章 ...

  8. BZOJ5073 小A的咒语(动态规划)

    设f[i][j][0/1]为前i位选j段时其中第i位选/不选最多能匹配到哪,转移时f[i][j][0]→f[i+1][j][0],f[i][j][1]→f[i+1][j][0],f[i][j][1]→ ...

  9. poj2299——Ultra-QuickSort

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...

  10. 【JQuery】数据

    一.前言        接着前一章的内容,继续本章的学习 二.内容 queue 显示或操作在匹配元素上执行的函数队列 .queue(queueName) 操作在匹配元素上执行的函数队列 .queue( ...