Pagini recente » Cod sursa (job #2838970) | Cod sursa (job #495212) | Cod sursa (job #1434990) | Cod sursa (job #1120729) | Cod sursa (job #1234167)
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <deque>
using namespace std;
const char infile[] = "dijkstra.in";
const char outfile[] = "dijkstra.out";
ifstream fin(infile);
ofstream fout(outfile);
const int MAXN = 100005;
const int oo = 0x3f3f3f3f;
typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;
const inline int min(const int &a, const int &b) { if( a > b ) return b; return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b; return a; }
const inline void Get_min(int &a, const int b) { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b) { if( a < b ) a = b; }
class DirectedGraph {
private:
vector<vector <pair<int, int>>> adj;
public:
DirectedGraph() {
}
DirectedGraph(int N) {
adj.resize(N);
}
void addEdge(int x, int y, int z) {
adj[x].push_back(make_pair(y, z));
}
vector <int> Dijkstra(int source) {
vector <int> dist(adj.size(), oo);
dist[source] = 0;
priority_queue <pair<int, int>, vector <pair<int, int>>, greater <pair<int, int> > > Q;
for(Q.push(make_pair(dist[source], source)) ; !Q.empty() ; Q.pop()) {
int node = Q.top().second;
int cost = Q.top().first;
//Q.pop();
if(dist[node] < cost)
continue;
for(auto it : adj[node])
if(dist[it.first] > cost + it.second) {
dist[it.first] = cost + it.second;
Q.push(make_pair(dist[it.first], it.first));
}
}
return dist;
}
};
int N, M;
int main() {
fin >> N >> M;
DirectedGraph G(N);
while(M --) {
int x, y, z;
fin >> x >> y >> z;
-- x; -- y;
G.addEdge(x, y, z);
}
vector <int> ans = G.Dijkstra(0);
for(int i = 1 ; i < static_cast<int>(ans.size()) ; ++ i) {
if(ans[i] == oo)
ans[i] = 0;
fout << ans[i] << ' ';
}
fin.close();
fout.close();
return 0;
}