【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. require(): open_basedir restriction in effect. File

    新安装的 lnmp 环境,将项目放上报 require(): open_basedir restriction in effect. File 的错误! 错误日志显示,访问脚本不在 open_base ...

  2. 题解 P2195 【HXY造公园】

    天哪这道题竟然只有一篇题解! emm,首先读题看完两个操作就已经有很明确的思路了,显然是并查集+树的直径 一波解决. 并查集不多说了,如果不了解的可以看这里. 树的直径的思路很朴实,就是两边DFS(B ...

  3. 【Codeforces Round #422 (Div. 2) B】Crossword solving

    [题目链接]:http://codeforces.com/contest/822/problem/B [题意] 让你用s去匹配t,问你最少需要修改s中的多少个字符; 才能在t中匹配到s; [题解] O ...

  4. 关于App程序猿泡沫

    前言 做开发快七年了,对于程序猿,外行人总有着数不完的讽刺和误解,可是我都懒得去解释.代码搬运工人也好,民工也罢,随他们去说吧.可是网上近期流传的程序猿泡沫,尤其是APP程序猿泡沫的文章导致非常多我们 ...

  5. 《AndroidStudio每日一贴》11. 重构之提炼为常量

    提炼为常量是从暂时变量高速提炼出静态常量,这也是我们经常使用的重构手段. 很多其它有用技巧请查看<AndroidStudio有用指南> 操作步骤: ➤ 菜单条: Refactor -> ...

  6. hdoj--5053--the Sum of Cube(水)

    the Sum of Cube Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tot ...

  7. Codeforces--602A--Two Bases(水)

    Two Bases Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit St ...

  8. Vsftp权限控制(持续增加中)

    把用户限制在自己的home目录中,例如限制用户Leon只能访问/home/Leon目录下的文件,不允许访问上级目录. 先打开配置文件 vi /etc/vsftpd/vsftpd.conf 第一种方法: ...

  9. Xamarin大佬的地址

    https://www.cnblogs.com/hlx-blogs/p/7266098.html http://www.cnblogs.com/GuZhenYin/p/6971069.html

  10. Vue官方文档中的camelCased (驼峰式) 命名与 kebab-case

    因为html特性中 元素的 prop是不区分大小写的 所以不管html中怎么大写小写变化,下面的组件的prop应该写成小写 Vue中有这样一种设定: props中如果使用为kebab-case命名方式 ...