Advertisement
mhuman

Untitled

Jul 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define sqr(x) ((x) * (x))
  6. #define pb push_back
  7. #define mp make_pair
  8. #define X first
  9. #define Y second
  10. #define fin(name) freopen(name, "r", stdin)
  11. #define fout(name) freopen(name, "w", stdout)
  12. #define I(x, a) for(auto x : a)
  13. #define F(i, l, r) for(auto i = l; i < r; ++i)
  14. #define E(i, l, r) for(auto i = l; i <= r; ++i)
  15. #define DF(i, l, r) for(auto i = l; i >= r; --i)
  16. #define clean(a) memset((a),0,sizeof (a))
  17. #define sync ios_base::sync_with_stdio(0);cin.tie(0)
  18. #define all(x) (x).begin(),(x).end()
  19. #define ret return
  20. #define cont continue
  21. #define brk break
  22. #define ins insert
  23. #define sz(a) ((int)(a).size())
  24.  
  25. typedef long long ll;
  26. typedef unsigned long long ull;
  27. typedef long double dbl;
  28. typedef pair <int, int> pii;
  29.  
  30. const int inf = (int)1e9;
  31. const ll linf = (ll)1e18;
  32. const int mod = (int)1e9 + 7;
  33. const dbl eps = (dbl)1e-8;
  34. const int maxn = (int)5e5 + 5;
  35. const dbl pi = acos(-1);
  36.  
  37. int n, D;
  38. ll res;
  39. vector <pair <ll, ll> > a[maxn];
  40. vector <ll> b[maxn];
  41.  
  42. ll dfs(int v, ll z, int p = -1) {
  43.     ll bestw = 0;
  44.     F(i, 0, sz(a[v])) {
  45.         int to = a[v][i].X;
  46.         if (to == p)
  47.             cont;
  48.         ll w = a[v][i].Y + b[v][i] * z;
  49.         w += dfs(to, z, v);
  50.         res = max(res, w + bestw);
  51.         bestw = max(bestw, w);
  52.     }
  53.     return bestw;
  54. }
  55.  
  56. ll calc(ll z) {
  57.     res = 0;
  58.     dfs(0, z);
  59.     return res;
  60. }
  61.  
  62. int main() {
  63. //  fin("t.in");
  64.     sync;
  65.     cin >> n >> D;
  66.     F(i, 0, n - 1) {
  67.         int x, y, w, p;
  68.         cin >> x >> y >> w >> p; --x; --y;
  69.         a[x].pb({y, w});
  70.         b[x].pb(p);
  71.         a[y].pb({x, w});
  72.         b[y].pb(p);
  73.     }
  74.     int l = 0, r = D;
  75.     while (l < r) {
  76.         int m = (l + r) / 2;
  77.         ll x1 = calc(m), x2 = calc(m + 1);
  78.         if (x1 <= x2) {
  79.             r = m;
  80.         } else {
  81.             l = m + 1;
  82.         }
  83.     }
  84.     cout << l << endl << calc(l) << endl;
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement