#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
const ll mod=1e9+7;
const ll maxn=1e5+5;
const ll INF=1e18;
#define ok ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define fi first
#define se second
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define endl '\n'
int n, k, m, arr[5], u, v, w, bit, par[21];
ll dis[maxn][5], ans=INF;
vector<pii> adj[maxn];
vector<pair<int, pii>> edge;
int find(int a) {
return par[a]=(par[a]==a? a : find(par[a]));
}
int main() {
ok
cin >> n >> k >> m;
if(k==5 && n<=20) {
for(int i=0; i<k; i++) {
cin >> arr[i];
bit|=arr[i];
}
for(int i=0; i<m; i++) {
cin >> u >> v >> w;
edge.pb({w, {u, v}});
}
sort(edge.begin(), edge.end());
for(int i=1; i<(1<<(n+1)); i++) {
if((i&bit)!=bit) continue;
for(int j=1; j<=n; j++) par[j]=j;
ll cur=0;
for(auto it : edge) {
if(find(it.se.fi)!=find(it.se.se) && (i&(1<<it.se.fi)) && (i&(1<<it.se.se))) {
cur+=it.fi;
par[find(it.se.fi)]=find(it.se.se);
}
}
bool bisa=1;
for(int j=1; j<k; j++) {
if(find(arr[j])!=find(arr[j-1])) {
bisa=0;
break;
}
}
if(!bisa) continue;
ans=min(ans, cur);
}
cout << ans;
return 0;
}
for(int i=0; i<k; i++) cin >> arr[i];
for(int i=0; i<m; i++) {
cin >> u >> v >> w;
adj[u].pb({v, w});
adj[v].pb({u, w});
}
if(k==4 && n<=1000) {
ll dist[n+1][n+1];
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) dist[i][j]=INF;
}
for(int i=1; i<=n; i++) {
dist[i][i]=0;
priority_queue<pll, vector<pll>, greater<pll>> pq;
pq.push({0, i});
while(!pq.empty()) {
pll fr=pq.top();
pq.pop();
if(fr.fi>dist[fr.se][i]) continue;
for(auto it : adj[fr.se]) {
if(dist[it.fi][i]>dist[fr.se][i]+it.se) {
dist[it.fi][i]=dist[fr.se][i]+it.se;
pq.push({dist[it.fi][i], it.fi});
}
}
}
}
for(int i=1; i<=n; i++) {
for(int j=1; j<=n; j++) {
for(int l=1; l<16; l++) {
ll cur=dist[i][j];
for(int m=0; m<4; m++) {
if(l&(1<<m)) {
cur+=dist[arr[m]][i];
} else cur+=dist[arr[m]][j];
}
ans=min(ans, cur);
}
}
}
cout << ans;
return 0;
}
for(int i=0; i<k; i++) {
for(int j=1; j<=n; j++) dis[j][i]=INF;
dis[arr[i]][i]=0;
priority_queue<pll, vector<pll>, greater<pll>> pq;
pq.push({0, arr[i]});
while(!pq.empty()) {
pll fr=pq.top();
pq.pop();
if(fr.fi>dis[fr.se][i]) continue;
for(auto it : adj[fr.se]) {
if(dis[it.fi][i]>dis[fr.se][i]+it.se) {
dis[it.fi][i]=dis[fr.se][i]+it.se;
pq.push({dis[it.fi][i], it.fi});
}
}
}
}
for(int i=1; i<=n; i++) {
ll cur=0;
for(int j=0; j<k; j++) cur+=dis[i][j];
ans=min(ans, cur);
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Incorrect |
35 ms |
2644 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
228 ms |
14640 KB |
Output is correct |
2 |
Correct |
230 ms |
15660 KB |
Output is correct |
3 |
Correct |
81 ms |
9688 KB |
Output is correct |
4 |
Correct |
51 ms |
7388 KB |
Output is correct |
5 |
Correct |
169 ms |
14660 KB |
Output is correct |
6 |
Correct |
52 ms |
7420 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
226 ms |
10596 KB |
Output is correct |
2 |
Correct |
229 ms |
10580 KB |
Output is correct |
3 |
Correct |
132 ms |
10532 KB |
Output is correct |
4 |
Correct |
71 ms |
4692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
264 ms |
14704 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
335 ms |
14720 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |