Flow Problem
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 9353 Accepted Submission(s): 4367
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)
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
EK代码:
#include <cstdio>
#include <queue>
#include <cstring>
#define SIZE 20
#define INF 1000000000
using namespace std ;
int graph[SIZE][SIZE] , pre[SIZE] , flow[SIZE] ;
int n , m ;
int min(int a , int b)
{
return a>b?b:a ;
}
int bfs(int src , int des)
{
queue<int> que ;
memset(pre,-1,sizeof(pre)) ;
memset(flow,0,sizeof(flow)) ;
flow[src] = INF ;
pre[src] = 0 ;
que.push(src) ;
while(!que.empty())
{
int index = que.front() ;
que.pop() ;
if(index == des)
{
break ;
}
for(int i = 1 ; i <= n ; ++i)
{
if(pre[i] == -1 && graph[index][i]>0)
{
flow[i] = min(graph[index][i],flow[index]) ;
que.push(i) ;
pre[i] = index ;
}
}
}
if(pre[des] == -1)
return -1 ;
else
return flow[des] ;
}
int maxFlow(int src , int des)
{
int increasement = 0 ;
int sum = 0 ;
while((increasement=bfs(src,des))!=-1)
{
int k = des ;
while(k!=src)
{
int last = pre[k] ;
graph[last][k] -= increasement ;
graph[k][last] += increasement ;
k = last ;
}
sum += increasement ;
}
return sum ;
}
int main()
{
int t , c = 1;
scanf("%d",&t) ;
while(t--)
{
scanf("%d%d",&n,&m) ;
memset(graph,0,sizeof(graph)) ;
for(int i = 0 ; i < m ; ++i)
{
int x , y , w ;
scanf("%d%d%d",&x,&y,&w) ;
graph[x][y] += w ;
}
printf("Case %d: %d\n",c++,maxFlow(1,n)) ;
}
return 0 ;
}
Dinic代码:
#include <cstdio>
#include <cstring>
#include <queue>
#define MAX 250
#define INF 1000000000
using namespace std ;
int graph[MAX][MAX] , dis[MAX] ;
int h , r ;
int n , m ;
bool bfs()
{
memset(dis,-1,sizeof(dis)) ;
dis[1] = 0 ;
h = 0 , r = 1 ;
queue<int> que ;
que.push(1) ;
int j ;
while(!que.empty())
{
j = que.front() ;
que.pop() ;
for(int i = 1 ; i <= n ; ++i)
{
if(dis[i]<0 && graph[j][i]>0)
{
dis[i] = dis[j]+1 ;
que.push(i) ;
}
}
}
if(dis[n]>0)
return true ;
return false ;
}
int find(int x , int low)
{
int deta = 0 ;
if(x == n)
return low ;
for(int i = 1 ; i <= n ; ++i)
{
if(graph[x][i]>0 && dis[i]==dis[x]+1 && (deta=find(i,low>graph[x][i]?graph[x][i]:low)))
{
graph[x][i] -= deta ;
graph[i][x] += deta ;
return deta ;
}
}
return 0 ;
}
int main()
{
int t , c = 1;
scanf("%d",&t) ;
while(t--)
{
scanf("%d%d",&n,&m) ;
memset(graph,0,sizeof(graph));
for(int i = 0 ; i < m ; ++i)
{
int x , y , w;
scanf("%d%d%d",&x,&y,&w) ;
graph[x][y] += w ;
}
int ans = 0 ;
while(bfs())
{
int deta = 0 ;
while((deta=find(1,INF)))
{
ans += deta ;
}
}
printf("Case %d: %d\n",c++,ans) ;
}
return 0 ;
}
与君共勉