/* -*- coding: utf-8 -*- * * 2642.cc: No.2642 Don't cut line! - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 100000; const int BN = 17; const int MAX_K = 100000; const int MAX_P = 100000; const int INF = 1 << 30; const long long LINF = 1LL << 60; /* typedef */ typedef long long ll; typedef queue qi; typedef pair pii; typedef vector vpii; typedef tuple tp4; struct UFT { vector links, ranks, sizes; UFT() {} void init(int n) { links.resize(n); for (int i = 0; i < n; i++) links[i] = i; ranks.assign(n, 1); sizes.assign(n, 1); } int root(int i) { int i0 = i; while (links[i0] != i0) i0 = links[i0]; return (links[i] = i0); } int rank(int i) { return ranks[root(i)]; } int size(int i) { return sizes[root(i)]; } bool same(int i, int j) { return root(i) == root(j); } int merge(int i0, int i1) { int r0 = root(i0), r1 = root(i1), mr; if (r0 == r1) return r0; if (ranks[r0] == ranks[r1]) { links[r1] = r0; sizes[r0] += sizes[r1]; ranks[r0]++; mr = r0; } else if (ranks[r0] > ranks[r1]) { links[r1] = r0; sizes[r0] += sizes[r1]; mr = r0; } else { links[r0] = r1; sizes[r1] += sizes[r0]; mr = r1; } return mr; } }; /* global variables */ tp4 es[MAX_K]; bool used[MAX_K]; UFT uft; vpii nbrs[MAX_N]; int ps[MAX_N][BN], pws[MAX_N][BN], ds[MAX_N], cis[MAX_N]; /* subroutines */ int find_maxw(int u, int v) { if (ds[u] > ds[v]) swap(u, v); int maxw = 0; for (int i = BN - 1; i >= 0; i--) if (((ds[v] - ds[u]) >> i) & 1) { maxw = max(maxw, pws[v][i]); v = ps[v][i]; } if (u == v) return maxw; for(int i = BN - 1; i >= 0; i--) if (ps[u][i] != ps[v][i]) { maxw = max(maxw, max(pws[u][i], pws[v][i])); u = ps[u][i], v = ps[v][i]; } return max(maxw, max(pws[u][0], pws[v][0])); } /* main */ int main() { int n, k; ll c; scanf("%d%d%lld", &n, &k, &c); for (int i = 0; i < k; i++) { int u, v, w, p; scanf("%d%d%d%d", &u, &v, &w, &p); u--, v--; es[i] = {w, p, u, v}; } sort(es, es + k); uft.init(n); int maxp = 0; ll wsum = 0; for (int i = 0; i < k; i++) { auto [w, p, u, v] = es[i]; if (! uft.same(u, v)) { used[i] = true; uft.merge(u, v); maxp = p; wsum += w; nbrs[u].push_back({v, i}); nbrs[v].push_back({u, i}); } } if (wsum > c) { puts("-1"); return 0; } //printf("maxp=%d, wsum=%lld\n", maxp, wsum); ps[0][0] = -1, pws[0][0] = 0, ds[0] = 0; for (int u = 0; u >= 0;) { auto &nbru = nbrs[u]; int up = ps[u][0]; if (cis[u] < nbru.size()) { auto [v, ei] = nbru[cis[u]++]; if (v != up) { ps[v][0] = u, pws[v][0] = get<1>(es[ei]), ds[v] = ds[u] + 1; u = v; } } else u = up; } for (int i = 0; i < BN - 1; i++) for (int u = 0; u < n; u++) { if (ps[u][i] >= 0) { ps[u][i + 1] = ps[ps[u][i]][i]; pws[u][i + 1] = max(pws[u][i], pws[ps[u][i]][i]); } else { ps[u][i + 1] = -1; pws[u][i + 1] = 0; } } for (int i = 0; i < k; i++) if (! used[i]) { auto [w, p, u, v] = es[i]; if (maxp < p) { int mw = find_maxw(u, v); if (wsum + w - mw <= c) maxp = p; } } printf("%d\n", maxp); return 0; }