# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
107125 |
2019-04-22T05:50:39 Z |
SOIVIEONE |
Cities (BOI16_cities) |
C++14 |
|
1169 ms |
65876 KB |
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
#define INF 1000000021
#define MOD 1000000007
#define pb push_back
#define sqr(a) (a)*(a)
#define M(a, b) make_pair(a,b)
#define T(a, b, c) make_pair(a, make_pair(b, c))
#define F first
#define S second
#define all(x) (x.begin(), x.end())
#define deb(x) cerr << #x << " = " << x << '\n'
#define N 1222222
using namespace std;
//using namespace __gnu_pbds;
typedef long double ld;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
const ld pi = 2 * acos(0.0);
template<class T> bool umin(T& a, T b){if(a>b){a=b;return 1;}return 0;}
template<class T> bool umax(T& a, T b){if(a<b){a=b;return 1;}return 0;}
template<class T, class TT> bool pal(T a, TT n){int k=0;for(int i=0;i<=n/2;i++){if(a[i]!=a[n-i-1]){k=1;break;}}return k?0:1;}
//int month[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
ll p[N], sz[N], c[N], o[N], used[N];
vll v[N];
void dfs(int u, int e)
{
used[u] = 1;
o[u] = e;
for(auto it : v[u])
{
if(it != e)
dfs(it, u);
}
}
ll find(ll x)
{
return (p[x] == x ? x : p[x] = find(p[x]));
}
int main()
{
int n, m, k;
cin >> n >> k >> m;
for(int i = 1; i <= k; i ++)
cin >> c[i];
vector<pair<ll, pll> > e;
map<pll, ll> mp;
for(int i = 1; i <= m; i ++)
{
ll x, y, w;
cin >> x >> y >> w;
if(!mp.count(M(x, y)))
mp[M(x, y)] = mp[M(y, x)] = w;
else
if(mp[M(x, y)] > w)
mp[M(x, y)] = mp[M(y, x)] = w;
e.pb(M(w, M(x, y)));
}
for(int i = 1; i <= n; i ++)
p[i] = i;
for(int i = 1; i <= k; i ++)
sz[c[i]] = 1;
sort all(e);
for(auto it : e)
{
ll x = it.S.F, y = it.S.S, w = it.F;
x = find(x);
y = find(y);
if(x != y)
{
sz[x] += sz[y];
p[y] = x;
v[it.S.F].pb(it.S.S);
v[it.S.S].pb(it.S.F);
// cout << it.S.F << ' ' << it.S.S << '\n';
if(sz[x] == k)
break;
}
}
dfs(c[1], 0);
set<pll> foo;
for(int i = 2; i <= k; i ++)
{
ll cur = c[i];
while(cur != c[1])
{
ll fo = cur, ho = o[cur];
if(ho > fo)
swap(fo,ho);
foo.insert(M(fo, ho));
cur = o[cur];
}
}
ll ans = 0;
for(auto it : foo)
// cout << it.F << ' ' << it.S << '\n',
ans += mp[it];
cout << ans;
return 0;
//ios::sync_with_stdio(false);
//cin.tie(0);
}
/*
6 3 8
1 4 6
1 5 1
1 2 7
5 6 20
5 2 10
2 6 1
2 3 2
2 4 5
3 4 1
*/
Compilation message
cities.cpp: In function 'int main()':
cities.cpp:77:30: warning: unused variable 'w' [-Wunused-variable]
ll x = it.S.F, y = it.S.S, w = it.F;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
29056 KB |
Output is correct |
2 |
Correct |
29 ms |
29056 KB |
Output is correct |
3 |
Incorrect |
29 ms |
29056 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1060 ms |
65300 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
29464 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1169 ms |
65876 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1038 ms |
64916 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |