[BeiJing wc2012]连连看
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} /********************************************************************/ struct ZKM_MinCostMaxFlow{
const static int maxn = 1e5+;
const static int maxE = 1e5+;
const static int INF = 0xfffffff; struct Edge{
int to, next, cap, flow, cost;
Edge(int _to = , int _next = , int _cap = , int _flow = , int _cost = ):
to(_to), next(_next), cap(_cap), flow(_flow), cost(_cost){}
}edge[maxE*]; int head[maxn], tot;
int cur[maxn], dis[maxn];
bool vis[maxn];
int ss, tt, N;
int min_cost, max_flow;
void init(int N){
tot = ;
for(int i = ;i < N;i++) head[i] = -;
}
int addedge(int u, int v, int cap, int cost){
edge[tot] = Edge(v, head[u], cap, , cost);
int rs = tot;
head[u] = tot++;
edge[tot] = Edge(u, head[v], , , -cost);
head[v] = tot++;
return rs;
}
int aug(int u, int flow){
if(u == tt) return flow;
vis[u] = true;
for(int i = cur[u];i != -;i = edge[i].next){
int v = edge[i].to;
if(edge[i].cap > edge[i].flow && !vis[v] && dis[u] == dis[v] + edge[i].cost){
int tmp = aug(v, min(flow, edge[i].cap - edge[i].flow));
edge[i].flow += tmp;
edge[i^].flow -= tmp;
cur[u] = i;
if(tmp) return tmp;
}
}
return ;
}
bool modify_label(){
int d = INF;
for(int u = ;u < N;u++){
if(vis[u]){
for(int i = head[u];i != -;i = edge[i].next){
int v = edge[i].to;
if(edge[i].cap > edge[i].flow && !vis[v]){
d = min(d, dis[v]+edge[i].cost - dis[u]);
}
}
}
}
if(d == INF) return false;
for(int i = ;i < N;i++){
if(vis[i]){
vis[i] = false;
dis[i] += d;
}
}
return true;
} pair<int, int> mincostmaxflow(int start, int ed, int n){
ss = start, tt = ed, N = n;
min_cost = max_flow = ;
for(int i = ;i < n;i++) dis[i] = ;
while(){
for(int i = ;i < n;i++) cur[i] = head[i];
while(){
for(int i = ;i < n;i++) vis[i] = false;
int tmp = aug(ss, INF);
if(tmp == ) break;
max_flow += tmp;
min_cost += tmp*dis[ss];
}
if(!modify_label()) break;
}
return make_pair( max_flow, min_cost );
}
}solver; int gcd(int x, int y){
if(y == ) return x;
else return gcd(y, x%y);
} bool check(int x, int y){
if(x == y) return false;
if(x < y) swap(x, y);
int t = sqrt(x*x-y*y);
if(t*t != x*x-y*y) return false;
if(gcd(y, t) != ) return false;
return true;
} int main(){
int a, b;
a = read(); b = read();
solver.init(b*+);
for(int i = a;i <= b;i++){
for(int j = a;j <= b;j++){
if(check(i, j)){
solver.addedge(i, j+b, , -i-j);
}
}
solver.addedge(b*+, i, , ); //拆点
solver.addedge(i+b, b*+, , ); //拆点
}
pair<int, int> ans = solver.mincostmaxflow(b*+, b*+, b*+);
printf("%d %d\n", ans.first/, -ans.second/);
return ;
}
[BeiJing wc2012]连连看的更多相关文章
- BZOJ 2661: [BeiJing wc2012]连连看 费用流
2661: [BeiJing wc2012]连连看 Description 凡是考智商的题里面总会有这么一种消除游戏.不过现在面对的这关连连看可不是QQ游戏里那种考眼力的游戏.我们的规则是,给出一个闭 ...
- BZOJ2661: [BeiJing wc2012]连连看
2661: [BeiJing wc2012]连连看 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 483 Solved: 200[Submit][S ...
- BZOJ_2661_[BeiJing wc2012]连连看_费用流
BZOJ_2661_[BeiJing wc2012]连连看_费用流 Description 凡是考智商的题里面总会有这么一种消除游戏.不过现在面对的这关连连看可不是QQ游戏里那种考眼力的游戏.我们的规 ...
- 【BZOJ2661】[BeiJing wc2012]连连看 最大费用流
[BZOJ2661][BeiJing wc2012]连连看 Description 凡是考智商的题里面总会有这么一种消除游戏.不过现在面对的这关连连看可不是QQ游戏里那种考眼力的游戏.我们的规则是,给 ...
- [BZOJ2661][BeiJing wc2012]连连看 费用流
2661: [BeiJing wc2012]连连看 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1349 Solved: 577[Submit][ ...
- 2661: [BeiJing wc2012]连连看
Description 凡是考智商的题里面总会有这么一种消除游戏.不过现在面对的这关连连看可不是QQ游戏里那种考眼力的游戏.我们的规则是,给出一个闭区间[a,b]中的全部整数,如果其中某两个数x,y( ...
- 【bzoj2661】[BeiJing wc2012]连连看 最大费用最大流
题目描述 凡是考智商的题里面总会有这么一种消除游戏.不过现在面对的这关连连看可不是QQ游戏里那种考眼力的游戏.我们的规则是,给出一个闭区间[a,b]中的全部整数,如果其中某两个数x,y(设x>y ...
- bzoj 2661: [BeiJing wc2012]连连看
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #inclu ...
- [BeiJing wc2012]连连看(建模,最小费用最大流)
前言 突然发现自己在图论①被dalao吊着打... Solution 看到数据范围1000,感觉可以直接枚举连边,然后新建两个点就好了. 注意要拆点,不然可能会死循环(过来人) 代码实现 #inclu ...
- 【费用流】bzoj2661 [BeiJing wc2012]连连看
将每个数拆点,互相连边,然后满足条件的数对之间互相连边,跑最大费用流,答案是流量和费用分别除以2. 一定要i->j.j->i都连上,否则可能会出现一个数在一边被选择了,在另一边的另一个匹配 ...
随机推荐
- EASYARM-IMX283 制作ubifs文件系统
ubifs主页:http://www.linux-mtd.infradead.org/doc/ubifs.html nandflash上常用的文件系统有jffs2.yaffs和ubifs,其中ubif ...
- System.Configuration.ConfigurationErrorsException: An error occurred creating the configuration sect
An error has occurred creating the configuration section handler for userSettings/Microsoft.SqlServe ...
- Linux三种网络-vmware三种网络模式
Host-Only 桥接 NAT VMware虚拟机三种联网方法及原理 一.Brigde——桥接:默认使用VMnet0 1.原理: Bridge 桥"就是一个主机,这个机器拥有两块网卡,分别 ...
- HTML5响应式模版Mocha
HTML5响应式模版Mocha,经典,html5,蓝色,扁平,HTML5响应式模版Mocha是一款宽屏大气的HTML5网站展示模板. http://www.huiyi8.com/moban/
- BZOJ 1193 [HNOI2006]马步距离:大范围贪心 小范围暴搜
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1193 题意: 给定起点(px,py).终点(sx,sy).(x,y < 100000 ...
- LinkedList_1.打印两个有序链表的公共部分
思路: 实例化出两个链表($link_a, $link_b),比较连个链表当前元素的大小,谁小谁执行next()方法继续比较,当出现相当的时候把相等的值塞入数组$common里,当两个链表有一个元素比 ...
- hdu-1542 Atlantis(离散化+线段树+扫描线算法)
题目链接: Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- nginx rewrite 导致验证码不正确
配置nginx里url rewrite的时候,为了使浏览器地址栏的URL保持不变, 使用proxy_pass反向代理,但发现每次都会生成新的jsessionid 解决方法,配置中增加 proxy_co ...
- <C++>友元与虚函数的组合
为类重载<<与>>这两个运算符时,重载函数必须为该类的友元函数. 当友元不能被继承,故不能当作虚函数,无法使用多态. 可以用以下结构实现友元与虚函数的组合. class bas ...
- MySQL条件判断处理函数_20160925
MySQL条件判断处理 一.假如我想把salesperson 分成 5组,计算每个销售分组的业绩 首先先将销售分组 SELECT *, CASE WHEN salesperson IN (" ...