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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> #include<algorithm> #include<ctime> #include<iomanip> #include<queue> #include<stack> #include<map> #include<vector> #define gh() getchar() #define re register #define db double typedef long long ll; using namespace std; const int MAXN=501; const int MAXM=2e4+1; const int INF=0x7f7f7f7f; template<class T> inline void read(T &x) { x=0; char ch=gh(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=gh(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=gh(); if(t) x=-x; } template<class T,class ...T1> inline void read(T &x,T1 &...x1) { read(x),read(x1...); } template<class T> inline bool checkMax(T &x,T &y) { return x<y?x=y,1:0; } template<class T> inline bool checkMin(T &x,T &y) { return x>y?x=y,1:0; } int N,M,S,T,Sv,Tv; struct Graph { int next,to,val; Graph(int n=0,int t=0,int v=0):next(n),to(t),val(v){} }Edge[MAXM<<1]; int Head[MAXN],Cur[MAXN],Total=1; inline void addEdge(int u,int v,int w) { Edge[++Total]=Graph(Head[u],v,w);Head[u]=Total; Edge[++Total]=Graph(Head[v],u,0);Head[v]=Total; } int A[MAXN],Fl[MAXN]; inline bool Bfs() { memset(Fl,-1,sizeof(Fl)); Fl[Sv]=0,Cur[Sv]=Head[Sv]; queue<int>Q; Q.push(Sv); while(!Q.empty()) { int x=Q.front();Q.pop(); for(int e=Head[x];e;e=Edge[e].next) { int v=Edge[e].to; if(Fl[v]==-1&&Edge[e].val) { Fl[v]=Fl[x]+1; Cur[v]=Head[v]; if(v==Tv) return 1; Q.push(v); } } } return 0; } int Dfs(int x,int inf) { if(x==Tv) return inf; int flow=0; for(int e=Cur[x];e&&flow<inf;e=Edge[e].next) { Cur[x]=e; int v=Edge[e].to; if(Fl[v]==Fl[x]+1&&Edge[e].val) { int k=Dfs(v,min(Edge[e].val,inf-flow)); if(!k) Fl[v]=-1; Edge[e].val-=k,Edge[e^1].val+=k,flow+=k; } } return flow; } inline int Dinic() { int r=0,flow; while(Bfs()) while(flow=Dfs(Sv,INF)) r+=flow; return r; } int main() { read(N,M,S,T); Sv=0,Tv=N+1; for(int i=1,u,v,w,c;i<=M;++i) { read(u,v,w,c); addEdge(u,v,c-w); A[u]-=w,A[v]+=w; } int Tot=0; for(int i=1;i<=N;++i) if(A[i]>0) addEdge(Sv,i,A[i]),Tot+=A[i]; else if(A[i]<0) addEdge(i,Tv,-A[i]); addEdge(T,S,INF); if(Dinic()<Tot) puts("please go home to sleep"); else { int res=Edge[Total].val; Sv=S,Tv=T; Edge[Total].val=Edge[Total-1].val=0; printf("%d\n",res+Dinic()); } return 0; }
|