首页 技术 正文
技术 2022年11月20日
0 收藏 424 点赞 4,990 浏览 2237 个字

【Link】:http://acm.hdu.edu.cn/showproblem.php?pid=3987

【Description】

给出一张有n个点的图,有的边又向,有的边无向,现在要你破坏一些路,使得从点0无法到达点n-1。破坏每条路都有一个代价。求在代价最小的前提下,最少需要破坏多少条道路。(就是说求在最小割的前提下,最小的割边数)

【Solution】

我们先在原图上跑一次最大流;

求出跑完最大流之后的剩余网络.

显然,最后剩余网络上容量变成0的(也就是满流的边);

它才可能是最小割的边.

接下来;

把那些可能是最小割的边的边的容量重新赋值为1;

然后,其余边都赋值成INF;

再跑一遍最大流;

求出此时的最小割.就是答案了.

也即最小割边数目.

【NumberOf WA】

0

【Reviw】

最小割模型

【Code】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define ri(x) scanf("%d",&x)
#define rl(x) scanf("%lld",&x)
#define rs(x) scanf("%s",x+1)
#define oi(x) printf("%d",x)
#define ol(x) printf("%lld",x)
#define oc putchar(' ')
#define os(x) printf(x)
#define all(x) x.begin(),x.end()
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)typedef pair<int,int> pii;
typedef pair<LL,LL> pll;const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1000;
const int M = 4e5;
const LL INF = 1e18;int n,m,en[M+10],fir[N+10],tfir[N+10],nex[M+10],totm;
int deep[N+10];
LL flow[M+10];
queue <int> dl;void add(int x,int y,int z){
nex[totm] = fir[x];
fir[x] = totm;
flow[totm] = z;
en[totm] = y;
totm++; nex[totm] = fir[y];
fir[y] = totm;
flow[totm] = 0;
en[totm] = x;
totm++;
}bool bfs(){
ms(deep,255);
dl.push(1);
deep[1] = 0;
while (!dl.empty()){
int x = dl.front();
dl.pop();
for (int i = fir[x];i != -1;i = nex[i]){
int y = en[i];
if (flow[i] >0 && deep[y]==-1){
deep[y] = deep[x] + 1;
dl.push(y);
}
}
}
return deep[n]!=-1;
}LL dfs(int x,LL limit){
if (x == n) return limit;
if (limit == 0) return 0;
LL cur,f = 0;
for (int i = tfir[x];i!=-1;i = nex[i]){
tfir[x] = i;
int y = en[i];
if (deep[y] == deep[x] + 1 && (cur = dfs(y,min(limit,flow[i])))) {
limit -= cur;
f += cur;
flow[i] -= cur;
flow[i^1] += cur;
if (!limit) break;
}
}
return f;
}int main(){
//Open();
//Close();
int T,kk = 0;
ri(T);
while (T--){
ms(fir,255);
totm = 0;
ri(n),ri(m);
rep1(i,1,m){
int x,y,z,d;
ri(x),ri(y),ri(z),ri(d);
x++,y++;
add(x,y,z);
if (d == 1) add(y,x,z);
}
while ( bfs() ) {
rep1(i,1,n) tfir[i] = fir[i];
dfs(1,INF);
} for (int i = 0;i < totm;i+=2)
if (flow[i]==0){
flow[i] = 1;
flow[i^1] = 0;
}else{
flow[i] = INF;
flow[i^1] = 0;
} LL ans = 0; while ( bfs() ) {
rep1(i,1,n) tfir[i] = fir[i];
ans+=dfs(1,INF);
}
os("Case ");oi(++kk);os(": ");ol(ans);puts("");
}
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291