Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define sqr(x) ((x) * (x))
- #define pb push_back
- #define mp make_pair
- #define X first
- #define Y second
- #define fin(name) freopen(name, "r", stdin)
- #define fout(name) freopen(name, "w", stdout)
- #define I(x, a) for(auto x : a)
- #define F(i, l, r) for(auto i = l; i < r; ++i)
- #define E(i, l, r) for(auto i = l; i <= r; ++i)
- #define DF(i, l, r) for(auto i = l; i >= r; --i)
- #define clean(a) memset((a),0,sizeof (a))
- #define sync ios_base::sync_with_stdio(0);cin.tie(0)
- #define all(x) (x).begin(),(x).end()
- #define ret return
- #define cont continue
- #define brk break
- #define ins insert
- #define sz(a) ((int)(a).size())
- typedef long long ll;
- typedef unsigned long long ull;
- typedef long double dbl;
- typedef pair <int, int> pii;
- const int inf = (int)1e9;
- const ll linf = (ll)1e18;
- const int mod = (int)1e9 + 7;
- const dbl eps = (dbl)1e-8;
- const int maxn = (int)5e5 + 5;
- const dbl pi = acos(-1);
- int n, D;
- ll res;
- vector <pair <ll, ll> > a[maxn];
- vector <ll> b[maxn];
- ll dfs(int v, ll z, int p = -1) {
- ll bestw = 0;
- F(i, 0, sz(a[v])) {
- int to = a[v][i].X;
- if (to == p)
- cont;
- ll w = a[v][i].Y + b[v][i] * z;
- w += dfs(to, z, v);
- res = max(res, w + bestw);
- bestw = max(bestw, w);
- }
- return bestw;
- }
- ll calc(ll z) {
- res = 0;
- dfs(0, z);
- return res;
- }
- int main() {
- // fin("t.in");
- sync;
- cin >> n >> D;
- F(i, 0, n - 1) {
- int x, y, w, p;
- cin >> x >> y >> w >> p; --x; --y;
- a[x].pb({y, w});
- b[x].pb(p);
- a[y].pb({x, w});
- b[y].pb(p);
- }
- int l = 0, r = D;
- while (l < r) {
- int m = (l + r) / 2;
- ll x1 = calc(m), x2 = calc(m + 1);
- if (x1 <= x2) {
- r = m;
- } else {
- l = m + 1;
- }
- }
- cout << l << endl << calc(l) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement