参考题目:loj117
解析:
先扔一个板子。改天更新,如有不足欢迎指正。
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
#define gc getchar
#define pc putchar
#define cs const
#define st static
inline
ll getint(){
st ll num;
st char c;
for(c=gc();!isdigit(c);c=gc());
for(num=0;isdigit(c);c=gc())num=(num<<1)+(num<<3)+(c^48);
return num;
}
inline
void outint(ll a){
st char ch[23];
if(a==0)return (void)pc('0');
while(a)ch[++ch[0]]=(a-a/10*10)^48,a/=10;
while(ch[0])pc(ch[ch[0]--]);
}
int n,m;
int ss,tt;
ll in[50010];
int last[50010],nxt[350100],to[350100],ecnt=1;
ll cap[350100];
inline
void addedge(cs int &u,cs int &v,cs ll &val){
nxt[++ecnt]=last[u],last[u]=ecnt,to[ecnt]=v,cap[ecnt]=val;
nxt[++ecnt]=last[v],last[v]=ecnt,to[ecnt]=u,cap[ecnt]=0;
}
int lev[50010],cur[50010];
inline
bool BFS(){
memset(lev,-1,sizeof lev);
for(int re i=ss;i<=tt;++i)cur[i]=last[i];
queue<int > q;
q.push(ss),lev[ss]=0;
while(!q.empty()){
int u=q.front();
q.pop();
for(int re e=last[u],v=to[e];e;e=nxt[e],v=to[e]){
if(cap[e]&&lev[v]==-1){
lev[v]=lev[u]+1;
if(v==tt)return true;
q.push(v);
}
}
}
return false;
}
inline
ll Dinic(cs int &u,cs ll &flow){
if(u==tt)return flow;
ll ans=0;
for(int &e=cur[u],v=to[e];e;e=nxt[e],v=to[e]){
if(cap[e]&&lev[v]>lev[u]){
ll delta=Dinic(v,min(flow-ans,cap[e]));
if(delta){
cap[e]-=delta;
cap[e^1]+=delta;
ans+=delta;
if(ans==flow)return ans;
}
}
}
lev[u]=-1;
return ans;
}
inline
ll maxflow(){
ll ans=0;
while(BFS())ans+=Dinic(ss,(1ll<<62)-1);
return ans;
}
int main(){
int s,t;
n=getint();
m=getint();
s=getint();
t=getint();
for(int re i=1;i<=m;++i){
int u=getint(),v=getint();
ll l=getint(),r=getint();
in[u]+=l;in[v]-=l;
addedge(u,v,r-l);
}
tt=n+1;
ll sum=0;
for(int re i=1;i<=n;++i){
if(in[i]>0)sum+=in[i],addedge(i,tt,in[i]);
if(in[i]<0)addedge(ss,i,-in[i]);
}
ll flow=maxflow();
addedge(t,s,(1ll<<62)-1);
flow+=maxflow();
if(flow!=sum)puts("please go home to sleep");
else outint(cap[ecnt]);
return 0;
}