[Cqoi2014]危桥 (两遍网络流)
#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;
} /********************************************************************/ #define inf 0xffffff
#define T 2001
const int maxn = 2e6+;
const int Maxn = 2e3+;
int a, b;
int ans1, ans2;
int head[Maxn], q[Maxn], dis[Maxn], from[Maxn];
bool vis[Maxn]; struct node
{
int to, from, Next;
int v, c;
}e[maxn];
int cnt = ; int gcd(int x, int y){
if(y == ) return x;
else return gcd(y, x%y);
} void add_edge(int u, int v, int w, int c){
e[++cnt].to = v; e[cnt].from = u; e[cnt].Next = head[u]; head[u] = cnt;
e[cnt].v = w; e[cnt].c = c;
} void insert(int u, int v, int w, int c){
add_edge(u, v, w, c);
add_edge(v, u, , -c);
} //是否满足条件
bool check(int x, int y){
if(x < y) swap(x, y);
int t = int(sqrt(x*x-y*y));
return (gcd(y, t) == && x*x-y*y == t*t);
} bool spfa(){
for(int i = ;i <= T;i++){
dis[i] = -inf;
}
int t = , w = ;
dis[] = ; q[] = ; vis[] = ;
while(t != w){
int now = q[t]; t++;
if(t == T) t = ;
for(int i = head[now];i;i = e[i].Next){
if(e[i].v && e[i].c+dis[now] > dis[e[i].to]){
dis[e[i].to] = e[i].c + dis[now];
from[e[i].to] = i;
if(!vis[e[i].to]){
vis[e[i].to] = ;
q[w++] = e[i].to;
if(w == T) w = ;
}
}
}
vis[now] = ;
}
if(dis[T] == -inf) return false;
return true;
} void dfs(){
int x = inf;
for(int i = from[T];i;i = from[e[i].from]){
x = min(e[i].v, x);
}
for(int i = from[T];i;i = from[e[i].from]){
ans2 += x*e[i].c;
e[i].v -= x;
e[i^].v += x;
}
} int main(){
a = read(); b = read();
for(int i = a;i <= b;i++){
for(int j = a;j <= b;j++){
if(check(i, j) && i != j){
insert(i, j+, , i+j);
}
}
}
for(int i = a;i <= b;i++){
insert(, i, , );
insert(i+, T, , );
}
while(spfa()) dfs();
for(int i = ;i <= cnt;i += ){
if()
}
return ;
}
[Cqoi2014]危桥 (两遍网络流)的更多相关文章
- Luogu3163 [CQOI2014]危桥 ---- 网络流 及 一个细节的解释
Luogu3163 [CQOI2014]危桥 题意 有$n$个点和$m$条边,有些边可以无限次数的走,有些边这辈子只能走两次,给定两个起点和终点$a_1 --> a_2$(起点 --> 终 ...
- BZOJ 3504: [Cqoi2014]危桥 [最大流]
3504: [Cqoi2014]危桥 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1407 Solved: 703[Submit][Status] ...
- bzoj千题计划137:bzoj [CQOI2014]危桥
http://www.lydsy.com/JudgeOnline/problem.php?id=3504 往返n遍,即单向2*n遍 危桥流量为2,普通桥流量为inf 原图跑一遍最大流 交换b1,b2再 ...
- 3504: [Cqoi2014]危桥
3504: [Cqoi2014]危桥 链接 分析: 首先往返的可以转化为全是“往”,那么只要将容量除以2即可. 然后S向a1连边容量为an(除以2之前为2*an),S向a2连边容量为an,b1,b2向 ...
- Magic Potion(最大流,跑两遍网络流或者加一个中转点)
Magic Potion http://codeforces.com/gym/101981/attachments/download/7891/20182019-acmicpc-asia-nanjin ...
- BZOJ.3504.[CQOI2014]危桥(最大流ISAP)
BZOJ 洛谷 这种题大多是多源多汇跑网络流.往返\(a_n/b_n\)次可以看做去\(a_n/b_n\)次,直接把危桥能走的次数看做\(1\). 先不考虑别的,直接按原图建模:危桥建双向边容量为\( ...
- AC日记——[CQOI2014]危桥 洛谷 P3163
题目描述 Alice和Bob居住在一个由N座岛屿组成的国家,岛屿被编号为0到N-1.某些岛屿之间有桥相连,桥上的道路是双向的,但一次只能供一人通行.其中一些桥由于年久失修成为危桥,最多只能通行两次.A ...
- [CQOI2014]危桥
题目描述 Alice和Bob居住在一个由N座岛屿组成的国家,岛屿被编号为0到N-1.某些岛屿之间有桥相连,桥上的道路是双 向的,但一次只能供一人通行.其中一些桥由于年久失修成为危桥,最多只能通行两次. ...
- BZOJ3504 CQOI2014危桥(最大流)
如果只有一个人的话很容易想到最大流,正常桥连限流inf双向边,危桥连限流2双向边即可.现在有两个人,容易想到给两起点建超源两汇点建超汇,但这样没法保证两个人各自到达自己要去的目的地.于是再超源连一个人 ...
- 3504. [CQOI2014]危桥【最大流】
Description Alice和Bob居住在一个由N座岛屿组成的国家,岛屿被编号为0到N-1.某些岛屿之间有桥相连,桥上的道路是双 向的,但一次只能供一人通行.其中一些桥由于年久失修成为危桥,最多 ...
随机推荐
- python字符串替换之re.sub()
re.sub(pattern, repl, string, count=0, flags=0) pattern可以是一个字符串也可以是一个正则,用于匹配要替换的字符,如果不写,字符串不做修改.\1 代 ...
- [原]NYOJ-6174问题-57
大学生程序代写 /*6174问题 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 假设你有一个各位数字互不相同的四位数,把所有的数字从大到小排序后得到a,从小到大后得到b ...
- POJ3080 POJ3450Corporate Identity(广义后缀自动机||后缀数组||KMP)
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...
- bzoj 3280: 小R的烦恼 费用流
题目: Description 小R最近遇上了大麻烦,他的程序设计挂科了.于是他只好找程设老师求情.善良的程设老师答应不挂他,但是要求小R帮助他一起解决一个难题. 问题是这样的,程设老师最近要进行一项 ...
- 【转】 Pro Android学习笔记(六十):Preferences(4):MultiSelect List Preference
目录(?)[-] XML文件 在设备中保存 读出信息 ListPreference提供单选列表,我们可以通过CheckBoxPreference提供多选列表.此外,Android在3.0后提供Mult ...
- mariadb复制
实验环境:CentOS7 #安装mariadb-server #主数据库: [root@~ localhost]#vim /etc/my.cnf.d/server.cnf [mysqld] skip_ ...
- Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: Cannot open connection
Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceE ...
- k8s 基础 pod操作
创建hell world pod #vim hello-world-pod.yaml apiVersion: v1 kind: Pod metadata: name: hello-world spec ...
- python+selenium简单实现拖动元素实例
from selenium import webdriver#引入ActionChains类from selenium.webdriver.common.action_chains impo ...
- HTable基本概念
出处:http://www.taobaotest.com/blogs/1582 引言 团队中使用HBase的项目多了起来,对于业务人员而言,通常并不需要从头搭建.维护一套HBase的集群环境,对于其架 ...