#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ALL(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;}
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll LASTBIT(ll mask){return mask & (-mask);}
ll pop_cnt(ll mask){return __builtin_popcountll(mask);}
ll ctz(ll mask){return __builtin_ctzll(mask);}
ll clz(ll mask){return __builtin_clzll(mask);}
ll logOf(ll mask){return 63 - clz(mask);}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b){a = b; return true;}
return false;
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b){a = b; return true;}
return false;
}
template <class T>
void printArr(T a, string separator = " ", string finish = "\n", ostream& out = cout){
for(auto i: a) out << i << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
#define int long long
struct DSU{
int n;
vector<int> parent, sz;
DSU(int _n){
n = _n;
parent.resize(n+1); sz.resize(n+1, 1);
for(int i = 1; i<=n; ++i) parent[i] = i;
}
int find_set(int u){return (u == parent[u]) ? u : (parent[u] = find_set(parent[u]));}
bool same_set(int u, int v){return find_set(u) == find_set(v);}
bool join_set(int u, int v){
u = find_set(u), v = find_set(v);
if (u != v){
if (sz[u] < sz[v]) swap(u, v);
parent[v] = u;
sz[u] += sz[v];
return true;
}
return false;
}
};
const int N = 1e5 + 69, LOG_N = 10;
int n, m, k;
vector<array<int, 3>> path, useful;
vector<pair<int, int>> money;
vector<pair<int, int>> graph[N];
ll goon[N];
namespace Nig{
vector<pair<int, int>> graph[N];
ll chode[N];
vector<array<int, 3>> ans;
void reset(){
for(int i = 1; i<=n; ++i){
graph[i].clear();
}
}
void dfs(int u, int p){
chode[u] = goon[u];
for(pair<int, int> v: graph[u]) if (v.first != p){
dfs(v.first, u);
chode[u] += chode[v.first];
if (v.second) ans.push_back({u, v.first, chode[v.first]});
}
}
vector<array<int, 3>> go(int root, DSU &mst){
ans.clear();
for(array<int, 3> i: useful){
int u = i[0], v = i[1];
if (mst.join_set(u, v)){
graph[u].push_back({v, 0});
graph[v].push_back({u, 0});
}
}
dfs(root, root);
return ans;
}
}
signed main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
cin >> n >> m >> k;
path.resize(m);
for(int i = 0; i<m; ++i){
for(int j = 0;j < 3; ++j) cin >> path[i][j];
}
sort(ALL(path), [] (array<int, 3> x, array<int, 3> y){return x[2] < y[2];});
DSU mst(n);
for(int i = 0; i<m; ++i){
int u = path[i][0], v = path[i][1], w = path[i][2];
if (mst.join_set(u, v)){
useful.push_back(path[i]);
graph[u].push_back({v, w});
graph[v].push_back({u, w});
}
}
money.resize(k);
for(int i = 0; i<k; ++i) cin >> money[i].first >> money[i].second;
for(int i = 1; i<=n; ++i) cin >> goon[i];
mst = DSU(n);
vector<array<int, 3>> annoying, chill;
for(pair<int, int> i: money) mst.join_set(i.first, i.second);
for(array<int, 3> i: useful){
if (mst.join_set(i[0], i[1])) {
chill.push_back(i);
}
else annoying.push_back(i);
}
mst = DSU(n);
for(array<int, 3> i: chill) {
mst.join_set(i[0], i[1]);
}
vector<int> S;
S.push_back(0);
for(int i= 1; i<=n; ++i) if (i == mst.find_set(i)) S.push_back(i);
for(pair<int, int> &i: money){
i.first = lower_bound(ALL(S), mst.find_set(i.first)) - S.begin();
i.second = lower_bound(ALL(S), mst.find_set(i.second)) - S.begin();
}
for(array<int, 3> &i: annoying){
i[0] = lower_bound(ALL(S), mst.find_set(i[0])) - S.begin();
i[1] = lower_bound(ALL(S), mst.find_set(i[1])) - S.begin();
}
useful = annoying;
vector<ll> sum(S.size());
for(int i= 1; i<=n; ++i){
int idx = lower_bound(ALL(S), mst.find_set(i)) - S.begin();
sum[idx] += goon[i];
}
memset(goon, 0, sizeof goon);
for(int i= 1; i<S.size(); ++i) goon[i] = sum[i];
n = S.size() - 1;
// cout << n << "\n";
// for(int i = 1; i<=n; ++i) cout << goon[i] << " "; cout << "\n\n";
// for(pair<int, int> i: money) cout << i.first << " " << i.second << "\n";
// for(array<int, 3> i: useful) printArr(i);
int root = lower_bound(ALL(S), mst.find_set(1)) - S.begin();
ll ans = 0;
for(int mask = 1; mask < MASK(k); ++mask){
DSU mst(n);
bool check = true;
for(int i = 0; i<k; ++i) if (GETBIT(mask, i)){
check = check && mst.join_set(money[i].first, money[i].second);
}
if (check == false) continue;
Nig::reset();
// cerr << string(50, '-') << endl;
for(int i= 0; i<k; ++i) if (GETBIT(mask, i)) {
int u = money[i].first, v = money[i].second;
Nig::graph[u].push_back({v, 1});
Nig::graph[v].push_back({u, 1});
}
vector<array<int, 3>> edging = Nig::go(root, mst);
sort(ALL(edging), [](array<int, 3> x, array<int, 3> y){return x[2] > y[2];});
vector<int> toll(edging.size(), -1);
vector<array<int, 3>> sex = useful;
for(int i = 0; i < edging.size(); ++i){
DSU mst(n);
for(int j = i+1; j<edging.size(); ++j){
int u = edging[j][0], v = edging[j][1];
mst.join_set(u, v);
}
int u = edging[i][0], v = edging[i][1];
for(array<int, 3> item: sex){
if (mst.join_set(abs(item[0]), abs(item[1]))){
if (mst.same_set(u, v) && toll[i] == -1){
toll[i] = item[2];
break;
}
}
}
int val = toll[i];
for(int j = 0; j <= sex.size(); ++j){
if (j == sex.size() || sex[j][2] >= val){
sex.insert(sex.begin() + j, {{-u, -v, val}});
break;
}
}
}
ll cur = 0;
for(int i = 0; i<edging.size(); ++i) cur += 1LL * toll[i] * edging[i][2];
// for(int i = 0; i<edging.size(); ++i) cerr << toll[i] << " " << edging[i][2] << "\n";
// cerr << cur << endl;
maximize(ans, cur);
}
cout << ans << "\n";
return 0;
}
Compilation message
toll.cpp: In function 'int main()':
toll.cpp:172:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
172 | for(int i= 1; i<S.size(); ++i) goon[i] = sum[i];
| ~^~~~~~~~~
toll.cpp:207:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
207 | for(int i = 0; i < edging.size(); ++i){
| ~~^~~~~~~~~~~~~~~
toll.cpp:210:31: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
210 | for(int j = i+1; j<edging.size(); ++j){
| ~^~~~~~~~~~~~~~
toll.cpp:225:30: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
225 | for(int j = 0; j <= sex.size(); ++j){
| ~~^~~~~~~~~~~~~
toll.cpp:226:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
226 | if (j == sex.size() || sex[j][2] >= val){
| ~~^~~~~~~~~~~~~
toll.cpp:234:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
234 | for(int i = 0; i<edging.size(); ++i) cur += 1LL * toll[i] * edging[i][2];
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
4 ms |
6492 KB |
Output is correct |
4 |
Correct |
3 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
4 ms |
6492 KB |
Output is correct |
4 |
Correct |
3 ms |
6492 KB |
Output is correct |
5 |
Correct |
7 ms |
6748 KB |
Output is correct |
6 |
Correct |
4 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
4 ms |
6492 KB |
Output is correct |
4 |
Correct |
3 ms |
6492 KB |
Output is correct |
5 |
Correct |
7 ms |
6748 KB |
Output is correct |
6 |
Correct |
4 ms |
6748 KB |
Output is correct |
7 |
Correct |
188 ms |
27080 KB |
Output is correct |
8 |
Correct |
214 ms |
28352 KB |
Output is correct |
9 |
Correct |
199 ms |
28384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6492 KB |
Output is correct |
3 |
Correct |
4 ms |
6492 KB |
Output is correct |
4 |
Correct |
3 ms |
6492 KB |
Output is correct |
5 |
Correct |
7 ms |
6748 KB |
Output is correct |
6 |
Correct |
4 ms |
6748 KB |
Output is correct |
7 |
Correct |
188 ms |
27080 KB |
Output is correct |
8 |
Correct |
214 ms |
28352 KB |
Output is correct |
9 |
Correct |
199 ms |
28384 KB |
Output is correct |
10 |
Execution timed out |
2531 ms |
28376 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |