Submission #1245781

#TimeUsernameProblemLanguageResultExecution timeMemory
1245781vuxxcodeCities (BOI16_cities)C++20
14 / 100
462 ms63228 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define eb emplace_back #define pb push_back #define fi first #define se second #define ii pair<int,int> #define pl pair<ll,ll> #define ve vector #define vi ve<int> #define vvi ve<vi> #define vl ve<ll> #define vii ve<ii> #define all(x) x.begin(), x.end() #define fo(i,a,b) for (int i=(a),_b=(b); i<=_b; ++i) #define fd(i,a,b) for (int i=(a),_b=(b); i>=_b; --i) #define lc p+1 #define rc p+(mid-l+1)*2 #define maxi(a, b) a = max(a, b) #define mini(a, b) a = min(a, b) #define bit(s, i) (((s) >> (i)) & 1) #define tup3 array<int,3> #define tup4 array<int,4> #define matrix vvi #define cntBit(x) (__builtin_popcountll(x)) #define SZ(x) int(x.size()) #define x1 _ #define x2 __ #define y1 _ #define y2 __ const int N = 1e5+5, M = 1e6+5; const int inf = 1e9+10; const int mod = 1e9+7; const int LG = 20, base=11111, B = 500; bool st_mem; int n, k, m; ve<ii> g[N]; vl d[5]; ll cost[N][1<<6]; vl dij(int s) { vl d(n+1, 1e16); struct node { int u; ll d; bool operator < (const node &o) const { return d > o.d; } }; priority_queue<node> q; d[s] = 0; q.push({s,0}); while (q.size()) { node t = q.top(); q.pop(); int u = t.u; ll dd = t.d; if (dd ^ d[u]) continue; for (auto &x:g[u]) { int v=x.fi, c=x.se; if (d[v]>dd+c) { d[v]=dd+c; q.push({v,dd+c}); } } } return d; } void sol() { cin >> n >> k >> m; vi v(k); fo(i,0,k-1) cin >> v[i]; fo(i,1,m) { int u, v, c; cin >> u >> v >> c; g[u].eb(v, c), g[v].eb(u, c); } fo(i,0,k-1) d[i]=dij(v[i]); if (k == 1) { cout << 0; return; } if (k == 2) { cout << d[0][v[1]]; return; } ll ans=1e18; fo(i,1,n) { fo(j,1,(1<<k)-1) fo(l,0,k-1) if (j>>l&1) cost[i][j]+=d[l][i]; mini(ans, cost[i][(1<<k)-1]); } fo(i,1,n) { fo(mask,1,(1<<k)-1) if (k-cntBit(mask)<=2) { vi s, t, tc = {i}, ot; ll sum=0; fo(j,0,k-1) { if (mask>>j&1) s.eb(j), tc.eb(v[j]), sum+=d[j][i]; else t.eb(j), ot.eb(v[j]); } ll add=1e18; if (ot.empty()) add=0; for (int x:tc) if (t.size()) { ll cur=1e18; fo(_,1,2) { ll tmp = d[t[0]][x]; if (ot.size()>1) tmp+=d[t[0]][ot[1]]; mini(cur, tmp); if (ot.size()>1) { swap(ot[0],ot[1]); swap(t[0],t[1]); } } mini(add,cur); } sum += add; mini(ans, sum); } } cout << ans; } bool en_mem; signed main() { ios::sync_with_stdio(0); cin.tie(0); if(fopen("A.inp","r")) { freopen("A.inp","r",stdin); freopen("A.out","w",stdout); } int tc = 1; // cin >> tc; fo(i,1,tc) sol(); // cerr << abs(&en_mem - &st_mem) / 1024.0 / 1024.0; return 0; }

Compilation message (stderr)

cities.cpp: In function 'int main()':
cities.cpp:127:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  127 |         freopen("A.inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~
cities.cpp:128:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  128 |         freopen("A.out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~
#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...