如图
求出Us 到 Ut 的最大流:
我的思路:用dfs寻找增广链 (如果不知道什么是增广链的话 你可以百度(最大流视频) 找到一个 中国民航院 有一个游庆山的一个视频 )
我的方法如下:
文件:1.txt
0 1 5
0 2 4
0 3 3
1 4 5
1 5 3
2 5 3
2 6 2
3 6 2
4 7 4
5 7 3
6 7 4
我的代码:
#include<iostream>
#include<fstream>
using namespace std;
const int n=8; //0~7个点
const int inf=9999;
int a[n][n]={0},b[n][n]={0};
int c[n]={inf};
int jx=true;
void pr(int n)
{
int i,min=inf;
for(i=0;i<n;i++)
{
//cout<<c[i]<<"->"<<c[i+1]<<":";
if(a[c[i]][c[i+1]]!=0)
{
// cout<<a[c[i]][c[i+1]]<<"("<<b[c[i]][c[i+1]]<<")"<<' ';
if(min>a[c[i]][c[i+1]]-b[c[i]][c[i+1]]) min=a[c[i]][c[i+1]]-b[c[i]][c[i+1]];
}
else
{
// cout<<a[c[i+1]][c[i]]<<' ';
if(min>b[c[i+1]][c[i]]) min=b[c[i+1]][c[i]];
}
}
if(min>0)
{
//我可以执行这一段
for(i=0;i<n;i++)
{
if(a[c[i]][c[i+1]]!=0)
{
b[c[i]][c[i+1]]=b[c[i]][c[i+1]]+min;
}
else
{
b[c[i]][c[i+1]]=b[c[i]][c[i+1]]-min;
}
}
jx=true;
}
//cout<<"min:"<<min<<endl;
}
void fun(int head,int end,int depth=0)
{
//寻找head->i 的一个没有饱和的路
if(depth==0)
{
c[depth]=head;
depth++;
}
int i,j,bestmax;
for(i=0;i<n;i++)
if(a[head][i]!=b[head][i]||a[i][head]!=0)
{
for(j=0;j<depth;j++)
if(c[j]==i) break;
if(j>=depth)
{
c[depth]=i; //符合
if(i==end) pr(depth);
else fun(i,end,depth+1);
}
}
}
int main()
{
int i,j,t;
ifstream out("1.txt");
while(!out.eof())
{
out>>i>>j>>t;
a[i][j]=t;
}
/*
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<i<<"->"<<j<<':'<<b[i][j]<<"\t";
}
}
cout<<endl<<"执行函数后:"<<endl;
*/
while(jx)
{
jx=false;
fun(0,7);
}
int sum=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(b[i][j]!=0)
cout<<i<<"->"<<j<<':'<<b[i][j]<<endl;
}
sum=sum+b[0][i];
}
cout<<endl<<"sum:"<<sum<<endl;
system("pause");
return 0;
}