标签:
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define M 1000005
#define N 1000005
#define INF 0x3f3f3f3f
using namespace std;
int cnt,n,m,S,T;
int s,t,v,ans;
struct node
{
int from,to,val,next;
}edge[M],a[M];
int head[N],dep[N];
void init()
{
memset(head,-1,sizeof(head)),cnt=0;
}
void edgeadd(int from,int to,int val)
{
edge[cnt].from=from,edge[cnt].to=to,edge[cnt].val=val,edge[cnt].next=head[from],head[from]=cnt++;
edge[cnt].from=to,edge[cnt].to=from,edge[cnt].val=0,edge[cnt].next=head[to],head[to]=cnt++;
}
int bfs(int s,int t)
{
queue<int>q;
memset(dep,0,sizeof(dep));
q.push(s),dep[s]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
if(u==t)return 1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int to=edge[i].to;
if((!edge[i].val)||dep[to]!=0)continue;
dep[to]=dep[u]+1;
q.push(to);
}
}
return 0;
}
int dfs(int s,int max_vale)
{
int ret=0,tmp;
if(s==T)return max_vale;
for(int i=head[s];i!=-1;i=edge[i].next)
{
int to=edge[i].to;
if(dep[to]!=dep[s]+1||edge[i].val==0)continue;
tmp=dfs(to,min(max_vale-ret,edge[i].val));
edge[i].val-=tmp;
edge[i^1].val+=tmp;
ret+=tmp;
if(ret==max_vale)return ret;
}
return ret;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)scanf("%d%d%d",&a[i].from,&a[i].to,&a[i].val);
scanf("%d%d%d",&s,&t,&v);
S=s,T=t;
init();
for(int i=1;i<=m;i++)
{
if(a[i].val>v)edgeadd(a[i].from,a[i].to,1),edgeadd(a[i].to,a[i].from,1);
}
while(bfs(S,T))
while(int t=dfs(S,INF))
ans+=t;
init();
for(int i=1;i<=m;i++)
{
if(a[i].val<v)edgeadd(a[i].from,a[i].to,1),edgeadd(a[i].to,a[i].from,1);
}
while(bfs(S,T))
while(int t=dfs(S,INF))
ans+=t;
printf("%d\n",ans);
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/wzq_qwq/article/details/47045203