#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/*
Phu Trong from Nguyen Tat Thanh High School for gifted student
*/
template <class X, class Y>
bool minimize(X &x, const Y &y){
X eps = 1e-9;
if (x > y + eps) {x = y; return 1;}
return 0;
}
template <class X, class Y>
bool maximize(X &x, const Y &y){
X eps = 1e-9;
if (x + eps < y) {x = y; return 1;}
return 0;
}
#define MAX_NODE 100005
#define MAX_EDGE 200005
int numNode, numCity, numEdge;
struct Edge{
int u, v, w;
Edge(){}
Edge(int _u, int _v, int _w): u(_u), v(_v), w(_w){}
int other(int x){return (x ^ u ^ v);}
} edge[MAX_EDGE];
vector<int> G[MAX_NODE];
bool good[MAX_NODE];
int dp[MAX_NODE][MASK(5)];
int id[MAX_NODE];
int cnt = 0;
void upd(int x){
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
for (int i = 1; i <= numNode; ++i){
q.emplace(dp[i][x], i);
}
while(q.size()){
int du, u; tie(du, u) = q.top(); q.pop();
if (du > dp[u][x]) continue;
for (int&i : G[u]){
int v = edge[i].other(u);
if(minimize(dp[v][x], dp[u][x] + edge[i].w)){
q.emplace(dp[v][x], v);
}
}
}
}
void process(void){
cin >> numNode >> numCity >> numEdge;
for (int i = 1; i <= numCity; ++i){
int u; cin >> u;
good[u] = true;
id[u] = cnt++;
}
for (int i = 1; i <= numEdge; ++i){
cin >> edge[i].u >> edge[i].v >> edge[i].w;
G[edge[i].u].emplace_back(i);
G[edge[i].v].emplace_back(i);
}
memset(dp, 0x3f, sizeof dp);
for (int i = 1; i <= numNode; ++i) {
if (good[i]){
dp[i][MASK(id[i])] = 0;
}
}
for (int msk = 0; msk < MASK(numCity); ++msk){
for (int sub_msk = msk; sub_msk > 0; sub_msk = (msk & (sub_msk - 1))){
for (int i = 1; i <= numNode; ++i){
minimize(dp[i][msk], dp[i][msk ^ sub_msk] + dp[i][sub_msk]);
}
}
upd(msk);
}
int ans = LINF;
for (int i = 1; i <= numNode; ++i) minimize(ans, dp[i][MASK(numCity) - 1]);
cout << ans;
}
signed main(){
#define name "Whisper"
cin.tie(nullptr) -> sync_with_stdio(false);
//freopen(name".inp", "r", stdin);
//freopen(name".out", "w", stdout);
process();
return (0 ^ 0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
27740 KB |
Output is correct |
2 |
Correct |
11 ms |
27744 KB |
Output is correct |
3 |
Correct |
10 ms |
27736 KB |
Output is correct |
4 |
Correct |
12 ms |
27700 KB |
Output is correct |
5 |
Correct |
13 ms |
27740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
531 ms |
45296 KB |
Output is correct |
2 |
Correct |
532 ms |
45300 KB |
Output is correct |
3 |
Correct |
342 ms |
36964 KB |
Output is correct |
4 |
Correct |
60 ms |
37248 KB |
Output is correct |
5 |
Correct |
296 ms |
45208 KB |
Output is correct |
6 |
Correct |
53 ms |
37204 KB |
Output is correct |
7 |
Correct |
14 ms |
27992 KB |
Output is correct |
8 |
Correct |
12 ms |
27956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
27996 KB |
Output is correct |
2 |
Correct |
14 ms |
28020 KB |
Output is correct |
3 |
Correct |
13 ms |
27740 KB |
Output is correct |
4 |
Correct |
13 ms |
27828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1069 ms |
45292 KB |
Output is correct |
2 |
Correct |
1053 ms |
45284 KB |
Output is correct |
3 |
Correct |
705 ms |
37052 KB |
Output is correct |
4 |
Correct |
536 ms |
41932 KB |
Output is correct |
5 |
Correct |
150 ms |
38908 KB |
Output is correct |
6 |
Correct |
71 ms |
38928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2181 ms |
45284 KB |
Output is correct |
2 |
Correct |
2234 ms |
45296 KB |
Output is correct |
3 |
Correct |
2122 ms |
45320 KB |
Output is correct |
4 |
Correct |
1466 ms |
36996 KB |
Output is correct |
5 |
Correct |
963 ms |
41904 KB |
Output is correct |
6 |
Correct |
248 ms |
38876 KB |
Output is correct |
7 |
Correct |
95 ms |
38900 KB |
Output is correct |