Submission #1314643

#TimeUsernameProblemLanguageResultExecution timeMemory
1314643joshjuiceVoting Cities (NOI22_votingcity)C++20
100 / 100
59 ms8524 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define rep(x, a, b) for(auto x=a;(a<b?x<b:x>b);(a<b?x++:x--))
#define all(a) a.begin(), a.end()
#define srtvc(a) sort(all(a))
#define bcsrtvc(a) sort(all(a), greater<__typeof__(a[0])>())
#define ppf pop_front
#define ppb pop_back
#define pf push_front
#define pb push_back
#define ef emplace_front
#define eb emplace_back
#define fr first
#define sc second
#define pq priority_queue
#define pii pair<int, int>
#define vc vector
#define dq deque
#define sz(a) a.size()
#define mnto(x,y) x = min(x, (__typeof__(x))y)
#define mxto(x,y) x = max(x, (__typeof__(x))y)
#define setval(arr, x) memset(arr, x, sizeof(arr))

template<typename T>
using vv = vc<vc<T>>;

const int INF = 1e18;

struct Edge {
  int to, cost;
};

struct State {
  int node;
  int mask;
  int dist;
  bool operator>(const State &other) const {
    return dist > other.dist;
  }
};

int32_t main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  int n, e, k;
  cin >> n >> e >> k;
  vc<int> voting(k);
  rep(i, 0, k) cin >> voting[i];
  vv<Edge> adj(n);
  rep(i, 0, e) {
    int u, v, c;
    cin >> u >> v >> c;
    adj[v].pb({u, c});
  }
  const int MAX_MASK = 1 << 5;
  vv<int> dist(n, vc<int> (MAX_MASK, INF));
  pq<State, vc<State>, greater<State>> heap;
  for (int city : voting) {
    rep(mask, 0, MAX_MASK) {
      dist[city][mask] = 0;
      heap.push({city, mask, 0});
    }
  }
  while (sz(heap)) {
    auto s = heap.top();
    heap.pop();
    if (s.dist != dist[s.node][s.mask]) continue;
    for (auto &e : adj[s.node]) {
      if (dist[e.to][s.mask] > s.dist + e.cost) {
        dist[e.to][s.mask] = s.dist + e.cost;
        heap.push({e.to, s.mask, dist[e.to][s.mask]});
      }
      rep(t, 0, 5) {
        if (!(s.mask & (1 << t))) {
          int nm = s.mask | (1 << t);
          int dc = e.cost * (10 - (t+1)) / 10;
          if (dist[e.to][nm] > s.dist + dc) {
            dist[e.to][nm] = s.dist + dc;
            heap.push({e.to, nm, dist[e.to][nm]});
          }
        }
      }
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int S, P[5];
    cin >> S;
    rep(i, 0, 5) cin >> P[i];
    rep(i, 0, 5) if (P[i] == -1) P[i] = INF;
    int ans = INF;
    rep(mask, 0, MAX_MASK) {
      int cost = dist[S][mask];
      rep(t, 0, 5) if (mask & (1 << t)) cost += P[t];
      mnto(ans, cost);
    }
    cout << (ans >= INF ? -1 : ans) << '\n';
  }
}
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...