【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 ...
随机推荐
- [POI2008]POD-Subdivision of Kingdom(搜索+状压)
题意 给定一个n个点的无向图,要求将点集分成大小相等的两个子集,使两个子集之间的边数最少 (n<=26) 题解 一开始想了半天DP发现不会,去看题解全是搜索. 所以发现C(1326)可以过我就写 ...
- PHP -Casbin: 支持 ACL、RBAC、ABAC 多种模型的 PHP 权限管理框架
PHP-Casbin 是一个用 PHP 语言打造的轻量级开源访问控制框架( https://github.com/php-casbin... ),目前在 GitHub 开源.PHP-Casbin 采用 ...
- 紫书 例题 10-6 UVa 1635 (二项式定理+唯一分解定理)
首先可以发现按照题目的算法最后得出来是一个杨辉三角 如果ai的系数是m的倍数,那么i即为答案 因为这个系数可能很大,而我们只需要判断倍数 所以我们就把m分解质因数,然后判断每一个系数 的质因数的幂是不 ...
- Spring Cloud学习笔记【九】配置中心Spring Cloud Config
Spring Cloud Config 是 Spring Cloud 团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分.其中服务端 ...
- 解决Python 插查 MySQL 时中文乱码问题
首先找到这里的解决方法, count = cursor.fetchall() for i in count: idc_a = i[0] if isinstance(idc_a, unicode): i ...
- jQuery模拟输出回车键
jQuery模拟输出回车键 学习了:https://zhidao.baidu.com/question/1753748968579760068.html 原文少了个r var e = jQuery.E ...
- CXF实战之在Tomcat中公布Web Service(二)
服务接口及实现类请參考WebService框架CXF实战(一) 创建Maven Web项目,在pom.xml中加入CXF和Spring Web的引用,因为CXFServlet须要Spring Web的 ...
- Android中ViewPager的使用
我们在一个apk中第一次開始的时候,会有一个guide界面.一般使用ViewPager来完毕. 布局文件例如以下: <? xml version="1.0" encoding ...
- SpringMVC+MyBatis (druid、logback)
数据库连接池是阿里巴巴的druid.日志框架式logback 1.整合SpringMVCspringMybatis-servlet.xml: <?xml version="1.0&qu ...
- JS取出两个数组的不同或相同元素
JS合并两个数组的方法 我们在项目过程中,有时候会遇到需要将两个数组合并成为一个的情况.比如: var a = [1,2,3]; var b = [4,5,6]; 有两个数组a.b,需求是将两个数组合 ...