这是用Markdown写的博文....然而并没有用
昨天被二逼平衡树整了一天。。。整个人都不好了
写个入门网络流这么费劲 我果然够弱
#include <iostream>
#include <queue>
using namespace std;
int G[300][300];
int Prev[300]; //路径上每个节点的前驱节点
bool Visited[300];
int n,m; //m是顶点数目,顶点编号从1开始 1是源,m是汇, n是边数
unsigned Augment()
{
int v;
int i;
deque<int> q;
memset(Prev,0,sizeof(Prev));
memset(Visited,0,sizeof(Visited));
Prev[1] = 0;
Visited[1] = 1;
q.push_back(1);
bool bFindPath = false;
//用bfs寻找一条源到汇的可行路径
while( ! q.empty())
{
v = q.front();q.pop_front();
for( i = 1;i <= m;i ++)
{
if( G[v][i] > 0 && Visited[i] == 0)
{
//必须是依然有容量的边,才可以走
Prev[i] = v;
Visited[i] = 1;
if( i == m )
{
bFindPath = true;
q.clear();
break;
}
else
q.push_back(i);
}
}
}
if( ! bFindPath)
return 0;
int nMinFlow = 999999999;
v = m;
//寻找源到汇路径上容量最小的边,其容量就是此次增加的总流量
while( Prev[v] )
{
nMinFlow = min( nMinFlow,G[Prev[v]][v]);
v = Prev[v];
}
//沿此路径添加反向边,同时修改路径上每条边的容量
v = m;
while( Prev[v] )
{
G[Prev[v]][v] -= nMinFlow;
G[v][Prev[v]] += nMinFlow;
v = Prev[v];
}
return nMinFlow;
}
int main()
{
while (cin >> n >> m )
{
//m是顶点数目,顶点编号从1开始
int i,j,k;
int s,e,c;
memset( G,0,sizeof(G));
for( i = 0;i < n;i ++ )
{
cin >> s >> e >> c;
G[s][e] += c; //两点之间可能有多条边
}
unsigned int MaxFlow = 0;
unsigned int aug;
while( aug = Augment() )
MaxFlow += aug;
cout << MaxFlow << endl;
}
return 0;
}
用dinic在写! 这算是dinic吧 可是为什么人家0ms 而我16ms.....
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int a[201][201];
int b[201];
int d[201];
int n,m;
int team[201],head,tail;
bool bfs()
{
head=tail=0;
memset(d,0,sizeof(d));
d[1]=1;
team[++tail]=1;
while(head<tail)
{
int x=team[++head];
for(int i=1;i<=n;i++)
if(a[x][i]!=0&&d[i]==0)
d[i]=d[x]+1,team[++tail]=i;
}
if(d[n]==0) return false;
else return true;
}
int dfs(int x,int mmin)
{
int tmp;
if(x==n) return mmin;
for(int i=1;i<=n;i++)
if( a[x][i]!=0 && d[x]+1==d[i] && (tmp=dfs(i,min(mmin,a[x][i]))) )
{
a[x][i]-=tmp;
a[i][x]+=tmp;
return tmp;
}
return 0;
}
int main()
{
while(scanf("%d %d",&m,&n)==2)
{
int ans=0;
memset(a,0,sizeof(a));
for(int x,y,z,i=1;i<=m;i++)
scanf("%d %d %d",&x,&y,&z),a[x][y]+=z;
while(bfs())
ans+=dfs(1,10000000);
printf("%d\n",ans);
}
return 0;
}