Cod sursa(job #2713320)

Utilizator Iulia25Hosu Iulia Iulia25 Data 27 februarie 2021 18:14:08
Problema Ubuntzei Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.22 kb
#include <fstream>
#include <queue>
#include <vector>

using namespace std;

ifstream cin ("ubuntzei.in");
ofstream cout ("ubuntzei.out");


struct idk  {
	int cost, nod;
};

auto cmp = [](idk x, idk y)  {
	return (x.cost > y.cost);
};

priority_queue <idk, vector <idk>, decltype(cmp)> pq(cmp);

struct idk2  {
	int cost, nod, stare;
};

auto cmp2 = [](idk2 x, idk2 y)  {
	return (x.cost > y.cost);
};

priority_queue <idk2, vector <idk2>, decltype(cmp2)> pq2(cmp2);


vector <pair <int, int> > v[2002];

int n, k;
int loc[2002], r[20];
int dp[65540][17], d[17][17], dist[17][2002];

void dijkstra(int nod, int i)  {
  pq.push({0, nod});
  while (!pq.empty())  {
    int cost = pq.top().cost;
    int nod = pq.top().nod;
    pq.pop();
    if (dist[i][nod] != 0 && dist[i][nod] < cost)
      continue;
    for (auto it : v[nod])  {
      if (loc[it.first] == i)
        continue;
      if (dist[i][it.first] != 0 && dist[i][it.first] < cost + it.second)
        continue;
      pq.push({cost + it.second, it.first});
      dist[i][it.first] = cost + it.second;
    }
  }
}

void dijkstra2()  {
  pq2.push({0, 0, 0});
  for (int i = 0; i < (1 << k); ++i)
    for (int j = 0; j <= k; ++j)
      dp[i][j] = 1e9;
  dp[0][0] = 0;
  while (!pq2.empty())  {
    int cost = pq2.top().cost;
    int nod = pq2.top().nod;
    int stare = pq2.top().stare;
    pq2.pop();
    if (cost > dp[stare][nod])
      continue;
    for (int i = 0; i < k; ++i)  {
      int stare2 = (stare | (1 << i));
      if (i + 1 != nod && dp[stare2][i + 1] > cost + d[nod][i + 1]) {
        dp[stare2][i + 1] = cost + d[nod][i + 1];
        pq2.push({cost + d[nod][i + 1], i + 1, stare2});
      }
    }
  }
}

int main()  {
	int m, x, y, C;
	cin >> n >> m >> k;
	for (int i = 1; i <= k; ++i)  {
		cin >> x;
		r[i] = x;
		loc[x] = i;
	}
	for (int i = 1; i <= m; ++i)  {
		cin >> x >> y >> C;
		v[x].push_back({y, C});
		v[y].push_back({x, C});
	}
	r[0] = 1, loc[1] = 0;
	dijkstra(1, 0);
	for (int i = 1; i <= k; ++i)
    dijkstra(r[i], i);
  for (int i = 0; i <= k; ++i)  {
    for (int j = i + 1; j <= k; ++j)  {
      d[i][j] = d[j][i] = dist[i][r[j]];
    }
  }
  int ans = 2e9;
  dijkstra2();
  for (int i = 0; i <= k; ++i)
    ans = min(ans, dp[(1 << k) - 1][i] + dist[i][n]);
  cout << ans;
	return 0;
}