#include <iostream> #include <cstring> #include <cstdio> #include <queue> #define mem(a,b) memset(a,b,sizeof(a)) using namespace std; const int maxn = 100010, INF = 0x7fffffff; int d[maxn], head[maxn], in[maxn]; int n, m, s, t; struct node{ int u, v, c, f, next, bz; //bz为标记是否为原图中的路 1 是 0 不是 }Node[maxn]; void add(int u, int v, int c, int f,int i, int bz) { Node[i].u = u; Node[i].v = v; Node[i].c = c; Node[i].f = f; Node[i].next = head[u]; head[u] = i; Node[i].bz = bz; } int bfs() { queue<int> Q; mem(d,0); Q.push(s); d[s] = 1; while(!Q.empty()) { int u = Q.front(); Q.pop(); for(int i=head[u]; i!=-1; i=Node[i].next) { node e = Node[i]; if(!d[e.v] && e.c > e.f) { d[e.v] = d[e.u] + 1; Q.push(e.v); } } } return d[t] != 0; } int dfs(int u, int cap) { if( u == t) return cap; for(int i=head[u]; i!=-1; i=Node[i].next) { node e = Node[i]; if(d[e.v] == d[e.u] + 1 && e.c > e.f) { int V = dfs(e.v, min(cap, e.c - e.f)); if(V > 0) { Node[i].f += V; Node[i^1].f -= V; return V; } } } return 0; } int Dinic(int u) { int ans = 0; while(bfs()) { while(int l = dfs(u, INF)) ans += l; } return ans; } int main() { mem(head,-1); int s_, t_; //源点和汇点 cin>> n >> m >> s_ >> t_; s = 0; t = n+1; // 设置超级源点 和 超级汇点 int sum = 0, cnt = 0; for(int i=0; i<m; i++) { int u, v, b, d; cin>> u >> v >> b >> d; add(u, v, d-b, 0, cnt++,1); add(v, u, 0, 0, cnt++,1); in[v] += b; in[u] -= b; } for(int i=1; i<=n; i++) { if(in[i] > 0) { sum += in[i]; add(s,i,in[i],0,cnt++,0); add(i,s,0,0,cnt++,0); } else { add(i,t,-in[i],0,cnt++,0); add(t,i,0,0,cnt++,0); } } add(t_,s_,INF,0,cnt++,0); //连接汇点和源点 上界为无穷大 下界为0 add(s_,t_,0,0,cnt++,0); if(sum != Dinic(s)) //如果不等于 说明没有可行流 { cout<< "please go home to sleep" <<endl; } else //如果有可行流 则删除超级源点和超级汇点 跑一次Dinic 即可 { sum = Node[head[t_]^1].c - Node[head[t_]^1].f; for(int i=0; i<cnt; i++) { if(!Node[i].bz) Node[i].v = 0; } head[s] = head[t] = -1; s = s_; t = t_; printf("%d\n",sum + Dinic(s)); } return 0; }