#include<bits/stdc++.h> usingnamespace std; #define ll long long typedef pair<int, int> pii; constint INF = 0x3f3f3f3f; constint maxn = 1e5 + 10; int n, m; ll sum; structE { int v, w; }; vector<E>G[maxn]; priority_queue<int, vector<int>, greater<int> >que; vector<pii>vc; intmain(){ scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); sum += w; G[u].push_back(E{ v,w }); } ll tmp = 0; int l = 0; int u = 1; while (u != n) { u = G[u][0].v; l++; } if (l == 1) { cout << 0 << endl; return0; } sum /= l; for (E e : G[1]) { int u = e.v; que.push(e.w); while (u != n) { que.push(G[u][0].w); u = G[u][0].v; } int tp = que.top(), cnt = 1; tmp += tp; que.pop(); while (!que.empty()) { vc.push_back(pii(cnt++, que.top() - tp)); tp = que.top(); que.pop(); } } sum -= tmp; ll ans = 0; sort(vc.begin(), vc.end()); int i = 0; while (sum >= vc[i].second) { ans += 1ll * vc[i].first*vc[i].second; sum -= vc[i].second; i++; } ans += 1ll * vc[i].first*sum; cout << ans << endl; return0; }
M. Value
题意:从 1 到 n 中选一些不相等的数,value 等于选中的数的 a 值之和,若存在 i>1, j>1,且 ik=j ,则 value 要减去 b[j]。求最大的 value。
由数据范围可知,有 ik=j 关系的数组成的集合大小不超过18.
则每个集合内部可以状压,暴力求出最大 value。而不同集合之间没有影响,所以可以直接把各个集合的 value 相加。