【hdu 3987】Harry Potter and the Forbidden Forest
【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的更多相关文章
- 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题
[HDU 3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】
[把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...
- HDU 3987 Harry Potter and the Forbidden Forest(边权放大法+最小割)
Harry Potter and the Forbidden Forest Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/ ...
- 【HDU 2196】 Computer(树的直径)
[HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...
- 【HDU 2196】 Computer (树形DP)
[HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...
- 【HDU 5145】 NPY and girls(组合+莫队)
pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...
- 【hdu 1043】Eight
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...
- 【HDU 3068】 最长回文
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...
随机推荐
- luoguP1462通往奥格瑞玛的道路(二分答案+spfa)
题意 给出n个点m条边的无向图. 每条边有两个权值a,b; 问在保证从1到n的路径a权值和小于x时,路径上b权值最大值最小为多少. (n≤10000,m≤50000,x≤1000000000) 题解 ...
- CF209C Trails and Glades(欧拉路)
题意 最少添加多少条边,使无向图有欧拉回路. n,m≤106 题解 求出每个点的度数 奇度数点需要连一条新边 仅有偶度数点的连通块需要连两条新边 答案为上面统计的新边数 / 2 注意:此题默认以1为起 ...
- easyui_datagrid使用
easyui的datagrid显示数据的方式(使用了jQuery) 第一步 创建显示的格式,方法有两种: 第一种:在HTML标签中创建,类似如下的形式,参数可以在标签中设置,也可以在脚本中 这种方式在 ...
- info---Linux下info格式的帮助指令。
info命令是Linux下info格式的帮助指令. 它的几个常用快捷键. ?键:它就会显示info的常用快捷键. N键:显示(相对于本节点的)下一节点的文档内容. P键:显示(相对于本节点的)前一节点 ...
- shell清除日志小脚本
#!/bin/bash #清除日志脚本 LOG_DIR=/var/log ROOT_UID=0 #用户id为0的 ,即为root if [ "$UID" -ne "$RO ...
- BNU 34974 MATLAB大法好
题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=34974 MATLAB大法好 Time Limit: 8000ms Memory Limi ...
- HTML5学习笔记简明版(11):新增的API
HTMLDocument上的扩展 HTML5在DOM Level 2 HTML上扩展了一些HTMLDocument的接口.这些接口在全部实现了Document接口的元素对象上进行了实现. HTML5在 ...
- POJ - 3984 - 迷宫问题 (DFS)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10936 Accepted: 6531 Description ...
- OpenCV【2】---读取png图片显示到QT label上的问题
问题一: 操作图片test.png是一个365x365的PNG图片 通过OpenCV自带的GUI显示出来图像是没问题的,例如以下操作代码所看到的: QStringfileName=QFileD ...
- Delphi的参数修饰const/var/output 与C++的对应关系
delphi的const/input和默认的没有修饰, C++都是一样的 delphi的var,对应C++那边是指针, 调用方需要管理内存(负责分配内存及销毁) delphi的output , 对应 ...