标签:pop printf ems += eof span const sizeof include
#include<bits/stdc++.h> using namespace std; const int nmaxn=5000+8,mmaxn=50000+7; bool vis[nmaxn]; int h[nmaxn],dis[nmaxn],ef[nmaxn],pre[nmaxn],num[nmaxn]; struct edge{ int to,nex,fl,co; }e[mmaxn<<1]; int s,t,cnt=0; void add(int u,int v,int cos,int flo){ e[cnt]=(edge){v,h[u],flo,cos}; h[u]=cnt++; } bool spfa(){ memset(dis,0x3f3f3f3f,sizeof(dis)); ef[s]=0x3f3f3f3f; dis[s]=0,vis[s]=1; pre[t]=0; queue<int> q; q.push(s); while(!q.empty()){ int u=q.front(); q.pop();vis[u]=0; for(int i=h[u];i!=-1;i=e[i].nex){ int v=e[i].to; if(e[i].fl&&dis[v]>dis[u]+e[i].co){ dis[v]=dis[u]+e[i].co; pre[v]=u;num[v]=i; ef[v]=min(ef[u],e[i].fl); if(!vis[v]){ q.push(v); vis[v]=1; } } } } return pre[t]; } int main(){ int n,m;scanf("%d%d%d%d",&n,&m,&s,&t); memset(h,-1,sizeof(h)); for(int i=1;i<=m;++i){ int u,v,w,f;scanf("%d%d%d%d",&u,&v,&w,&f); add(u,v,f,w);add(v,u,-f,0); } int ansf=0,ansc=0; while(spfa()){ ansf+=ef[t]; ansc+=ef[t]*dis[t]; for(int i=t;i!=s;i=pre[i]){ e[num[i]].fl-=ef[t]; e[num[i]^1].fl+=ef[t]; } } printf("%d %d",ansf,ansc); return 0; }
标签:pop printf ems += eof span const sizeof include
原文地址:https://www.cnblogs.com/xiaobuxie/p/11391904.html