Pagini recente » Cod sursa (job #1248346) | Cod sursa (job #764911) | Cod sursa (job #2825607) | Cod sursa (job #3316223) | Cod sursa (job #1099753)
#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< pair<int, int> > Graph[MAXN];
typedef vector< pair<int, 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; }
/*
Dijkstra's Algirithm:
O(M log N)
greedy
-heap
*/
Graph G;
int N, M;
inline vector <int> Dijkstra(const int &stNode, const int &N) {
vector <int> dp(N, oo);
priority_queue<pair<int, int>, vector <pair<int, int> > ,greater <pair<int, int> > > Q;
dp[0] = 0;
Q.push(make_pair(0, stNode));
while(!Q.empty()) {
int cost = Q.top().first;
int Node = Q.top().second;
Q.pop();
if(dp[Node] < cost)
continue;
for(It it = G[Node].begin(), fin = G[Node].end(); it != fin ; ++ it)
if(dp[it->first] > dp[Node] + it->second) {
dp[it->first] = dp[Node] + it->second;
Q.push(make_pair(dp[it->first], it->first));
}
}
return dp;
}
int main() {
fin >> N >> M;
for(int i = 1 ; i <= M ; ++ i) {
int x, y, z;
fin >> x >> y >> z;
-- x;
-- y;
G[x].push_back(make_pair(y, z));
}
vector<int> dist = Dijkstra(0, N);
for(vector<int> :: iterator it = dist.begin() + 1, fin = dist.end(); it != fin ; ++ it)
if(*it == oo)
fout << "0 ";
else fout << *it << ' ';
fin.close();
fout.close();
return 0;
}