#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
#define rall(s) s.rbegin(),s.rend()
struct dsu{
vector<ll>p,sz;
dsu(ll n){
p.resize(n+1);
iota(p.begin(),p.end(),0);
sz.resize(n+1);
fill(sz.begin(),sz.end(),1);
}
ll find(ll x){
if(p[x]==x){
return x;
}
return p[x]=find(p[x]);
}
bool same(ll x,ll y){
x=find(x);
y=find(y);
if(x==y){
return true;
}
else{
return false;
}
}
void merge(ll x,ll y){
x=find(x);
y=find(y);
if(x!=y){
if(sz[x]<sz[y]){
swap(x,y);
}
sz[x]+=sz[y];
p[y]=x;
}
}
};
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll n,k,m;
cin>>n>>k>>m;
ll dist[n+5],p[k+5];
for(ll i=1;i<=k;i++){
cin>>p[i];
p[i]--;
}
vector<pair<ll,ll>>adj[n+5];
vector<array<ll,3>>edge;
ll D[n+5][n+5];
for(ll i=0;i<n;i++){
for(ll j=0;j<m;j++){
D[i][j]=1e18;
}
D[i][i]=0;
}
for(ll i=1;i<=m;i++){
ll a,b,c;
cin>>a>>b>>c;
a--,b--;
edge.pb({c,a,b});
adj[a].pb({b,c});
adj[b].pb({a,c});
D[a][b]=c;
D[b][a]=c;
}
for(ll r=0;r<n;r++){
for(ll i=0;i<n;i++){
for(ll j=0;j<n;j++){
if(D[i][r]<1e18&&D[r][j]<1e18){
D[i][j]=min(D[i][j],D[i][r]+D[r][j]);
}
}
}
}
sort(all(edge));
ll ans=1e18;
for(ll i=0;i<n;i++){
ll cur=0;
for(ll j=1;j<=k;j++){
cur+=D[i][p[j]];
}
ans=min(ans,cur);
}
if(k==4){
for(ll i=0;i<n;i++){
for(ll j=0;j<n;j++){
ll cur=min({
D[p[1]][i]+D[p[2]][i]+D[p[3]][j]+D[p[4]][j],
D[p[1]][i]+D[p[2]][j]+D[p[3]][i]+D[p[4]][j],
D[p[1]][j]+D[p[2]][i]+D[p[3]][i]+D[p[4]][j],
D[p[1]][i]+D[p[2]][j]+D[p[3]][j]+D[p[4]][i],
D[p[1]][j]+D[p[2]][i]+D[p[3]][j]+D[p[4]][i],
D[p[1]][j]+D[p[2]][j]+D[p[3]][i]+D[p[4]][i],
});
ans=min(ans,cur+D[i][j]);
}
}
}
cout<<ans;
}
Compilation message
cities.cpp: In function 'int main()':
cities.cpp:53:8: warning: unused variable 'dist' [-Wunused-variable]
53 | ll dist[n+5],p[k+5];
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
125 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1248 ms |
8540 KB |
Output is correct |
2 |
Correct |
1221 ms |
8516 KB |
Output is correct |
3 |
Correct |
956 ms |
8536 KB |
Output is correct |
4 |
Correct |
169 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
125 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
122 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |