Cod sursa(job #2978479)

Utilizator alin_simpluAlin Pop alin_simplu Data 13 februarie 2023 20:03:17
Problema Ubuntzei Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <fstream>
#include <queue>
#include <vector>
#include <climits>
using namespace std;

ofstream fout("ubuntzei.out");

using VI = vector<int>;
using PI = pair<int, int>;
using VVP = vector<vector<PI>>;
using VVI = vector<VI>;

VVP G;
VVI D; // d[i][j] = dist. de la i la j
VI D_0;
VI K; // loc. prin care trecem
int n, m, k;
const int Inf = INT_MAX;

void Read();
void Dijkstra(int , VI&);

int main(){
	Read();
	
	if (k == 2){
		Dijkstra(1, D_0);
		fout << D_0[n] << ' ';
	}
	else {
		for (int i = 0; i < k; ++i)
			Dijkstra(K[i], D[i]);
			
		int road_lenght = 0;
		
		for (int i = 1; i <= k; ++i)
			if (i + 1 < k)
				road_lenght += D[K[i]][K[i + 1]];
				
		fout << road_lenght;
	}
	
	return 0;
}

void Dijkstra(int src, VI& d){
	priority_queue<PI, vector<PI>, greater<PI>> Q;
	d[src] = 0;
	Q.emplace(0, src);
	
	while (!Q.empty()){
		auto[dx, x] = Q.top();
		Q.pop();
		
		if (dx > d[x]) continue;
		
		for (auto pair : G[x]){
			auto [y, w] = pair;
			
			if (d[y] > d[x] + w){
				d[y] = d[x] + w;
				Q.emplace(d[y], y);
				}
		}
	}
}

void Read(){
	ifstream fin("ubuntzei.in");
	
	fin >> n >> m >> k;
	G = VVP(n + 1);
	D_0 = VI(n + 1, Inf);
	
    if (k){
		K.emplace_back(0);
		K.emplace_back(1);
			for (int loc, i = 1;i <= k; i++){
				fin >> loc;
				K.emplace_back(loc);
			}
		K.emplace_back(n);
}

    k = K.size();
    D = VVI(k + 1, VI(k + 1, Inf));
	
	for(int x, y, w; m; m--){
		fin >> x >> y >> w;
		G[x].emplace_back(y, w);
		G[y].emplace_back(x, w);
	}
}