qfbgsm @ 2019-08-15 16:00:15
#include<iostream>
#include<cstring>
#include<math.h>
#include<stdlib.h>
#include<cstring>
#include<cstdio>
#include<utility>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=1e5+10;
int dep[maxn],head[maxn],vis[maxn];//层次
int n,m,s,t;
const int inf=0x3f3f3f3f;
int maxflow=0;
int cnt=1;
struct node{
int to,val,next;
}star[500100];
void add(int u,int v,int w){//前向星
star[cnt].to=v;
star[cnt].val=w;
star[cnt].next=head[u];
head[u]=cnt++;
}
bool bfs(){//分层
memset(dep,inf,sizeof(dep));
memset(vis,0,sizeof(vis));
dep[s]=0;vis[s]=1;
queue<int>q;q.push(s);
while(!q.empty()){
int from=q.front();
q.pop();
vis[from]=0;
for(int i=head[from];i!=-1;i=star[i].next){
int to=star[i].to;
if(dep[to]>=dep[from]+1&&star[i].val){//star[i].val!=0
dep[to]=dep[from]+1;
if(!vis[to]){
q.push(to);
vis[to]=1;
}
}
}
}
if(dep[t]!=inf)return 1;
else return 0;
}
int dfs(int from,int flow){
int rlow=0;
if(from==t)return flow;
for(int i=head[from];i!=-1;i=star[i].next){
int to=star[i].to;
if(star[i].val&&dep[to]==dep[from]+1){
if(rlow=dfs(to,min(flow,star[i].val))){
star[i].val-=rlow;
star[i^1].val+=rlow;//二进制异或,相同为0,相异为1
return rlow;
}
}
}
return 0;
}
int dinic(){
int lowflow=0;//当前增广路能通过的最大流量
while(bfs()){
while(lowflow=dfs(s,inf))maxflow+=lowflow;
}
return maxflow;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n>>m>>s>>t;
int u,v,w;
memset(head,-1,sizeof(head));
for(int i=1;i<=m;i++){
cin>>u>>v>>w;
add(u,v,w);
add(v,u,0);//反向边
}
cout<<dinic();
return 0;
}
by AlexWaker @ 2019-09-01 18:31:19
我猜你是2 9 10没过 边数组定义小了,第二个测试数据有10万条边,你需要至少定义20万条边的数组。 我刚开始也是边定义少了,全局变量总是莫名其妙地被改变,扩容了之后结果就对了