很容易发现是网络流的题目,但最少边怎么求呢?初时想不到,但画图后忽然发现可以这样:

求一次网络流最小割后,把满流的边置1,不满流的置INF。再求一次最大流即可。

为什么呢?

是否会存在一些边当前不满流,但有可能是最少边数最少割的边呢?否。因为按照DINIC的求法,每次都是增广容量最少的路,若当前不满流,则必定不是最小割的边,所以只需在满流的边,即可组成最小割的边寻找最少边就可以了。

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std; const int INF=0x3f3f3f3f;
const int MAXN=;
const int MAXM=; struct Node{
int from,to,next;
int cap;
}edge[MAXM];
int tol; int dep[MAXN];
int head[MAXN]; int n,m;
void init(){
tol=;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,int w){
edge[tol].from=u;
edge[tol].to=v; edge[tol].cap=w; edge[tol].next=head[u];
head[u]=tol++;
edge[tol].from=v;
edge[tol].to=u;
edge[tol].cap=;
edge[tol].next=head[v];
head[v]=tol++;
} int BFS(int start,int end){
int que[MAXN];
int front,rear; front=rear=;
memset(dep,-,sizeof(dep));
que[rear++]=start;
dep[start]=;
while(front!=rear) {
int u=que[front++];
if(front==MAXN)front=;
for(int i= head[u];i!=-; i=edge[i].next){
int v=edge[i].to;
if(edge[i].cap>&& dep[v]==-){
dep[v]=dep[u]+;
que[rear++]=v;
if(rear>=MAXN) rear=;
if(v==end)return ;
}
}
}
return ;
}
int dinic(int start,int end){
int res=;
int top;
int stack[MAXN];
int cur[MAXN];
while(BFS(start,end)){
memcpy(cur,head, sizeof(head));
int u=start;
top=;
while(){
if(u==end){
int min=INF;
int loc;
for(int i=;i<top;i++)
if(min>edge [stack[i]].cap) {
min=edge [stack[i]].cap;
loc=i;
}
for(int i=;i<top;i++){
edge[stack[i]].cap-=min;
edge[stack[i]^].cap+=min;
}
res+=min;
top=loc;
u=edge[stack[top]].from;
}
for(int i=cur[u]; i!=-; cur[u]=i=edge[i].next)
if(edge[i].cap!= && dep[u]+==dep[edge[i].to])
break;
if(cur[u] !=-){
stack [top++]= cur[u];
u=edge[cur[u]].to;
}
else{
if(top==) break;
dep[u]=-;
u= edge[stack [--top] ].from;
}
}
}
return res;
} int main(){
int T,cas=;
int u,v,w,d;
scanf("%d",&T);
while(T--){
init();
cas++;
scanf("%d%d",&n,&m);
for(int i=;i<m;i++){
scanf("%d%d%d%d",&u,&v,&w,&d);
if(d){
addedge(v,u,w);
}
addedge(u,v,w);
}
dinic(,n-);
for(int i=;i<tol;i+=){
if(edge[i].cap==){
edge[i].cap=; edge[i^].cap=;
}
else {
edge[i].cap=INF;
edge[i^].cap=;
}
}
int ans=dinic(,n-);
printf("Case %d: ",cas);
printf("%d\n",ans);
}
return ;
}

DINIC的模板

 const int INF=0x3f3f3f3f;
const int MAXN=;
const int MAXM=; struct Node{
int from,to,next;
int cap;
}edge[MAXM];
int tol; int dep[MAXN];
int head[MAXN]; int n,m;
void init(){
tol=;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,int w){
edge[tol].from=u;
edge[tol].to=v; edge[tol].cap=w; edge[tol].next=head[u];
head[u]=tol++;
edge[tol].from=v;
edge[tol].to=u;
edge[tol].cap=;
edge[tol].next=head[v];
head[v]=tol++;
} int BFS(int start,int end){
int que[MAXN];
int front,rear; front=rear=;
memset(dep,-,sizeof(dep));
que[rear++]=start;
dep[start]=;
while(front!=rear) {
int u=que[front++];
if(front==MAXN)front=;
for(int i= head[u];i!=-; i=edge[i].next){
int v=edge[i].to;
if(edge[i].cap>&& dep[v]==-){
dep[v]=dep[u]+;
que[rear++]=v;
if(rear>=MAXN) rear=;
if(v==end)return ;
}
}
}
return ;
}
int dinic(int start,int end){
int res=;
int top;
int stack[MAXN];
int cur[MAXN];
while(BFS(start,end)){
memcpy(cur,head, sizeof(head));
int u=start;
top=;
while(){
if(u==end){
int min=INF;
int loc;
for(int i=;i<top;i++)
if(min>edge [stack[i]].cap) {
min=edge [stack[i]].cap;
loc=i;
}
for(int i=;i<top;i++){
edge[stack[i]].cap-=min;
edge[stack[i]^].cap+=min;
}
res+=min;
top=loc;
u=edge[stack[top]].from;
}
for(int i=cur[u]; i!=-; cur[u]=i=edge[i].next)
if(edge[i].cap!= && dep[u]+==dep[edge[i].to])
break;
if(cur[u] !=-){
stack [top++]= cur[u];
u=edge[cur[u]].to;
}
else{
if(top==) break;
dep[u]=-;
u= edge[stack [--top] ].from;
}
}
}
return res;
}

HDU 3987 && DINIC的更多相关文章

  1. HDU 3987 Harry Potter and the Forbidden Forest(边权放大法+最小割)

    Harry Potter and the Forbidden Forest Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/ ...

  2. hdu 3987 Harry Potter and the Forbidden Forest 求割边最少的最小割

    view code//hdu 3987 #include <iostream> #include <cstdio> #include <algorithm> #in ...

  3. 【hdu 3987】Harry Potter and the Forbidden Forest

    [Link]:http://acm.hdu.edu.cn/showproblem.php?pid=3987 [Description] 给出一张有n个点的图,有的边又向,有的边无向,现在要你破坏一些路 ...

  4. hdu 1532 Dinic模板(小白书)

    hdu1532 输入n,m. n条边,m个点,之后给出a到b的容量,求1到m的最大流. 注意:Dinic只能调用一次,因为原理是改变cap的值,如果调用多次一样的,那么第一次会对,其余的都会是0,因为 ...

  5. 【网络流#3】hdu 1532 - Dinic模板题

    输入为m,n表示m条边,n个结点 记下来m行,每行三个数,x,y,c表示x到y的边流量最大为c 这道题的模板来自于网络 http://blog.csdn.net/sprintfwater/articl ...

  6. hdu 2435 dinic算法模板+最小割性质

    #include<stdio.h> #include<queue> #include<string.h> using namespace std; #define ...

  7. Kakuro Extension HDU - 3338 (Dinic)

    Kakuro puzzle is played on a grid of "black" and "white" cells. Apart from the t ...

  8. hdu 4289 dinic模板

    题意:有N个城市,现在城市S出现了一伙歹徒,他们想运送一些炸弹到D城市,不过警方已经得到了线报知道他们的事情,不过警察不知道他们所在的具体位置,所以只能采取封锁城市的办法来阻断暴徒,不过封锁城市是需要 ...

  9. HDU 6214 Smallest Minimum Cut 【网络流最小割+ 二种方法只能一种有效+hdu 3987原题】

    Problem Description Consider a network G=(V,E) with source s and sink t . An s-t cut is a partition ...

随机推荐

  1. Find Minimum in Rotated Sorted Array 典型二分查找

    https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Suppose a sorted array is rot ...

  2. E20170907-ts

    flash  vt. 使闪光,使闪烁; 拍出,发出(电报等); 〈口〉炫耀;          adj. 闪光的,闪耀的,一闪而过的; 浮华的; 庞大的;           n. 闪光; 闪光灯下摄 ...

  3. Gold Balanced Lineup(hash)

    http://poj.org/problem?id=3274 ***** #include <stdio.h> #include <iostream> #include < ...

  4. centos vi和vim用法

    所有的 Unix Like 系统都会内建 vi 文书编辑器,其他的文书编辑器则不一定会存在. 但是目前我们使用比较多的是 vim 编辑器. vim 具有程序编辑的能力,可以主动的以字体颜色辨别语法的正 ...

  5. 使用logback实现http请求日志导入mongodb

    spring boot自带logback作为其日志新系统,但是在实际工作中,常常需要对日志进行管理或分析,如果只是单纯的将日志导入文本文件,则在查询时操作过于繁琐,如果将其导入mysql等关系型数据库 ...

  6. itext 生成pdf文档 小结(自己备忘)

    1.引入maven <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf& ...

  7. Halcon学习笔记之支持向量机(二)

    例程:classify_halogen_bulbs.hdev 在Halcon中模式匹配最成熟最常用的方式该署支持向量机了,在本例程中展示了使用支持向量机对卤素灯的质量检测方法.通过这个案例,相信大家可 ...

  8. js,jquery中.each()方法遍历如何终止循环

    用.each()方法遍历节点的时候,用“return false”只能终止当前循环并跳入下一次循环,并不能终止所有循环.代码如下: $(".days").each(function ...

  9. Spring Boot (14) 数据源配置原理

    数据源配置源码 这里截取org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration的部分源码,主要介绍Tomcat和Hika ...

  10. buf.readInt8函数详解

    offset {Number} 0 noAssert {Boolean} 默认:false 返回:{Number} 从该 Buffer 指定的 offset 位置开始读取一个有符号的8位整数值. 设置 ...