HDU3987(最小割最少割边)
Harry Potter and the Forbidden Forest
Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2233    Accepted Submission(s): 765
Problem Description

The Forbidden Forest is mysterious. It consists of N nodes numbered from 0 to N-1. All of Death Eaters stay in the node numbered 0. The position of Castle is node n-1. The nodes connected by some roads. Harry need block some roads by magic and he want to minimize the cost. But it’s not enough, Harry want to know how many roads are blocked at least.
Input
The first line is number of test case.
Each test case, the first line contains two integers n, m, which means the number of nodes and edges of the graph. Each node is numbered 0 to n-1.
Following m lines contains information about edges. Each line has four integers u, v, c, d. The first two integers mean two endpoints of the edges. The third one is cost of block the edge. The fourth one means directed (d = 0) or undirected (d = 1).
Technical Specification
1. 2 <= n <= 1000
2. 0 <= m <= 100000
3. 0 <= u, v <= n-1
4. 0 < c <= 1000000
5. 0 <= d <= 1
Output
Output the case number and the answer of how many roads are blocked at least.
Sample Input
Sample Output
Author
//2017-09-18
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cmath> using namespace std; const int N = ;
const int M = ;
const int INF = 0x3f3f3f3f;
int head[N], tot;
struct Edge{
int next, to, w;
}edge[M]; void add_edge(int u, int v, int w){
edge[tot].w = w;
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++; edge[tot].w = ;
edge[tot].to = u;
edge[tot].next = head[v];
head[v] = tot++;
} void init_edge(){
tot = ;
memset(head, -, sizeof(head));
}
struct Dinic{
int level[N], S, T;
void setST(int _S, int _T){
S = _S;
T = _T;
}
bool bfs(){
queue<int> que;
memset(level, -, sizeof(level));
level[S] = ;
que.push(S);
while(!que.empty()){
int u = que.front();
que.pop();
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to;
int w = edge[i].w;
if(level[v] == - && w > ){
level[v] = level[u]+;
que.push(v);
}
}
}
return level[T] != -;
}
int dfs(int u, int flow){
if(u == T)return flow;
int ans = , fw;
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to, w = edge[i].w;
if(!w || level[v] != level[u]+)
continue;
fw = dfs(v, min(flow-ans, w));
ans += fw;
edge[i].w -= fw;
edge[i^].w += fw;
if(ans == flow)return ans;
}
if(ans == )level[u] = ;
return ans;
}
int maxflow(){
int flow = ;
while(bfs())
flow += dfs(S, INF);
return flow;
}
}dinic; int main()
{
int T, n, m, kase = ;
scanf("%d", &T);
while(T--){
init_edge();
scanf("%d%d", &n, &m);
int s = , t = n-;
dinic.setST(s, t);
int u, v, w, d;
while(m--){
scanf("%d%d%d%d", &u, &v, &w, &d);
add_edge(u, v, w);
if(d)add_edge(v, u, w);
}
dinic.maxflow();
for(int i = ; i < tot; i += ){
if(edge[i].w == ){
edge[i].w = ;
edge[i^].w = ;
}else{
edge[i].w = INF;
edge[i^].w = ;
}
}
printf("Case %d: %d\n", ++kase, dinic.maxflow());
}
return ;
}
HDU3987(最小割最少割边)的更多相关文章
- HDU 3251 Being a Hero(最小割+输出割边)
		
Problem DescriptionYou are the hero who saved your country. As promised, the king will give you some ...
 - poj 3204(最小割--关键割边)
		
Ikki's Story I - Road Reconstruction Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 7 ...
 - poj1815Friendship(最小割求割边)
		
链接 题意为去掉多少个顶点使图不连通,求顶点连通度问题.拆点,构造图,对于<u,v>可以变成<u2,v1> <v2,u1>容量为无穷,<u1,u2>容量 ...
 - hdu3987,最小割时求最少割边数
		
题意:求最小割时候割边最少的数量.算法:先求dinic一遍,跑出残网络,再把该网络中满流量(残量为0)的边 残量改为1,其他边残量改为无穷,则再跑一次最大流,所得即为答案.(思,最小割有喝多组,但是要 ...
 - 最小割&网络流应用
		
重要链接 基础部分链接 : 二分图 & 网络流初步 zzz大佬博客链接 : 网络流学习笔记 重点内容:最小割二元关系新解(lyd's ppt) 题目:网络流相关题目 lyd神犇课件链接 : 网 ...
 - hdu 3987 Harry Potter and the Forbidden Forest 求割边最少的最小割
		
view code//hdu 3987 #include <iostream> #include <cstdio> #include <algorithm> #in ...
 - HDU3987 Harry Potter and the Forbidden Forest(边数最少的最小割)
		
方法1:两遍最大流.一遍最大流后,把满流边容量+1,非满流边改为INF:再求最小割即为答案. 我大概想了下证明:能构成最小割的边在第一次跑最大流时都满流,然后按那样改变边容量再求一次最小割,就相当于再 ...
 - HDU 6214.Smallest Minimum Cut 最少边数最小割
		
Smallest Minimum Cut Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Oth ...
 - HDU - 6214:Smallest Minimum Cut(最小割边最小割)
		
Consider a network G=(V,E) G=(V,E) with source s s and sink t t . An s-t cut is a partition of nodes ...
 
随机推荐
- 内置函数_zip()
			
zip() zip()函数用来把多个可迭代对象中的元素压缩到一起,返回一个可迭代的zip对象,其中每个元素都是包含原来的多个可迭代对象对应位置上元素的元组,最终结果中包含的元素个数取决于所有参数序列或 ...
 - Log4Cpp的使用(转)
			
本文介绍如何使用Log4CPP. Log4Cpp介绍 Log4Cpp的Api接口可以在http://log4cpp.sourceforge.net/api/index.html中查询得到. Log4C ...
 - unable to load http://docbook.sourceforge.net/release/xsl/current/html/docbook.xsl
			
问题:unable to load http://docbook.sourceforge.net/release/xsl/current/html/docbook.xsl 解决:yum -y inst ...
 - Android 和 iOS 实现录屏推流的方案整理
			
一.录屏推流实现的步骤 1. 采集数据 主要是采集屏幕获得视频数据,采集麦克风获得音频数据,如果可以实现的话,我们还可以采集一些应用内置的音频数据. 2. 数据格式转换 主要是将获取到的视频和音频转换 ...
 - Android数据存储之SharedPreferences使用
			
SharedPreferences是Android中一种轻型的数据存储类.本质上是基于XML文件进行存储Key-Value键值对的数据,生成的XML文件的目录在/data/data/包名/Shared ...
 - python中的变量和算数运算
			
先说下变量的作用: 用来保存数据,为什么要保存? 后面要使用. 变量的概念: 变量就是用来存储一些信息,供程序以后调用或者操作修改.变量为标记数据提供了一种描述性的名字,以便我们的程序可以被程序的阅读 ...
 - LeetCode:105_Construct Binary Tree from Preorder and Inorder Traversal | 根据前序和中序遍历构建二叉树 | Medium
			
要求:通过二叉树的前序和中序遍历序列构建一颗二叉树 代码如下: struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode ...
 - iOS开发笔记-根据frame大小动态调整fontSize的自适应文本及圆形进度条控件的实现
			
最近同样是新App,设计稿里出现一种圆形进度条的设计,如下: 想了想,圆形进度条实现起来不难,但是其中显示百分比的文本确需要自适应,虽然可以使用时自己设定文本字体的大小,但是这样显得很麻烦,也很low ...
 - C# 窗体间传值
			
Form1: 父窗体, Form2: 子窗体. 1.父窗体接收子窗体的返回值: public partial class Form1: Form { private void btnOpen_Clic ...
 - 关于小窗滑动,父级body也跟随滑动的解决方案(2)
			
当第一次写这个问题的时候,并不知道竟然还会写2,而且(1)也并没有解决问题. 也发现,这个问题,真实也困住了很多人,找到了张鑫旭(http://www.zhangxinxu.com/wordpress ...