Cod sursa(job #2479036)

Utilizator isa_tudor_andreiAndrei Tudor isa_tudor_andrei Data 23 octombrie 2019 09:43:09
Problema Ubuntzei Scor 45
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <bits/stdc++.h>
#define NMAX 2000
#define KMAX 66000

using namespace std;

ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");

const long long INF = 200000000001;

struct Edge {
  long long dest, cost;
};

struct Drum{
  int bitmask;
  int dest;
  long long cost;
  bool operator<(const Drum& other) const
  {
        return cost>other.cost;
  }
};

vector<Edge> G[NMAX + 1];
priority_queue<Drum> pq;
long long dp[KMAX][NMAX + 1];
int n, m, k, f[NMAX + 1], poz[NMAX + 1];

void Bellman() {
  for( int i = 0; i <= (1<<k) - 1; i ++ )
    for( int j = 1; j <= n; j ++ )
      dp[i][j] = INF;
  while( !pq.empty() ) {
    int node = pq.top().dest;
    long long cost = pq.top().cost;
    int config = pq.top().bitmask;
    pq.pop();
    if( dp[config][node] > cost ) {
      dp[config][node] = cost;
      for( auto it : G[node] ) {
        int newconfig = config;
        if( f[it.dest] == 1 )
          newconfig = config | ( 1<<poz[it.dest] );
        if( dp[newconfig][it.dest] > cost + it.cost )
          pq.push({ newconfig, it.dest, it.cost + cost });
      }
    }
  }
}

int main() {
    fin>>n>>m;
    fin>>k;
    for( int i = 1; i <= k; i ++ ) {
      int x;
      fin>>x;
      f[x] = 1;
      poz[x] = i - 1;
    }
    for( int i = 1; i <= m; i ++ ) {
      long long x, y, p;
      fin>>x>>y>>p;
      G[x].push_back({y,p});
      G[y].push_back({x,p});
    }
    pq.push({0,1,0});
    Bellman();
    fout<<dp[(1<<k) - 1][n];
    return 0;
}