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
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: 2
Case 3: 2
Author
aMR @ WHU
1 //2017-09-18 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 #include <queue> 7 #include <cmath> 8 9 using namespace std; 10 11 const int N = 1500; 12 const int M = 1000000; 13 const int INF = 0x3f3f3f3f; 14 int head[N], tot; 15 struct Edge{ 16 int next, to, w; 17 }edge[M]; 18 19 void add_edge(int u, int v, int w){ 20 edge[tot].w = w; 21 edge[tot].to = v; 22 edge[tot].next = head[u]; 23 head[u] = tot++; 24 25 edge[tot].w = 0; 26 edge[tot].to = u; 27 edge[tot].next = head[v]; 28 head[v] = tot++; 29 } 30 31 void init_edge(){ 32 tot = 0; 33 memset(head, -1, sizeof(head)); 34 } 35 struct Dinic{ 36 int level[N], S, T; 37 void setST(int _S, int _T){ 38 S = _S; 39 T = _T; 40 } 41 bool bfs(){ 42 queue<int> que; 43 memset(level, -1, sizeof(level)); 44 level[S] = 0; 45 que.push(S); 46 while(!que.empty()){ 47 int u = que.front(); 48 que.pop(); 49 for(int i = head[u]; i != -1; i = edge[i].next){ 50 int v = edge[i].to; 51 int w = edge[i].w; 52 if(level[v] == -1 && w > 0){ 53 level[v] = level[u]+1; 54 que.push(v); 55 } 56 } 57 } 58 return level[T] != -1; 59 } 60 int dfs(int u, int flow){ 61 if(u == T)return flow; 62 int ans = 0, fw; 63 for(int i = head[u]; i != -1; i = edge[i].next){ 64 int v = edge[i].to, w = edge[i].w; 65 if(!w || level[v] != level[u]+1) 66 continue; 67 fw = dfs(v, min(flow-ans, w)); 68 ans += fw; 69 edge[i].w -= fw; 70 edge[i^1].w += fw; 71 if(ans == flow)return ans; 72 } 73 if(ans == 0)level[u] = 0; 74 return ans; 75 } 76 int maxflow(){ 77 int flow = 0; 78 while(bfs()) 79 flow += dfs(S, INF); 80 return flow; 81 } 82 }dinic; 83 84 int main() 85 { 86 int T, n, m, kase = 0; 87 scanf("%d", &T); 88 while(T--){ 89 init_edge(); 90 scanf("%d%d", &n, &m); 91 int s = 0, t = n-1; 92 dinic.setST(s, t); 93 int u, v, w, d; 94 while(m--){ 95 scanf("%d%d%d%d", &u, &v, &w, &d); 96 add_edge(u, v, w); 97 if(d)add_edge(v, u, w); 98 } 99 dinic.maxflow(); 100 for(int i = 0; i < tot; i += 2){ 101 if(edge[i].w == 0){ 102 edge[i].w = 1; 103 edge[i^1].w = 0; 104 }else{ 105 edge[i].w = INF; 106 edge[i^1].w = 0; 107 } 108 } 109 printf("Case %d: %d\n", ++kase, dinic.maxflow()); 110 } 111 return 0; 112 }