Submission #926423

#TimeUsernameProblemLanguageResultExecution timeMemory
926423irmuunCities (BOI16_cities)C++17
15 / 100
1248 ms262144 KiB
#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 (stderr)

cities.cpp: In function 'int main()':
cities.cpp:53:8: warning: unused variable 'dist' [-Wunused-variable]
   53 |     ll dist[n+5],p[k+5];
      |        ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...