Submission #640643

# Submission time Handle Problem Language Result Execution time Memory
640643 2022-09-15T04:21:03 Z makanhulia Cities (BOI16_cities) C++17
22 / 100
153 ms 20584 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define db double
#define pairll pair<ll,ll>
#define lpairll pair<ll,pairll>

#define repp(i,a,b) for (ll i = a; i <= b; i++)
#define repz(i,a,b) for (ll i = a; i < b; i++)
#define repm(i,a,b) for (ll i = a; i >= b; i--)
#define fr first
#define sc second
#define mp make_pair
#define pb push_back

const ll N = 1e5+5, MOD = 1e9+7;
ll tc = 1, n, m, ar[N], br[N], used[N];
ll x, y, k;
string s, s1, s2, ye = "YES", no = "NO";
vector<pairll> v, ed[N];
vector<lpairll> V;

void input(){
  cin >> n >> k >>  m;
  repp(i,1,k) cin >> ar[i], used[ar[i]] = 1;
  repp(i,1,m){
    ll x, y, w;
    cin >> x >> y >> w;
    ed[x].pb(mp(y,w));
    ed[y].pb(mp(x,w));
    V.pb(mp(w,mp(x,y)));
  }
}

ll pr[N];

ll fir(ll i){
  if (i == pr[i]) return i;
  return pr[i] = fir(pr[i]);
}

bool merge(ll a, ll b){
  a = fir(a), b = fir(b);
  if (a == b) return 0;
  pr[a] = b;
  return 1;
}

void sub1(){
  //create an mst with the nodes that are active
  vector<ll> act;
  repp(i,1,n){
    if (!used[i]) act.pb(i);
  }
  ll sz = act.size(), ans = 1e18;
  repp(i,0,(1<<sz)){
    repz(j,0,sz){
      if((1<<j)&i) used[act[j]] = 1;
      else used[act[j]] = 0;
    }
    // cout << "i = " << i << endl;
    // repp(j,1,n){
    //   cout << j << " " << used[j] << endl;
    // }
    ll cur = 0;
    repp(j,1,n) pr[j] = j;
    for (auto j : V){
      ll w = j.fr, x = j.sc.fr, y = j.sc.sc;
      if (!used[x] || !used[y]) continue;
      if(merge(x,y)) cur += w;
    }
    // repp(j,1,n){
    //   cout << j << " " << fir(j) << endl;
    // }
    bool imp = 0;
    repp(j,1,k){
      imp |= (fir(ar[j]) != fir(ar[1]));
    }
    if (imp) continue;
    ans = min(ans,cur);
  }
  cout << ans << endl;
}

void subk2(){
  ll dis[n+5];
  repp(i,1,n) dis[i] = 1e18;
  dis[ar[1]] = 0;
  priority_queue<pairll,vector<pairll>,greater<pairll> > pq;
  pq.push(mp(0,ar[1]));
  while(pq.size()){
    pairll a = pq.top();
    pq.pop();
    for (auto i : ed[a.sc]){
      ll nxidx = i.fr, w = i.sc;
      if (dis[nxidx] > dis[a.sc]+w){
        dis[nxidx] = dis[a.sc]+w;
        pq.push(mp(dis[nxidx],nxidx));
      }
    }
  }
  cout << dis[ar[2]] << endl;
}

void solve(){
  if (n <= 20 && m <= 40){
    sort(V.begin(),V.end());
    sub1();
  }
  else if (k == 2){
    subk2();
  }
}

int main(){
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  //cin >> tc;
  while(tc--){
    input();
    solve();
  }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 80 ms 2684 KB Output is correct
3 Correct 46 ms 2696 KB Output is correct
4 Correct 25 ms 2644 KB Output is correct
5 Correct 15 ms 2692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 129 ms 20572 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 2772 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 123 ms 20584 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 153 ms 20544 KB Output isn't correct
2 Halted 0 ms 0 KB -