Cod sursa(job #2367553)

Utilizator andreihagiuandreiasdasad andreihagiuandrei Data 5 martie 2019 11:22:24
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <queue>
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");

#define INF 0x3f3f3f3f
typedef pair<int, int> iPair;
void addEdge(vector<pair <int, int> > adj[], int u, int v, int wt) {
adj[u].push_back(make_pair(v, wt));
adj[v].push_back(make_pair(u, wt));
}

void shortestPath(vector<pair <int, int> > adj[], int V, int src) {
priority_queue <iPair, vector<iPair>, greater<iPair> > pq;
vector<int>dist(V, INF);
pq.push(make_pair(0, src));
dist[src]=0;

while (!pq.empty()) {
	int u = pq.top().second;
	pq.pop();


	for(auto x : adj[u])
	{
		int v=x.first;
		int weight=x.second;
		if(dist[v] > dist[u] + weight) {
			dist[v]=dist[u]+weight;
			pq.push(make_pair(dist[v],v));        }

	}

}

for(int i=1;i<V-1;i++) {
	fout << dist[i+1] << " ";
}
}

int main() {
 int V, E;
 fin >> E >> V;
 int x,y,z;
vector<iPair> adj[V];

for(int i=0;i<V;i++) {
	fin >> x >> y >> z;
	addEdge(adj, x, y, z);
}

	shortestPath(adj, V, 1);

	return 0;

}