HDoj 3549 Flow Problem【最大ストリーム入門dinicアルゴリズム】

4146 ワード

Flow Problem
Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 9735    Accepted Submission(s): 4562
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

 
最初の最大ストリーム、dinicアルゴリズム:
 
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <vector>
#define MAXN 100+10
#define MAXM 2000+10
#define INF 10000000+10
using namespace std;
struct Edge
{
	int from, to, cap, flow, next;
}edge[MAXM];
int n, m;
int dist[MAXN];//             
int top, head[MAXN], vis[MAXN];
int cur[MAXN];//cur[i]  i                         head 
void init()
{
	top = 0;
	memset(head, -1, sizeof(head));
}
void addedge(int a, int b, int c)
{
	Edge E1 = {a, b, c, 0, head[a]};
	edge[top] = E1;
	head[a] = top++;
	Edge E2 = {b, a, 0, 0, head[b]};
	edge[top] = E2;
	head[b] = top++;
}
void getmap()
{
	int a, b, c;
	while(m--)
	{
		scanf("%d%d%d", &a, &b, &c);
		addedge(a, b, c);
	}
}
bool BFS(int start, int end)
{
//         start->end   (        ,           -    ,           =    ,   0)   
//                          false        	
	int i;
	memset(dist, -1, sizeof(dist));
	memset(vis, 0, sizeof(vis));
	queue<int> Q;
	while(!Q.empty()) Q.pop();
	Q.push(start);
	vis[start] = 1;
	dist[start] = 0;
	while(!Q.empty())
	{
		int u = Q.front();
		Q.pop();
		for(i = head[u]; i != -1; i = edge[i].next)
		{
			Edge E = edge[i];
			if(!vis[E.to] && E.cap > E.flow)
			{
				vis[E.to] = 1;
				dist[E.to] = dist[u] + 1;
				if(E.to == end) return true;
				Q.push(E.to); 
			}
		}
	}
	return false;
}
int DFS(int x, int a, int end)//         start->end         a 
{
	if(x == end || a == 0) return a;
	int flow = 0, f;
	for(int& i = cur[x]; i != -1; i = edge[i].next)//        
	{
		Edge& E = edge[i];
		if(dist[x]+1 == dist[E.to] && (f = DFS(E.to, min(a, E.cap-E.flow), end)) > 0)
		{
			E.flow += f;
			edge[i^1].flow -= f;
			flow += f;
			a -= f;
			if(a == 0) break;
		}
	}
	return flow;
}
int Maxflow(int start, int end)
{
	int flow = 0;
	while(BFS(start, end))//  1 n      
	{
		memcpy(cur, head, sizeof(head));
		flow += DFS(start, INF, end);
	}
	return flow;
}
int main()
{
	int t;
	int k = 1;
	scanf("%d", &t);
	while(t--)
	{
		scanf("%d%d", &n, &m);
		init();
		getmap();
		printf("Case %d: %d
", k++, Maxflow(1, n)); } return 0; }