最小费用最大流
#include<cstdio> #include<cstring> #include<cmath> #include<ctime> #include<iostream> #include<algorithm> #include<queue> #include<set> #define maxint (2147483647) #define l(a) ((a)<<1) #define r(a) ((a)<<1|1) #define b(a) (2<<(a)) #define rep(i,a,b) for(int i=a;i<=(b);i++) #define Rep(e,x) for(edge*e=fir[x];e;e=e->next) #define clr(a) memset(a,0,sizeof(a)) typedef long long ll; using namespace std; int readint(){ int t=0,f=1;char c=getchar(); while(!isdigit(c)){ if(c=='-') f=-1; c=getchar(); } while(isdigit(c)){ t=(t<<3)+(t<<1)+c-'0'; c=getchar(); } return t*f; } const int maxn=5009,maxm=50009; int n,m,S,T,flow,cost,d[maxn],a[maxn]; bool p[maxn]; struct edge{ int v,w,c; edge*next,*r; }e[maxm<<2],*fir[maxn],*cur[maxn],*pre[maxn],*pt=e; void add(int u,int v,int w,int c){ pt->v=v;pt->w=w;pt->c=c; pt->next=fir[u];fir[u]=pt++; } void addedge(int u,int v,int w,int c){ add(u,v,w,c);add(v,u,0,-c); fir[u]->r=fir[v];fir[v]->r=fir[u]; } bool spfa(){ queue<int>Q;Q.push(S);clr(p);p[S]=1; rep(i,1,n) d[i]=maxint;d[S]=0;clr(a);a[S]=maxint; while(!Q.empty()){ int x=Q.front();Q.pop();p[x]=0; Rep(e,x) if(e->w) if(d[e->v]>d[x]+e->c){ d[e->v]=d[x]+e->c; a[e->v]=min(a[x],e->w); pre[e->v]=e; if(!p[e->v]){ Q.push(e->v); p[e->v]=1; } } } return d[T]!=maxint; } void cal(){ while(spfa()){ flow+=a[T];cost+=a[T]*d[T]; for(edge*e=pre[T];e;e=pre[e->r->v]){ e->w-=a[T];e->r->w+=a[T]; } } } int main(){ //freopen("#intput.txt","r",stdin); //freopen("#output.txt","w",stdout); n=readint();m=readint();S=readint();T=readint(); rep(i,1,m){ int u=readint(),v=readint(),w=readint(),c=readint(); addedge(u,v,w,c); } cal(); printf("%d %d\n",flow,cost); //fclose(stdin); //fclose(stdout); return 0; }