Cod sursa(job #2373788)

Utilizator cella.florescuCella Florescu cella.florescu Data 7 martie 2019 15:19:16
Problema Ubuntzei Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 2e3;
const int MAXK = 15;
const int INF = 0x3f3f3f3f;

struct PQNode {
  int node, conf, cost;
  bool operator < (const PQNode& other) const {
    return cost > other.cost;
  }
};

int dp[MAXN + 1][1 << MAXK], ubu[MAXN + 1];
vector < pair < int, int > > g[MAXN + 1];
priority_queue < PQNode > pq;

inline void update(int node, int conf, int cost) {
  if (cost < dp[node][conf]) {
    dp[node][conf] = cost;
    pq.push({node, conf, cost});
  }
}

int main()
{
    int n, m, k;
    ifstream fin("ubuntzei.in");
    fin >> n >> m >> k;
    memset(ubu, -1, sizeof ubu);
    for (int i = 0; i < k; ++i) {
      int u;
      fin >> u;
      ubu[u] = i;
    }
    for (int i = 0; i < m; ++i) {
      int x, y, z;
      fin >> x >> y >> z;
      g[x].push_back({y, z});
      g[y].push_back({x, z});
    }
    fin.close();
    memset(dp, INF, sizeof dp);
    dp[1][0] = 0;
    pq.push({1, 0, 0});
    while (pq.empty() == false) {
      int node = pq.top().node, cost = pq.top().cost, conf = pq.top().conf;
      pq.pop();
      if (dp[node][conf] == cost) {
        if (ubu[node] != -1)
          update(node, conf | (1 << ubu[node]), cost);
        for (auto it : g[node])
          update(it.first, conf, cost + it.second);
      }
    }
    ofstream fout("ubuntzei.out");
    fout << dp[n][(1 << k) - 1] << '\n';
    fout.close();
    return 0;
}