终于照着模板打了一遍前向星……
虽然还不是太懂……
直接上代码吧……依然是poj1273的题
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#define M 400
int INF=10000001;
using namespace std;
struct node
{
int u,v,cap,next;
};
node edge[M*M];
int next[M],head[M],d[M],vis[M];
int ecnt;
void init()
{
ecnt=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int c)
{
edge[ecnt].u=v;
edge[ecnt].v = v;
edge[ecnt].cap = c;
edge[ecnt].next = head[u];
head[u] = ecnt++;
edge[ecnt].u = v;
edge[ecnt].v = u;
edge[ecnt].cap = 0;
edge[ecnt].next = head[v];
head[v] = ecnt++;
}
bool bfs(int begin,int end)
{
int i,l,h,k,y;
for(i=0;i<=end;i++)d[i]=-1;
d[begin]=0;
queue<int> q;
q.push(begin);
while(!q.empty())
{
k=q.front();
q.pop();
for(i=head[k];i!=-1;i=edge[i].next)
{
y=edge[i].v;
if(edge[i].cap>0 && d[y]==-1)
{
d[y]=d[k]+1;
if(y==end)return true;
q.push(y);
}
}
}
return false;
}
int dfs(int x,int exp,int end)
{
vis[x]=1;
if(x==end)return exp;
int y,temp,i;
for(i=next[x];i!=-1;i=edge[i].next,next[x]=i)
{ y=edge[i].v;
if(edge[i].cap>0 && d[y]==d[x]+1 && vis[y]==0)
if(temp=(dfs(y,min(exp,edge[i].cap),end))>0)
{
edge[i].cap-=temp;
edge[i^1].cap+=temp;
return temp;
}
}
return 0;
}
int dinic(int begin,int end)
{
int i,ans=0,flow;
while(bfs(begin,end))
{
for(i=0;i<=end;i++)next[i]=head[i];
while(true)
{
for(i=0;i<=end;i++)vis[i]=0;
flow=dfs(begin,INF,end);
if(flow==0)break;
ans+=flow;
}
}
return ans;
}
int main()
{ int i,n,m;
while(~scanf("%d%d",&m,&n))
{ init();
for(i=1;i<=m;i++)
{ int u,v,c;
scanf("%d%d%d",&u,&v,&c);
add(u,v,c);
}
int sum=dinic(1,n);
printf("%d\n",sum);
}
return 0;
}