BZOJ2229—— [Zjoi2011]最小割
0、题目大意:求两点之间的最小割,然后找出其中小于x的数量
1、分析:最小割树水题,上个板子就好
#include <queue>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
#define inf 214748364
inline int read(){
char ch = getchar(); int x = 0, f = 1;
while(ch < '0' || ch > '9'){
if(ch == '-') f = -1;
ch = getchar();
}
while('0' <= ch && ch <= '9'){
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
struct Edge{
int from, to, cap, flow, next;
};
int head[310], cur[310];
Edge G[20010];
int tot;
int d[310];
bool vis[310];
int s, t, n, m;
int a[310];
int ans[310][310];
int b[310];
inline void init(){
memset(head, -1, sizeof(head));
tot = -1;
return;
}
inline void insert(int from, int to, int cap){
G[++ tot] = (Edge){from, to, cap, 0, head[from]};
head[from] = tot;
G[++ tot] = (Edge){to, from, 0, 0, head[to]};
head[to] = tot;
return;
}
inline bool BFS(){
memset(vis, 0, sizeof(vis));
queue<int> Q;
Q.push(s);
vis[s]=1;
d[s]=0;
while(!Q.empty()){
int x = Q.front(); Q.pop();
for(int i = head[x]; i != -1; i = G[i].next){
Edge& e = G[i];
if(e.cap - e.flow > 0 && !vis[e.to]){
vis[e.to] = 1;
d[e.to]=d[x]+1;
Q.push(e.to);
}
}
}
return vis[t];
}
inline int dfs(int x, int a){
if(x == t || a == 0) return a;
int flow = 0, f;
for(int& i = cur[x]; i != -1; i = G[i].next){
Edge& e = G[i];
if(d[x]+1 == d[e.to] && (f = dfs(e.to, min(e.cap - e.flow, a))) > 0){
e.flow += f;
G[i ^ 1].flow -= f;
flow += f;
a -= f;
if(a == 0) break;
}
}
return flow;
}
inline int maxflow(){
int res = 0;
while(BFS()){
for(int i = 1; i <= n; i ++) cur[i] = head[i];
res += dfs(s, inf);
}
return res;
}
inline void Clear(){
for(int i = 0; i <= tot; i += 2){
G[i].flow = G[i ^ 1].flow = (G[i].flow + G[i ^ 1].flow) / 2;
}
}
inline void DFS(int x){
vis[x] = 1;
for(int i = head[x]; i != -1; i = G[i].next) if(!vis[G[i].to] && G[i].cap > G[i].flow){
DFS(G[i].to);
}
}
inline void solve(int l, int r){
if(l == r) return;
s = a[l], t = a[r];
Clear();
int tw = maxflow();
memset(vis, 0, sizeof(vis));
DFS(s);
for(int i = 1; i <= n; i ++) if(vis[i]){
for(int j = 1; j <= n; j ++) if(!vis[j]){
ans[i][j] = ans[j][i] = min(ans[i][j], tw);
}
}
int L = l, R = r;
for(int i = l; i <= r; i ++){
if(vis[a[i]]) b[L ++] = a[i];
else b[R --] = a[i];
}
for(int i = l; i <= r; i ++) a[i] = b[i];
solve(l, L - 1); solve(L, r);
}
int main(){
int T = read();
while(T --){
n = read(); m = read();
init();
for(int i = 1; i <= m; i ++){
int u = read(), v = read(), w = read();
insert(u, v, w); insert(v, u, w);
}
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= n; j ++){
ans[i][j] = 214748364;
}
}
for(int i = 1; i <= n; i ++) a[i] = i;
solve(1, n);
int q = read();
while(q --){
LL ret = 0;
int x = read();
for(int i = 1; i <= n; i ++){
for(int j = i + 1; j <= n; j ++){
ret += (ans[i][j] <= x ? 1 : 0);
}
}
printf("%lld\n", ret);
}
puts("");
}
return 0;
}
BZOJ2229—— [Zjoi2011]最小割的更多相关文章
- BZOJ2229: [Zjoi2011]最小割
题解: 真是一道神题!!! 大家还是围观JZP的题解吧(网址找不到了...) 代码: #include<cstdio> #include<cstdlib> #include&l ...
- bzoj千题计划139:bzoj2229: [Zjoi2011]最小割
http://www.lydsy.com/JudgeOnline/problem.php?id=2229 最小割树介绍:http://blog.csdn.net/jyxjyx27/article/de ...
- bzoj2229: [Zjoi2011]最小割(分治最小割+最小割树思想)
2229: [Zjoi2011]最小割 题目:传送门 题解: 一道非常好的题目啊!!! 蒟蒻的想法:暴力枚举点对跑最小割记录...绝对爆炸啊.... 开始怀疑是不是题目骗人...难道根本不用网络流?? ...
- [bzoj2229][Zjoi2011]最小割_网络流_最小割树
最小割 bzoj-2229 Zjoi-2011 题目大意:题目链接. 注释:略. 想法: 在这里给出最小割树的定义. 最小割树啊,就是这样一棵树.一个图的最小割树满足这棵树上任意两点之间的最小值就是原 ...
- BZOJ2229[Zjoi2011]最小割——最小割树
题目描述 小白在图论课上学到了一个新的概念——最小割,下课后小白在笔记本上写下了如下这段话: “对于一个图,某个对图中结点的划分将图中所有结点分成两个部分,如果结点s,t不在同一个部分中,则称这个划分 ...
- BZOJ2229: [Zjoi2011]最小割(最小割树)
传送门 最小割树 算法 初始时把所有点放在一个集合 从中任选两个点出来跑原图中的最小割 然后按照 \(s\) 集合与 \(t\) 集合的归属把当前集合划分成两个集合,递归处理 这样一共跑了 \(n − ...
- bzoj2229: [Zjoi2011]最小割(最小割树)
传送门 这题是用最小割树做的(不明白最小割树是什么的可以去看看这一题->这里) 有了最小割树就很简单了……点数那么少……每次跑出一个最大流就暴力搞一遍就好了 //minamoto #includ ...
- 【BZOJ2229】[ZJOI2011]最小割(网络流,最小割树)
[BZOJ2229][ZJOI2011]最小割(网络流,最小割树) 题面 BZOJ 洛谷 题解 戳这里 那么实现过程就是任选两点跑最小割更新答案,然后把点集划分为和\(S\)联通以及与\(T\)联通. ...
- 【BZOJ2229】[Zjoi2011]最小割 最小割树
[BZOJ2229][Zjoi2011]最小割 Description 小白在图论课上学到了一个新的概念——最小割,下课后小白在笔记本上写下了如下这段话: “对于一个图,某个对图中结点的划分将图中所有 ...
随机推荐
- Install R & RStudio for Ubuntu
Install R r-project.org official source to install the latest R system. add R source sudo vi /etc/ ...
- BZOJ2286: [Sdoi2011]消耗战
建出虚树dp. 把询问点按dfs序排序,用一个以dfs序为关键字的单调栈(以深度为关键字也是一样的),每次将一个询问点与栈顶的点的lca入栈,再将这个询问点入栈,在这个过程中建出一棵树就是虚树.具体看 ...
- 启动tomcat时 错误: 代理抛出异常 : java.rmi.server.ExportException: Port already in use: 1099的解决办法
一.问题描述 今天一来公司,在IntelliJ IDEA 中启动Tomcat服务器时就出现了如下图所示的错误:
- 创建 sequence
-- Create sequence create sequence XRMKSD_DET_SEQminvalue 1maxvalue 999999999999999999999999999start ...
- 记录s标签范例
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- EnableViewState
EnableViewState 系统默认的值为true,在传递状态值时就包括该控件: 为false,则传递状态值时则不包括它. 可以提高网络访问的速度. 某些控件是不需要接受用户的操作或只需要接受一次 ...
- Java类集
类集就是一个动态的对象数组,是对一些实现好的数据结构进行了包装,这样在使用时就会非常方便,最重要的是类集框架本身不受对象数组长度的限制. 类集框架的主要接口
- Favorite Games
Samurai II: Vengeance: http://www.madfingergames.com/games
- 无法访问 IIS 元数据库。您没有足够的特权访问计算机上的 IIS 网站
我是WIN10+VS2015 之前用的是VS2012 后来卸载了VS2012 就出现了这个错误,请问该如何解决 在VS的图标上 按右键用管理员(Administrator)运行
- DNX 版本升级命令
一.稳定版本 dnvm install latest -a x86 -r clrdnvm install latest -a x86 -r coreclrdnvm install latest -a ...