EK
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define inf 0x3f3f3f3f
using namespace std;
int n,m,s,t;
long long ans;
struct ed {
int to,next;
int val;
} edge[200005];
int tot=1,head[1005];
void add(int u,int v,int w) {
edge[++tot].to=v;
edge[tot].next=head[u];
edge[tot].val=w;
head[u]=tot;
}
int p[1005],e[1005];
bool vis[1005];
bool bfs() {
memset(vis,false,sizeof(vis));
queue<int>q;
q.push(s);
vis[s]=true;
while(!q.empty()) {
int u=q.front();
q.pop();
for(int i=head[u]; i; i=edge[i].next) {
int v=edge[i].to,w=edge[i].val;
if(!vis[v]&&w>0) {
p[v]=u,e[v]=i;
if(v==t)return true;
q.push(v);
vis[v]=true;
}
}
}
return false;
}
void EK() {
while(bfs()) {
int flow=inf;
for(int i=t; i!=s; i=p[i])flow=min(flow,edge[e[i]].val);
for(int i=t; i!=s; i=p[i]) {
edge[e[i]].val-=flow;
edge[e[i]^1].val+=flow;
}
ans+=flow;
}
}
int main() {
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i=1; i<=m; i++) {
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w),add(v,u,0);
}
EK();
printf("%lld",ans);
return 0;
}
提交记录
用时:551ms
Dinic
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define inf 0x3f3f3f3f
using namespace std;
int n,m,s,t;
long long ans;
struct ed {
int to,next,val;
} edge[200005];
int tot=1,head[1005];
void add(int u,int v,int w) {
edge[++tot].to=v;
edge[tot].next=head[u];
edge[tot].val=w;
head[u]=tot;
}
int d[1005];
bool vis[1005];
int cur[1005];
bool bfs() {
memcpy(cur,head,sizeof(head));
memset(d,inf,sizeof(d));
queue<int>q;
q.push(s);
vis[s]=true;
d[s]=0;
while(!q.empty()) {
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u]; i; i=edge[i].next) {
int v=edge[i].to,w=edge[i].val;
if(d[v]>d[u]+1&&w) {
d[v]=d[u]+1;
if(!vis[v]) {
vis[v]=true;
q.push(v);
}
}
}
}
return d[t]!=inf;
}
int dfs(int u,int flow) {
if(u==t) {
ans+=flow;
return flow;
}
int used=0;
for(int i=cur[u]; i; i=edge[i].next) {
cur[u]=i;
int v=edge[i].to,w=edge[i].val;
if(d[v]==d[u]+1&&w>0) {
int rlow=dfs(v,min(flow-used,w));
if(rlow) {
used+=rlow;
edge[i].val-=rlow;
edge[i^1].val+=rlow;
if(used==flow)break;
}
}
}
return used;
}
void Dinic(){
while(bfs())dfs(s,inf);
}
int main() {
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i=1; i<=m; i++) {
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w),add(v,u,0);
}
Dinic();
printf("%lld",ans);
return 0;
}
提交记录
用时:101ms
ISAP
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define inf 0x3f3f3f3f
using namespace std;
int n,m,s,t;
long long ans;
struct ed {
int to,next,val;
} edge[200005];
int tot=1,head[1005];
void add(int u,int v,int w) {
edge[++tot].to=v;
edge[tot].next=head[u];
edge[tot].val=w;
head[u]=tot;
}
int d[1005],g[1005];
bool vis[1005];
void bfs() {
d[t]=0,g[d[t]]++;
queue<int>q;
q.push(t);
vis[t]=true;
while(!q.empty()) {
int u=q.front();
q.pop();
for(int i=head[u]; i; i=edge[i].next) {
int v=edge[i].to;
if(!vis[v]) {
d[v]=d[u]+1,g[d[v]]++;
q.push(v);
vis[v]=true;
}
}
}
}
int cur[1005];
int dfs(int u,int flow) {
if(u==t) {
ans+=flow;
return flow;
}
int used=0;
for(int i=cur[u]; i; i=edge[i].next) {
cur[u]=i;
int v=edge[i].to,w=edge[i].val;
if(d[v]+1==d[u]&&w>0) {
int rlow=dfs(v,min(flow-used,w));
if(rlow) {
used+=rlow;
edge[i].val-=rlow;
edge[i^1].val+=rlow;
if(used==flow)return used;
}
}
}
g[d[u]]--;
if(g[d[u]]==0)d[s]=n+1;
g[++d[u]]++;
return used;
}
void ISAP() {
bfs();
while(d[s]<n){
memcpy(cur,head,sizeof(head));
dfs(s,inf);
}
}
int main() {
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i=1; i<=m; i++) {
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w),add(v,u,0);
}
ISAP();
printf("%lld",ans);
return 0;
}
提交记录
用时:48ms