【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;
}

【hdu 3987】Harry Potter and the Forbidden Forest的更多相关文章

  1. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  2. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  3. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

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

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

  5. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  6. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

  7. 【HDU 5145】 NPY and girls(组合+莫队)

    pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...

  8. 【hdu 1043】Eight

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...

  9. 【HDU 3068】 最长回文

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...

随机推荐

  1. How Javascript works (Javascript工作原理) (十五) 类和继承及 Babel 和 TypeScript 代码转换探秘

    个人总结:读完这篇文章需要15分钟,文章主要讲解了Babel和TypeScript的工作原理,(例如对es6 类的转换,是将原始es6代码转换为es5代码,这些代码中包含着类似于 _classCall ...

  2. 紫书 习题 10-11 UVa 1646(斐波那契+高精度)

    自己用手算一下可以发现是斐波那契数列,然后因为数字很大,用高精度 以后做题的时候记得算几个数据找规律 #include<cstdio> #include<cmath> #inc ...

  3. HDU 4960 Another OCD Patient 简单DP

    思路: 因为是对称的,所以如果两段是对称的,那么一段的前缀和一定等于另一段的后缀和.根据这个性质,我们可以预处理出这个数列的对称点对.然后最后一个对称段是从哪里开始的,做n^2的DP就可以了. 代码: ...

  4. Servlet深入学习,规范,理解和实现(中)——深入理解Tomcat(一)

    心得:在写这篇博客之前.我大致阅读一些关于Tomcat的书籍和博客等资料.有些资料由于时间的关系,解说的Tomcat版本号太老.有些资料能够非常好的说明Tomcat整理结构和设计思想可是非常多重要的问 ...

  5. 78.Nodejs基础中间件Connect

    转自:https://www.cnblogs.com/chris-oil/p/5625437.html 前言 “中间件”在软件领域是一个非常广的概念,除操作系统的软件都可以称为中间件,比如,消息中间件 ...

  6. js生成验证码并验证的登录页面

    <!Doctype html> <html> <head> <meta charset="utf-8"/> <title> ...

  7. Windows 一键关闭UAC、防火墙、IE配置脚本

    有时候,在环境需求下,需要关闭windows防火墙,UAC,以及IE选项配置. 对不懂电脑来说是比较麻烦的,老是得教他们,关键还记不住…… so,以下脚本就可以解决这个问题 注:脚本 需要右键 以管理 ...

  8. python关于sorted里面key,reverse以及lamdba,operator这几个鸟人

     关于sorted:   help里给的解释 >>> help(sorted) Help on built-in function sorted in module __builti ...

  9. Swift学习笔记(10)--枚举

    1.定义语法: enum SomeEnumeration { // enumeration definition goes here } 2.使用 enum CompassPoint { case N ...

  10. spring在web.xml中的配置

    在实际项目中spring的配置文件applicationcontext.xml是通过spring提供的加载机制,自动加载的容器中去,在web项目中,配置文件加载到web容器中进行解析,目前,sprin ...