最后还是照着题解A了这道题……
#include <bits/stdc++.h>
using namespace std;
int n,m,s,t,edge_sum=1;
int maxflow,mincost;
int dis[5005],head[5005],incf[5005],pre[5005];
bool vis[5005];
struct Edge {
int next,to,dis,flow;
}edge[1000005];
inline void addedge(int from,int to,int flow,int dis) {
edge[++edge_sum].next=head[from];
edge[edge_sum].to=to;
edge[edge_sum].dis=dis;
edge[edge_sum].flow=flow;
head[from]=edge_sum;
}
inline bool spfa() {
queue <int> q;
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
q.push(s);
dis[s]=0;
vis[s]=1;
incf[s]=1 << 30;
while(!q.empty()) {
int u=q.front();
vis[u]=0;
q.pop();
for(register int i=head[u];i;i=edge[i].next) {
if(!edge[i].flow) continue;
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].dis) {
dis[v]=dis[u]+edge[i].dis;
incf[v]=min(incf[u],edge[i].flow);//更新incf
pre[v]=i;
if(!vis[v]) vis[v]=1,q.push(v);
}
}
}
if(dis[t]==1061109567) return 0;
return 1;
}
inline void MCMF() {
while(spfa()) {
int x=t;
maxflow+=incf[t];
mincost+=dis[t]*incf[t];
int i;
while(x!=s) {
edge[i].flow-=incf[t];
edge[i^1].flow+=incf[t];
x=edge[i^1].to;
}
}
}
signed main() {
scanf("%d%d%d%d",&n,&m,&s,&t);
for(register int u,v,w,x,i=1;i<=m;++i) {
scanf("%d%d%d%d",&u,&v,&w,&x);
addedge(u,v,w,x);
addedge(v,u,0,-x);
}
MCMF();
printf("%d %d\n",maxflow,mincost);
return 0;
标签:P3381,int,flow,vis,edge,incf,dis
From: https://www.cnblogs.com/zan-mei-tai-yang/p/18492458