# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
467213 |
2021-08-22T03:51:21 Z |
Killer2501 |
Cities (BOI16_cities) |
C++14 |
|
3224 ms |
76428 KB |
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define task "262144"
#define pll pair<ll, ll>
#define pi pair<ll, pll>
#define fi first
#define se second
using namespace std;
const ll mod = 1e15;
const ll N = 2e5+5;
const int base = 313;
ll n, m, t, k, T, ans, tong, a[N], dp[N][(1<<6)];
vector<pll> adj[N];
vector<ll> kq;
ll pw(ll k, ll n)
{
ll total = 1;
for(; n; n >>= 1)
{
if(n & 1)total = total * k % mod;
k = k * k % mod;
}
return total;
}
void sol()
{
cin >> n >> k >> m;
for(int i = 1; i <= k; i ++)
{
ll x;
cin >> x;
--x;
kq.pb(x);
}
while(m -- > 0)
{
ll x, y, w;
cin >> x >> y >> w;
--x;
--y;
adj[x].pb({w, y});
adj[y].pb({w, x});
}
for(int i = 0; i < n; i ++)for(int j = 1; j < (1<<k); j ++)dp[i][j] = mod;
for(int i = 0; i < k; i ++)dp[kq[i]][(1<<i)] = 0;
for(int mask = 0; mask < (1<<k); mask ++)
{
priority_queue< pll, vector<pll>, greater<pll> > pq;
for(int i = 0; i < n; i ++)
{
for(int j = mask; j > 0; j = (j - 1) & mask)
{
dp[i][mask] = min(dp[i][mask], dp[i][j] + dp[i][mask^j]);
}
if(dp[i][mask] < mod)pq.push({dp[i][mask], i});
}
while(!pq.empty())
{
pll u = pq.top();
pq.pop();
if(u.fi != dp[u.se][mask])continue;
//cout << u.se <<" "<<mask <<" "<<u.fi << '\n';
if(mask == (1<<k)-1)
{
cout << u.fi;
return;
}
for(pll v : adj[u.se])
{
if(dp[v.se][mask] > u.fi + v.fi)
{
dp[v.se][mask] = u.fi + v.fi;
pq.push({dp[v.se][mask], v.se});
}
}
}
}
}
int main()
{
if(fopen(task".in", "r"))
{
freopen(task".in", "r", stdin);
freopen(task".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int ntest = 1;
//cin >> ntest;
while(ntest -- > 0)
sol();
}
/*
4 5 2
10 9 5 2
6 4 20 15
9 7 10 9
-1 -1 16 11
1 2 3
2 3 3
1 4 1
4 3 1
3 1 1
*/
Compilation message
cities.cpp: In function 'int main()':
cities.cpp:85:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | freopen(task".in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:86:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
697 ms |
71844 KB |
Output is correct |
2 |
Correct |
702 ms |
75644 KB |
Output is correct |
3 |
Correct |
424 ms |
66144 KB |
Output is correct |
4 |
Correct |
83 ms |
17988 KB |
Output is correct |
5 |
Correct |
331 ms |
73328 KB |
Output is correct |
6 |
Correct |
80 ms |
17976 KB |
Output is correct |
7 |
Correct |
6 ms |
5708 KB |
Output is correct |
8 |
Correct |
5 ms |
5584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5580 KB |
Output is correct |
2 |
Correct |
8 ms |
5708 KB |
Output is correct |
3 |
Correct |
6 ms |
5580 KB |
Output is correct |
4 |
Correct |
6 ms |
5416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1573 ms |
72016 KB |
Output is correct |
2 |
Correct |
1541 ms |
75756 KB |
Output is correct |
3 |
Correct |
968 ms |
66160 KB |
Output is correct |
4 |
Correct |
854 ms |
48424 KB |
Output is correct |
5 |
Correct |
203 ms |
23844 KB |
Output is correct |
6 |
Correct |
91 ms |
20292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3128 ms |
71976 KB |
Output is correct |
2 |
Correct |
3224 ms |
76428 KB |
Output is correct |
3 |
Correct |
3192 ms |
75684 KB |
Output is correct |
4 |
Correct |
2078 ms |
66112 KB |
Output is correct |
5 |
Correct |
1733 ms |
48376 KB |
Output is correct |
6 |
Correct |
346 ms |
23792 KB |
Output is correct |
7 |
Correct |
105 ms |
19956 KB |
Output is correct |