Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef ONPC
- # define _GLIBCXX_DEBUG
- #define deb(a) cerr << "========" << #a << " = " << a << endl;
- #else
- #define deb(a)
- #endif
- #define sz(a) (int)(a).size()
- #include <bits/stdc++.h>
- using namespace std;
- mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
- typedef long long ll;
- struct Edge
- {
- int from, to, c, f;
- Edge(int from, int to, int c, int f):
- from(from),
- to(to),
- c(c),
- f(f) {}
- };
- const int N = 100005;
- const int INF = 1e9 + 7;
- vector<Edge> e;
- vector<int> g[N];
- int used[N], t, d[N];
- void upd(int ind, int f)
- {
- e[ind].f += f;
- e[ind ^ 1].f -= f;
- }
- int dfs(int v, int can, int cur)
- {
- if (v == t)
- return cur;
- used[v] = 1;
- for (int ind : g[v])
- {
- int u = e[ind].to, f = e[ind].f, c = e[ind].c;
- if (!used[u] && c - f >= can && d[u] == d[v] + 1)
- {
- int len = dfs(u, can, min(cur, c - f));
- if (len)
- {
- upd(ind, len);
- return len;
- }
- }
- }
- return 0;
- }
- vector<int> val;
- vector<vector<int> > ed;
- int dfs2(int v, int cur)
- {
- if (v == t)
- return cur;
- used[v] = 1;
- for (int ind : g[v])
- {
- int u = e[ind].to, f = e[ind].f;
- if (!used[u] && f > 0)
- {
- int len = dfs2(u, min(cur, f));
- if (len)
- {
- ed.back().push_back(ind);
- upd(ind, -len);
- return len;
- }
- }
- }
- return 0;
- }
- void add(int x, int y, int z)
- {
- g[x].push_back(sz(e));
- e.push_back(Edge(x, y, z, 0));
- g[y].push_back(sz(e));
- e.push_back(Edge(y, x, 0, 0));
- }
- int solve()
- {
- int n;
- if (!(cin >> n))
- return 1;
- int m, s;
- cin >> m;
- s = n;
- t = n + 1;
- e.clear();
- for (int i = 0; i <= t; i++)
- g[i].clear();
- val.clear();
- ed.clear();
- for (int i = 0; i < m; i++)
- {
- int x, y, l, r;
- cin >> x >> y >> l >> r;
- x--;
- y--;
- add(x, y, r - l);
- add(s, y, l);
- add(x, t, l);
- }
- ll flow = 0;
- while (true)
- {
- for (int i = 0; i <= t; i++)
- used[i] = 0, d[i] = INF;
- d[s] = 0;
- queue<int> q;
- q.push(s);
- while (q.size())
- {
- int v = q.front();
- q.pop();
- for (int it : g[v])
- {
- int u = e[it].to, f = e[it].f, c = e[it].c;
- if (d[u] > d[v] + 1 && f < c)
- {
- d[u] = d[v] + 1;
- q.push(u);
- }
- }
- }
- if (d[t] == INF)
- break;
- while (true)
- {
- for (int i = 0; i <= t; i++)
- used[i] = 0;
- int x = dfs(s, 1, INF);
- if (!x)
- break;
- else
- flow += x;
- }
- }
- for (int i = 0; i < sz(e); i += 6)
- if (e[i + 2].f != e[i + 2].c || e[i + 4].f != e[i + 4].c)
- {
- cout << "NO\n";
- return 1;
- }
- cout << "YES\n";
- for (int i = 0; i < sz(e); i += 6)
- cout << e[i + 2].c + e[i].f << ' ';
- cout << endl;
- return 1;
- }
- int32_t main()
- {
- ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
- int TET = 1e9;
- //cin >> TET;
- while (TET--)
- {
- if (solve())
- break;
- #ifdef ONPC
- cout << "\n__________________________" << endl;
- #endif
- }
- #ifdef ONPC
- cerr << endl << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
- #endif
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement