[BJOI2012]连连看 BZOJ2661 费用流
题目描述
凡是考智商的题里面总会有这么一种消除游戏。不过现在面对的这关连连看可不是QQ游戏里那种考眼力的游戏。我们的规则是,给出一个闭区间[a,b]中的全部整数,如果其中某两个数x,y(设x>y)的平方差x^2-y^2是一个完全平方数z^2,并且y与z互质,那么就可以将x和y连起来并且将它们一起消除,同时得到x+y点分数。那么过关的要求就是,消除的数对尽可能多的前提下,得到足够的分数。快动手动笔算一算吧。
输入输出格式
输入格式:
只有一行,两个整数,分别表示a,b。
输出格式:
两个数,可以消去的对数,及在此基础上能得到的最大分数。
输入输出样例
说明
对于30%的数据,1<=a,b<=100
对于100%的数据,1<=a,b<=1000
将每个点分为in,out;
建边的时候要注意我们消去的是一对数字;
所以建边的时候Inx----Outy,Iny----Outx都要建边;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 100005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
#define mclr(x,a) memset((x),a,sizeof(x))
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii; inline int rd() {
int x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/
bool vis[maxn];
int n, m, s, t;
int x, y, f, z;
int dis[maxn], pre[maxn], last[maxn], flow[maxn];
int maxflow, mincost; struct node {
int to, nxt, flow, dis;
}edge[maxn << 2]; int head[maxn], cnt;
queue<int>q; void addedge(int from, int to, int flow, int dis) {
edge[++cnt].to = to; edge[cnt].flow = flow; edge[cnt].dis = dis;
edge[cnt].nxt = head[from]; head[from] = cnt;
} bool spfa(int s, int t) {
memset(dis, 0x7f, sizeof(dis)); memset(flow, 0x7f, sizeof(flow));
ms(vis);
q.push(s); vis[s] = 1; dis[s] = 0; pre[t] = -1;
while (!q.empty()) {
int now = q.front(); q.pop(); vis[now] = 0;
for (int i = head[now]; i != -1; i = edge[i].nxt) {
if (edge[i].flow > 0 && dis[edge[i].to] > dis[now] + edge[i].dis) {
dis[edge[i].to] = edge[i].dis + dis[now];
pre[edge[i].to] = now; last[edge[i].to] = i;
flow[edge[i].to] = min(flow[now], edge[i].flow);
if (!vis[edge[i].to]) {
vis[edge[i].to] = 1; q.push(edge[i].to);
}
}
}
}
return pre[t] != -1;
} void mincost_maxflow() {
while (spfa(s, t)) {
int now = t;
maxflow += flow[t]; mincost += flow[t] * dis[t];
while (now != s) {
edge[last[now]].flow -= flow[t];
edge[last[now] ^ 1].flow += flow[t];
now = pre[now];
}
}
} bool OK(int x, int y) {
if (x < y)swap(x, y);
int Z = x * x - y * y;
int z = (int)sqrt(Z);
if (z*z == Z) {
if (gcd(z, y) == 1) {
return true;
}
else return false;
}
else return false;
} int main()
{
// ios::sync_with_stdio(0);
mclr(head, -1); cnt = 1;
int a, b; a = rd(); b = rd();
s = 0; t = b + 1;
int num = b - a + 1;
for (int i = a; i <= b; i++) {
addedge(s, i, 1, 0); addedge(i, s, 0, 0);
addedge(i + b , t, 1, 0); addedge(t, i + b , 0, 0);
}
for (int i = a; i <= b; i++) {
for (int j = i + 1; j <= b; j++) {
if (OK(i, j)) {
addedge(i + b, j, 0, i + j); addedge(j, i + b, 1, -(i + j));
addedge(j + b, i, 0, i + j); addedge(i, j + b, 1, -(i + j));
}
}
}
mincost_maxflow();
printf("%d %d\n", maxflow/2, -mincost/2);
return 0;
}
[BJOI2012]连连看 BZOJ2661 费用流的更多相关文章
- 【洛谷 P4134】 [BJOI2012]连连看(费用流)
题目链接 首先是可以\(O(n^2)\)枚举出所有符合要求的点对的,然后考虑建图. 还是拆点把每个点拆成入点和出点,源点连入点,出点连汇点,流量都是1,费用都是0. 然后对于没对符合要求的\((x,y ...
- BZOJ_2661_[BeiJing wc2012]连连看_费用流
BZOJ_2661_[BeiJing wc2012]连连看_费用流 Description 凡是考智商的题里面总会有这么一种消除游戏.不过现在面对的这关连连看可不是QQ游戏里那种考眼力的游戏.我们的规 ...
- BZOJ2661 连连看 (费用流)
把所有点拆成两个,将符合条件的两个点x,y连上边,流量为1,费用为-(x+y). 做一遍最小费用最大流,最后ans div 2即可. Program bzoj2661; ; ..] of longin ...
- 【BZOJ2661】[BeiJing wc2012]连连看 最大费用流
[BZOJ2661][BeiJing wc2012]连连看 Description 凡是考智商的题里面总会有这么一种消除游戏.不过现在面对的这关连连看可不是QQ游戏里那种考眼力的游戏.我们的规则是,给 ...
- BZOJ 2661 连连看(费用流)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2661 题意:给出一个区间[a,b]中的全部整数,如果其中某两个数x,y(设x>y) ...
- 【费用流】bzoj2661 [BeiJing wc2012]连连看
将每个数拆点,互相连边,然后满足条件的数对之间互相连边,跑最大费用流,答案是流量和费用分别除以2. 一定要i->j.j->i都连上,否则可能会出现一个数在一边被选择了,在另一边的另一个匹配 ...
- hdu-5988 Coding Contest(费用流)
题目链接: Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- POJ2195 Going Home[费用流|二分图最大权匹配]
Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22088 Accepted: 11155 Desc ...
- BZOJ3130: [Sdoi2013]费用流[最大流 实数二分]
3130: [Sdoi2013]费用流 Time Limit: 10 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 960 Solved: 5 ...
随机推荐
- Java多线程-线程的交互
一.线程交互的基础知识void notify():唤醒在此对象监视器上等待的单个线程. void notifyAll():唤醒在此对象监视器上等待的所有线程. void wait():导致当前的线程等 ...
- Enumeration & Class & Structure
[Enumeration] 1.当一个枚举值类型已经确定后,可以使用shorter dot syntax来赋予其它值: 2.对一个枚举值switch的时候也可以使用short dot syntax: ...
- 使用python创建生成动态链接库dll
如今,随着深度学习的发展,python已经成为了深度学习研究中第一语言.绝大部分的深度学习工具包都有python的版本,很多重要算法都有python版本的实现.为了将这些算法应用到具体工程中,这些工具 ...
- 819. Most Common Word 统计高频词(暂未被禁止)
[抄题]: Given a paragraph and a list of banned words, return the most frequent word that is not in the ...
- Solidity开发、测试、部署
这篇文章很详细的列举了几种方式来开始solidity开发: https://medium.com/@davekaj/solidity-tips-and-tricks-for-beginners-bui ...
- Hyperledger项目中使用的工具
Hyperledger作为一个众多IT厂商参与的项目,全球化的开源社区,其项目的组织形式.流程.工具,都值得借鉴.好工匠离不开好工具,我注意到Hyperledger项目中使用了大量的好工具,包括项目管 ...
- Mr_matcher的细节1
1.NodeHandle类(或者NodeHandle句柄)的私有名称 1)句柄可以让你通过构造函数指定命名空间 ros::NodeHandle nh("my_namespace") ...
- CSS3的2D与3D转换
2D和3D转换涉及到数学中的知识,作为一个数学专业的毕业生,不研究一下岂不是对不起自己的专业? 首先来看几个参数: 1.transform-origin:origin(起源,起点),也即变形的起点,在 ...
- HackNine 避免在EditText中验证日期
1.概要: 为什么不直接为EditTText设置一个点击监听器,而非要使用Button呢? 答案是:使用Button更安全,因为用户无法修改Button的文本内容.如果使用EditTex ...
- C++的惨痛教训(未完待续)
题记:只有痛才能让人铭记!痛促进进步~ 1. strncpy,大家都知道要做安全检查,可是谁都有嫌麻烦的时候,尤其是自己很自信不会产生溢出的时候,可能不会坑了自己,却会坑了使用这段代码的人.所以,1. ...