# HDU 3987 Harry Potter and the Forbidden Forest

Description

Harry Potter notices some Death Eaters try to slip into Castle. The Death Eaters hide in the most depths of Forbidden Forest. Harry need stop them as soon as.

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

Input consists of several test cases.

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

For each test case:
Output the case number and the answer of how many roads are blocked at least.

Sample Input

3

4 5
0 1 3 0
0 2 1 0
1 2 1 1
1 3 1 1
2 3 3 1

6 7
0 1 1 0
0 2 1 0
0 3 1 0
1 4 1 0
2 4 1 0
3 5 1 0
4 5 2 0

3 6
0 1 1 0
0 1 2 0
1 1 1 1
1 2 1 0
1 2 1 0
2 1 1 1

Sample Output

Case 1: 3
Case 2: 2
Case 3: 2

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

const int maxn=1100;
const int maxm=500000+10;

const int INF=0x3f3f3f3f;

int min(int x,int y)
{
if(x<y)
return x;
else
return y;
}

struct node {
int u, v, cap, flow, next;
};
node edge[maxm];
bool vis[maxn];
int cnt;

void init(){
cnt = 0;
}

void add(int u, int v, int w){
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].cap=w;
edge[cnt].flow=0;
edge[cnt].u=v;
edge[cnt].v=u;
edge[cnt].cap=0;
edge[cnt].flow=0;
}

bool BFS(int st, int ed){
queue<int>q;
memset(vis, 0 ,sizeof(vis));
memset(dist, -1, sizeof(dist));
q.push(st);
dist[st] = 0;
vis[st] = 1;
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; i != -1; i = edge[i].next){
node E = edge[i];
if(!vis[E.v] && E.cap > E.flow){
vis[E.v] = 1;
dist[E.v] = dist[u] + 1;
if(E.v == ed) return true;
q.push(E.v);
}
}
}
return false;
}

int DFS(int x, int ed, int a){
if(a == 0 || x == ed)
return a;
int flow = 0, f;
for(int &i = cur[x]; i != -1; i = edge[i].next){
node &E = edge[i];
if(dist[E.v] == dist[x] + 1 && (f = DFS(E.v, ed, min(a, E.cap - E.flow))) > 0){
E.flow += f;
edge[i ^ 1].flow -= f;
a -= f;
flow += f;
if(a == 0) break;
}
}
return flow;
}

int maxflow(int st, int ed){
int flow = 0;
while(BFS(st, ed)){
flow += DFS(st, ed, INF);
}
return flow;
}

int main()
{
int t,icase;
scanf("%d",&t);
for(icase=1;icase<=t;icase++)
{
init();
int n,m;
scanf("%d%d",&n,&m);
int i,j;
for(i=0;i<m;i++)
{
int u,v,c,d;
scanf("%d%d%d%d",&u,&v,&c,&d);
if(d==1)
}
maxflow(0,n-1);
for(i=0;i<cnt;i+=2)
{
if(edge[i].cap==edge[i].flow)
{
edge[i].cap=1;
edge[i].flow=0;
edge[i^1].cap=0;
edge[i^1].flow=0;
}
else
{
edge[i].cap=INF;
edge[i].flow=0;
edge[i^1].cap=0;
edge[i^1].flow=0;
}
}
int ans=maxflow(0,n-1);
printf("Case %d: %d\n",icase,ans);
}
return 0;
}

• 本文已收录于以下专栏：

## HDU 3987 Harry Potter and the Forbidden Forest（最小割中的最少割边）经典

Harry Potter and the Forbidden Forest Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536...
• u010372095
• 2015年08月02日 21:04
• 689

## Harry Potter and the Forbidden Forest(割边最小的最小割)

Harry Potter and the Forbidden Forest Time Limit:3000MS    Memory Limit:65536KB    64bit IO Forma...
• ZSGG_ACM
• 2015年08月01日 00:36
• 484

## 【最小割】HDU 3987 Harry Potter and the Forbidden Forest

• u012749539
• 2014年08月09日 22:38
• 559

## HDU 3987 Harry Potter and the Forbidden Forest 最小割

/* 很经典的最大流最小割的题目 题意：求最小割，但因为最小割是不唯一的，题目要求得到最小割的条件下使得割边最少 搜到usaco类似的一个题目才出的，构造很巧妙 建边的时候每条边权 w=w*(E+1...
• wsniyufang
• 2011年08月31日 22:08
• 765

## 【HDU】3987 Harry Potter and the Forbidden Forest 最小割

Harry Potter and the Forbidden Forest Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536...
• u013368721
• 2014年07月07日 17:04
• 509

## 【hdu 3987】Harry Potter and the Forbidden Forest

• harlow_cheng
• 2017年08月13日 23:06
• 57

## hdu 3987 Harry Potter and the Forbidden Forest （最小割）

Harry Potter and the Forbidden Forest Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536...
• clover_hxy
• 2017年01月15日 17:42
• 271

## hdu 3987 Harry Potter and the Forbidden Forest

• zxy_snow
• 2011年08月31日 22:28
• 1203

## hdu 3987 Harry Potter and the Forbidden Forest 最小割

• CHCXCHC
• 2014年12月02日 21:55
• 557

## hdu 3987 Harry Potter and the Forbidden Forest【网路流最小割模型】

Harry Potter and the Forbidden Forest Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536...
• letterwuyu
• 2014年12月01日 21:53
• 591

举报原因： 您举报文章：HDU 3987 Harry Potter and the Forbidden Forest 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)