【HDU - 3987】Harry Potter and the Forbidden Forest 【最小割 -- 最小化割边数】

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<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
#define LL long long

const int N = 1000+11;
const int M = 100000*4+11;
const int inf = 0x3f3f3f3f;

struct Edge {
int form,to,cap,flow,nexts;
}edge[M];
void init(){
top=0;
}

}
int vis[N],dis[N];
int cur[N];
bool bfs(int st,int ed){
queue<int>Q;
memset(vis,0,sizeof(vis));
memset(dis,-1,sizeof(dis));
Q.push(st);vis[st]=1;dis[st]=1;
while(!Q.empty()){
int now=Q.front();Q.pop();
Edge e=edge[i];
if(!vis[e.to]&&e.cap-e.flow>0){
vis[e.to]=1;
dis[e.to]=dis[now]+1;
if(e.to==ed) return 1;
Q.push(e.to);
}
}
}
return 0;
}
int dfs(int now,int a,int ed){
if(a==0||now==ed) return a;
int flow=0,f;
for(int &i=cur[now];i!=-1;i=edge[i].nexts){
Edge &e=edge[i];
if(dis[e.to]==dis[now]+1&&(f=dfs(e.to,min(e.cap-e.flow,a),ed))>0){
e.flow+=f;
flow+=f;
edge[i^1].flow-=f;
a-=f;
if(a==0) break;
}
}
return flow;
}

int max_flow(int st ,int ed){
int flow=0;
while(bfs(st,ed)){
flow+=dfs(st,inf,ed);
}
return flow;
}

int main(){
int t;scanf("%d",&t); int cas=1;
while(t--){
init();
int n,m;scanf("%d%d",&n,&m);
int a,b,c,d;
while(m--){
scanf("%d%d%d%d",&a,&b,&c,&d);
}
max_flow(0,n-1);
for(int i=0;i<top;i+=2){
if(edge[i].cap==edge[i].flow) {
edge[i].cap=1 ;edge[i].flow=0;
}else {
edge[i].cap=inf; edge[i].flow=0;
}
edge[i^1].cap=edge[i^1].flow=0;  // 注意清空反向边
}
printf("Case %d: %d\n",cas++,max_flow(0,n-1));
}
return 0;
}


• 评论

• 上一篇
• 下一篇