BZOJ 4519 [CQOI2016]不同的最小割
这道题目很奇怪.
为什么奇怪?因为这道题用了一种叫分治最小割/最小割树的玩意.
以前从来没有见过这东西.
推荐一个讲这玩意的博客
写起来还是很顺手的.
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<string>
#include<iomanip>
#include<algorithm>
#include<map>
using namespace std;
#define ll long long
#define FILE "dealing"
#define up(i,j,n) for(int i=j;i<=n;++i)
#define db double
#define uint unsigned ll
#define eps 1e-12
#define pii pair<ll,ll>
ll read(){
ll x=0,f=1,ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return f*x;
}
const ll maxn=20500,limit=1e6,mod=(ll)(7+1e9+0.1);
const ll inf=(ll)(1e9);
template<class T>bool cmax(T& a,T b){return a<b?a=b,true:false;}
template<class T>bool cmin(T& a,T b){return a>b?a=b,true:false;}
template<class T>T min(T& a,T& b){return a<b?a:b;}
template<class T>T max(T& a,T& b){return a>b?a:b;}
int n,m,S,T,id[maxn],vis[maxn],tmp[maxn],w[maxn],tot=0;
struct node{
int y,next,flow,rev;
}e[maxn];
int len=1,linkk[maxn];
void insert(int x,int y,int flow){
e[++len].y=y;
e[len].next=linkk[x];
linkk[x]=len;
e[len].flow=flow;
e[len].rev=len+1;
e[++len].y=x;
e[len].next=linkk[y];
linkk[y]=len;
e[len].flow=flow;
e[len].rev=len-1;
}
int d[maxn],q[maxn],head,tail;
bool makelevel(){
up(i,1,n)d[i]=-1;
d[S]=0;head=tail=0;
q[++tail]=S;
while(++head<=tail){
int x=q[head];
for(int i=linkk[x];i;i=e[i].next)
if(d[e[i].y]==-1&&e[i].flow)d[e[i].y]=d[x]+1,q[++tail]=e[i].y;
}
return d[T]!=-1;
}
int makeflow(int x,int flow){
if(x==T||!flow)return flow;
int maxflow=0,dis=0;
for(int i=linkk[x];i&&maxflow<flow;i=e[i].next){
if(d[e[i].y]==d[x]+1&&e[i].flow)
if(dis=makeflow(e[i].y,min(flow-maxflow,e[i].flow))){
e[i].flow-=dis;
e[i^1].flow+=dis;
maxflow+=dis;
}
}
if(!maxflow)d[x]=-1;
return maxflow;
}
int dinic(){
int ans=0,d;
while(makelevel())
while(d=makeflow(S,inf))
ans+=d;
return ans;
}
void dfs(int x){
vis[x]=1;
for(int i=linkk[x];i;i=e[i].next){
if(e[i].flow&&!vis[e[i].y])
dfs(e[i].y);
}
}
void divide(int l,int r){
if(l==r)return;
for(int i=2;i<=len;i+=2)
e[i].flow=e[i^1].flow=(e[i].flow+e[i^1].flow)>>1;
S=id[l],T=id[r];
int maxflow=dinic();
w[++tot]=maxflow;
up(i,1,n)vis[i]=0;
dfs(S);
int L=l,R=r;
up(i,l,r){
if(vis[id[i]])tmp[L++]=id[i];
else tmp[R--]=id[i];
}
up(i,l,r)id[i]=tmp[i];
divide(l,L-1);divide(R+1,r);
}
int main(){
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
n=read(),m=read();
up(i,1,n)id[i]=i;
up(i,1,m){
int x=read(),y=read(),v=read();
insert(x,y,v);
}
divide(1,n);
sort(w+1,w+tot+1);
w[0]=-1;
int ans=0;
up(i,1,tot)if(w[i]!=w[i-1])ans++;
printf("%d\n",ans);
return 0;
}
BZOJ 4519 [CQOI2016]不同的最小割的更多相关文章
- bzoj 4519: [Cqoi2016]不同的最小割 最小割树
怎么求一张无向图中任意两点之间的最小割? http://fanhq666.blog.163.com/blog/static/8194342620113495335724/ 一张无向图不同的最小割最多有 ...
- bzoj 4519: [Cqoi2016]不同的最小割【最小割树Gomory–Hu tree】
算法详见:http://www.cnblogs.com/lokiii/p/8191573.html 求出点两两之间的最小割之后,把他们扔到map/set里跑即可 可怕的是map和set跑的时间竟然完全 ...
- 4519: [Cqoi2016]不同的最小割
4519: [Cqoi2016]不同的最小割 Time Limit: 20 Sec Memory Limit: 512 MB Submit: 489 Solved: 301 [Submit][Stat ...
- bzoj千题计划140:bzoj4519: [Cqoi2016]不同的最小割
http://www.lydsy.com/JudgeOnline/problem.php?id=4519 最小割树 #include<queue> #include<cstdio&g ...
- bzoj4519: [Cqoi2016]不同的最小割(分治最小割)
4519: [Cqoi2016]不同的最小割 题目:传送门 题解: 同BZOJ 2229 基本一样的题目啊,就最后用set记录一下就ok 代码: #include<cstdio> #inc ...
- [ZJOI2011]最小割 & [CQOI2016]不同的最小割 分治求最小割
题面: [ZJOI2011]最小割 [CQOI2016]不同的最小割 题解: 其实这两道是同一道题.... 最小割是用的dinic,不同的最小割是用的isap 其实都是分治求最小割 简单讲讲思路吧 就 ...
- 【BZOJ4519】[Cqoi2016]不同的最小割 最小割树
[BZOJ4519][Cqoi2016]不同的最小割 Description 学过图论的同学都知道最小割的概念:对于一个图,某个对图中结点的划分将图中所有结点分成两个部分,如果结点s,t不在同一个部分 ...
- [BZOJ 3144] [Hnoi2013] 切糕 【最小割】
题目链接:BZOJ - 3144 题目分析 题意:在 P * Q 的方格上填数字,可以填 [1, R] . 在 (x, y) 上填 z 会有 V[x][y][z] 的代价.限制:相邻两个格子填的数字的 ...
- [BZOJ 3894] 文理分科 【最小割】
题目链接:BZOJ - 3894 题目分析 最小割模型,设定一个点与 S 相连表示选文,与 T 相连表示选理. 那么首先要加上所有可能获得的权值,然后减去最小割,即不能获得的权值. 那么对于每个点,从 ...
随机推荐
- 16. Spring Boot使用Druid(编程注入)【从零开始学Spring Boot】
转载:http://blog.csdn.net/linxingliang/article/details/52001744 在上一节使用是配置文件的方式进行使用druid,这里在扩散下使用编程式进行使 ...
- 国内最受欢迎的7大API供应平台对比和介绍
俗话说“巧妇难为无米之炊”,数据源就是数据产生价值中的那些大米.那大数据时代企业需要哪些数据呢?根据我个人理解我觉得可以大致分为以下几类: 1.(内部)企业自身业务生产经营环节产生的内部数据[包括销售 ...
- 决策树之 C4.5
C4.5 是对 ID3 的一个优化,它依据信息增益率来进行属性选择. 关于决策树.请參见:http://blog.csdn.net/bone_ace/article/details/46299681 ...
- 重读金典------高质量C编程指南(林锐)-------第二章 程序的板式
2.1 空行 规则1:在每个类声明之后,每个函数定义结束之后加空行. 规则2:在某个函数体内,相关的不加空行,不相关的加空行. // 空行 void Function1(-) { - } // 空行 ...
- (十)jQuery对表单、表格的操作
一.表单应用 1.HTML中的表单大致由三部分组成 (1).表单标签:包含处理表单数据所用的服务端程序URL,以及数据提交到服务器的方法. (2).表单域:包含文本框.密码框.隐藏域.多行文本框.复选 ...
- C# Excel
using System.IO;using System.Text;namespace iLIS.Common{ ///<summary> ///生成Excel文档内容 /// 存入工作流 ...
- detect——point_in_polygon
/******************实现功能:判断平面任一点是否在指定多边形内********************/ #include <string> #include <v ...
- win7查看端口占用
1.查看谁占用了我们的80端口,在windows命令行窗口下执行: netstat -aon|findstr 80 发现80端口被进程号为2596的进程占用.2.查看占用80端口进程的应用程序是什 ...
- .NET C# Json序列化与反序列化——Newtonsoft.Json学习笔记
Newtonsoft.Json,一款.NET中开源的Json序列化和反序列化类库(介绍及下载地址:http://json.codeplex.com/). /// <summary> ...
- Google论文BigTable拜读
这周少打点dota2,争取把这篇论文读懂并呈现出来,和大家一起分享. 先把论文搞懂,然后再看下和论文搭界的知识,比如hbase,Chubby和Paxos算法. Bigtable: A Distribu ...