HDU - 1532 - Drainage Ditches && 3549 - Flow Problem (网络流初步)

Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10875    Accepted Submission(s): 5131


Problem Description
Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. 
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. 
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle. 
 

Input
The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.
 

Output
For each case, output a single integer, the maximum rate at which water may emptied from the pond. 
 

Sample Input
   
   
   
   
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
 

Sample Output
   
   
   
   
50
 

Source
USACO 93
 



AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#define LL long long
#define INF 0x7fffffff
using namespace std;

const int maxn = 205;
int n, m;
int cap[maxn][maxn];
int flow[maxn];
int pre[maxn];

int bfs(int s, int t) {
    memset(pre, -1, sizeof(pre));
    pre[s] = 0;
    flow[s] = INF;
    
    queue<int> que;
    que.push(s);
    while(!que.empty()) {
        int cur = que.front();
        que.pop();
        if(cur == t) break;		//找到增广路径 
        for(int i = 1; i <= m; i++) {
            if(i != s && cap[cur][i] > 0 && pre[i] == -1) {
                pre[i] = cur;	//记录前驱 
                flow[i] = min(cap[cur][i], flow[cur]);
                que.push(i);
            }
        }
    }
    if(pre[t] == -1) return -1;
    else return flow[t];
}

int ek(int s, int t) {
    int inc = 0;
    int ans = 0;
    while((inc = bfs(s, t)) != -1) {
        int k = t;
        while(k != s) {
            cap[pre[k]][k] -= inc;
            cap[k][pre[k]] += inc;
            k = pre[k];
        }
        ans += inc;
    }
    return ans;
}

int main() {
    while(cin >> n >> m) {
        memset(cap, 0, sizeof(cap));
        for(int i = 0; i < n; i++) {
            int u, v, w;
            scanf("%d %d %d", &u, &v, &w);
            if(u == v) continue;
            cap[u][v] += w;
        }
        cout << ek(1, m) << endl;
    }
    return 0;
}









Flow Problem

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 9358    Accepted Submission(s): 4372


Problem Description
Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.
 

Input
The first line of input contains an integer T, denoting the number of test cases.
For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)
Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)
 

Output
For each test cases, you should output the maximum flow from source 1 to sink N.
 

Sample Input
   
   
   
   
2 3 2 1 2 1 2 3 1 3 3 1 2 1 2 3 1 1 3 1
 

Sample Output
   
   
   
   
Case 1: 1 Case 2: 2
 

Author
HyperHexagon
 

Source
HyperHexagon's Summer Gift (Original tasks)
 

 



AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#define LL long long
#define INF 0x7fffffff
using namespace std;

const int maxn = 205;
int n, m;
int cap[maxn][maxn];
int flow[maxn];
int pre[maxn];

int bfs(int s, int t) {
	memset(pre, -1, sizeof(pre));
	pre[s] = 0;
	flow[s] = INF;
	
	queue<int> que;
	que.push(s);
	while(!que.empty()) {
		int cur = que.front();
		que.pop();
		if(cur == t) break; //找到增广路径 
		for(int i = 1; i <= m; i++) {
			if(i != s && cap[cur][i] > 0 && pre[i] == -1) {
				pre[i] = cur;	//记录前驱 
				flow[i] = min(cap[cur][i], flow[cur]);
				que.push(i);
			}
		}
	}
	if(pre[t] == -1) return -1;
	else return flow[t];
}

int ek(int s, int t) {
	int inc = 0;
	int ans = 0;
	while((inc = bfs(s, t)) != -1) {
		int k = t;
		while(k != s) {
			cap[pre[k]][k] -= inc;
			cap[k][pre[k]] += inc;
			k = pre[k];
		}
		ans += inc;
	}
	return ans;
}

int main() {
	int T;
	scanf("%d", &T);
	for(int cas = 1; cas <= T; cas++) {
		cin >> m >> n;
		memset(cap, 0, sizeof(cap));
		for(int i = 0; i < n; i++) {
			int u, v, w;
			scanf("%d %d %d", &u, &v, &w);
			if(u == v) continue;
			cap[u][v] += w;
		}
		cout << "Case " << cas << ": " << ek(1, m) << endl;
	}
	return 0;
}











你可能感兴趣的:(ACM,HDU,网络流)