1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #include <bits/stdc++.h>
using namespace std;
const int MAXN=5e3+10; const int INF=0x3f3f3f3f;
int n,m,s,t,minc,maxf; int flow[MAXN],lst[MAXN],pre[MAXN]; int dis[MAXN],vis[MAXN];
struct edge_t{ int v,w,c,rid; };
vector<edge_t> G[MAXN];
inline void add(int u,int v,int w,int c){ int id=G[u].size(); int rid=G[v].size(); G[u].push_back({v,w,c,rid}); G[v].push_back({u,0,-c,id}); }
bool spfa(){ queue<int> q; memset(dis,0x3f,sizeof dis); memset(vis,0,sizeof vis); q.push(s);dis[s]=0;vis[s]=1;flow[s]=INF; while(!q.empty()){ int u=q.front();q.pop();vis[u]=0; for(int i=0;i<G[u].size();++i){ int v=G[u][i].v,w=G[u][i].w,c=G[u][i].c,rid=G[u][i].rid; if(dis[v]>dis[u]+c&&w>0){ dis[v]=dis[u]+c; flow[v]=min(flow[u],w); lst[v]=u; pre[v]=i; if(!vis[v])q.push(v),vis[v]=1; } } } if(dis[t]==INF)return false; return true; }
void mcmf(){ while(spfa()){ maxf+=flow[t]; minc+=dis[t]*flow[t]; for(int i=t;i!=s;i=lst[i]){ int u=lst[i],v=i,rid=G[u][pre[v]].rid; G[u][pre[v]].w-=flow[t]; G[v][rid].w+=flow[t]; } } }
int main(){ cin>>n>>m>>s>>t; for(int i=1,u,v,w,c;i<=m;++i){ cin>>u>>v>>w>>c; add(u,v,w,c); } mcmf(); cout<<maxf<<' '<<minc<<'\n'; return 0; }
|