# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
978033 |
2024-05-08T17:11:14 Z |
steveonalex |
Toll (APIO13_toll) |
C++17 |
|
2500 ms |
604 KB |
#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());
}
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 = 1e3 + 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];
// int parent[LOG_N][N];
// int max_path[LOG_N][N];
// int h[N];
int goon[N];
// void dfs(int u, int p){
// h[u] = h[p] + 1;
// for(int j = 1; MASK(j) < h[u]; ++j){
// int prev = parent[j-1][u];
// parent[j][u] = parent[j-1][prev];
// max_path[j][u] = max(max_path[j-1][u], max_path[j-1][prev]);
// }
// for(pair<int, int> v: graph[u]) if (v.first != p){
// parent[0][v.first] = u;
// max_path[0][v.first] = v.second;
// dfs(v.first, u);
// }
// }
// int LCA(int u, int v){
// if (h[u] < h[v]) swap(u, v);
// int diff = h[u] - h[v];
// for(int j = 0; j < LOG_N; ++j) if (GETBIT(diff, j))
// u = parent[j][u];
// if (u == v) return u;
// for(int j = LOG_N - 1; j>=0; --j) if (parent[j][u] != parent[j][v]){
// u = parent[j][u];
// v = parent[j][u];
// }
// return parent[0][u];
// }
// int get_max_path(int u, int v){
// int lck = LCA(u, v);
// int diff = h[u] - h[lck];
// int ans = 0;
// for(int j = 0; j< LOG_N; ++j) if (GETBIT(diff, j)) {
// maximize(ans, max_path[j][u]);
// u = parent[j][u];
// }
// diff = h[v] - h[lck];
// for(int j = 0; j<LOG_N; ++j) if (GETBIT(diff, j)){
// maximize(ans, max_path[j][v]);
// v = parent[j][v];
// }
// return ans;
// }
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(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(1, 1);
return ans;
}
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
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];
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(mst);
sort(ALL(edging), [](array<int, 3> x, array<int, 3> y){return x[2] > y[2];});
vector<int> toll(edging.size());
for(int i = 0; i < edging.size(); ++i){
int l = 0, r = 1e9;
while(l < r){
int mid = (l + r + 1) >> 1;
toll[i] = mid;
vector<array<int, 3>> sex = useful;
for(int j = 0; j< edging.size(); ++j){
int u = edging[j][0], v = edging[j][1], w = toll[j];
sex.push_back({{-u, -v, w}});
}
sort(ALL(sex), [](array<int, 3> x, array<int, 3> y){
if (x[2] != y[2]) return x[2] < y[2];
return x[1] < y[1];
});
int cnt = 0;
DSU mst(n);
for(array<int, 3> i: sex){
if (mst.join_set(abs(i[0]), abs(i[1]))){
if (i[1] < 0) cnt++;
}
}
if (cnt != toll.size()) r = mid - 1;
else l = mid;
}
toll[i] = l;
}
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 'void Nig::dfs(int, int)':
toll.cpp:140:67: warning: narrowing conversion of 'Nig::chode[v.std::pair<int, int>::first]' from 'll' {aka 'long long int'} to 'int' [-Wnarrowing]
140 | if (v.second) ans.push_back({u, v.first, chode[v.first]});
| ~~~~~~~~~~~~~^
toll.cpp: In function 'int main()':
toll.cpp:205:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
205 | for(int i = 0; i < edging.size(); ++i){
| ~~^~~~~~~~~~~~~~~
toll.cpp:212:33: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
212 | for(int j = 0; j< edging.size(); ++j){
| ~^~~~~~~~~~~~~~~
toll.cpp:229:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
229 | if (cnt != toll.size()) r = mid - 1;
| ~~~~^~~~~~~~~~~~~~
toll.cpp:236:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
236 | for(int i = 0; i<edging.size(); ++i) cur += 1LL * toll[i] * edging[i][2];
| ~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
99 ms |
516 KB |
Output is correct |
4 |
Correct |
88 ms |
500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
99 ms |
516 KB |
Output is correct |
4 |
Correct |
88 ms |
500 KB |
Output is correct |
5 |
Execution timed out |
2534 ms |
604 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
99 ms |
516 KB |
Output is correct |
4 |
Correct |
88 ms |
500 KB |
Output is correct |
5 |
Execution timed out |
2534 ms |
604 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
99 ms |
516 KB |
Output is correct |
4 |
Correct |
88 ms |
500 KB |
Output is correct |
5 |
Execution timed out |
2534 ms |
604 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |