Pagini recente » Cod sursa (job #1647488) | Cod sursa (job #2955725) | Cod sursa (job #1557131) | Cod sursa (job #1883442) | Cod sursa (job #2660612)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("ubuntzei.in");
ofstream g("ubuntzei.out");
const int NMAX = 201;
const int KMAX = 11;
const int INF = (1 << 26);
int N, M, K;
bool land[NMAX];
vector < pair < int, int > > G[NMAX];
int dist[NMAX][KMAX];
// dp[i][k] - costul traseului minim ca sa ajung din 1 in i, trecand exact prin K localitati
struct idk {
int node, k, cost;
};
struct idk_comparator {
bool operator()(const idk& a, const idk& b) const {
return a.cost > b.cost;
}
};
int dijkstra(const int& SRC) {
for(int i = 0;i <= NMAX;++i)
for(int j = 0;j <= KMAX;++j)
dist[i][j] = INF;
priority_queue < idk, vector < idk >, idk_comparator > pq;
if(land[SRC]) {
dist[SRC][1] = 0;
pq.push({ SRC, 1, 0 });
}else {
dist[SRC][0] = 0;
pq.push({ SRC, 0, 0 });
}
while(!pq.empty()) {
idk data = pq.top();
pq.pop();
if(dist[data.node][data.k] == data.cost) {
for(pair < int, int >& neighbor : G[data.node]) {
const int COST = data.k + (land[neighbor.first] == true ? 1 : 0);
if(dist[data.node][data.k] + neighbor.second < dist[neighbor.first][COST]) {
dist[neighbor.first][COST] = dist[data.node][data.k] + neighbor.second;
pq.push({ neighbor.first, COST, dist[neighbor.first][COST] });
}
}
}
}
return dist[N][K];
}
int main() {
f >> N >> M >> K;
for(int i = 1;i <= K;++i) {
int node;
f >> node;
land[node] = true;
}
while(M--) {
int x, y, c;
f >> x >> y >> c;
G[x].push_back({ y, c });
G[y].push_back({ x, c });
}
g << dijkstra(1);
return 0;
}