Harry Potter and the Forbidden Forest
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2063 Accepted Submission(s): 689
Problem 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.
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
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.
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: 2Case 3: 2
题意:求最小割的最小边数
思路:把第一遍dinic之后把网络中满流量(残量为0)的边的残量改为1,其余的边残量改为无穷大,不含反向边。再跑一次dinic即可得出答案
#include <bits/stdc++.h> using namespace std; #define maxm 400010 #define maxn 1010 #define inf 1<<30 int head[maxm], tot; int n, m; int que[maxn], Layer[maxn]; struct node{ int to, next, cap; }G[maxm]; void add(int u, int v, int cap){ G[tot].to = v, G[tot].cap = cap, G[tot].next = head[u], head[u] = tot++; G[tot].to = u, G[tot].cap = 0, G[tot].next = head[v], head[v] = tot++; } bool bfs(){ int rear = 0; memset(Layer, -1, sizeof Layer); que[rear++] = 0; Layer[0] = 0; for(int i = 0;i < rear;i++){ int u = que[i]; for(int j = head[u];j != -1;j = G[j].next){ int v = G[j].to; if(G[j].cap>0&&Layer[v]==-1){ Layer[v] = Layer[u] + 1; que[rear++] = v; if(v == n-1) return 1; } } } return 0; } int dfs(int u, int maxflow){ if(u == n-1||maxflow==0) return maxflow; int sum = 0; for(int i = head[u];i != -1;i = G[i].next){ int v = G[i].to; if(G[i].cap>0&&Layer[v]==Layer[u]+1){ int t = dfs(v, min(maxflow, G[i].cap)); G[i].cap -= t; G[i^1].cap += t; sum += t; maxflow -= t; } } return sum; } int dinic() { int ans = 0; while(bfs()){ ans += dfs(0, inf); } return ans; } int main() { int u, v, c, d, i, j, T, k = 1; scanf("%d", &T); while(T--){ scanf("%d %d", &n, &m); memset(head, -1, sizeof head); tot = 0; for(i = 0;i < m;i++){ scanf("%d %d %d %d", &u, &v, &c, &d); add(u, v, c); if(d) add(v, u, c); } dinic(); for(i = 0;i < tot;i+=2){ if(G[i].cap==0){ G[i].cap = 1; G[i^1].cap = 0; }else{ G[i].cap = inf; G[i^1].cap = 0; } } printf("Case %d: %d\n", k++, dinic()); } }