Pagini recente » Profil ciorile.chioare | Cod sursa (job #1125751) | Cod sursa (job #2007572) | Cod sursa (job #890798) | Cod sursa (job #1791755)
//#include <iostream>
#include <queue>
#include <string.h>
#include <vector>
#include <fstream>
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
const int maxn = 50005;
const int oo = 0x3f3f3f3f;
int n, m, dist[maxn],start;
vector <pair<int, int> > g[maxn];
inline void dijkstra(int stnode) {
priority_queue <pair<int, int> > q;
memset(dist, oo, sizeof(dist));
dist[stnode] = 0;
q.push(make_pair(0, stnode));
while(!q.empty()) {
int cost = q.top().first;
int node = q.top().second;
q.pop();
if(dist[node] != cost)
continue;
int nr=g[node].size();
for(int i=0;i<nr;++i) {
if(dist[g[node][i].first] > cost + g[node][i].second) {
dist[g[node][i].first] = cost + g[node][i].second;
q.push(make_pair(dist[g[node][i].first], g[node][i].first));
}
}
}
for(int i = 2 ; i <n-1 ; ++ i) {
if(dist[i] == oo )
dist[i] = 0;
cout << dist[i] << ' ';
}
if(dist[n]==oo) cout<<0;
else cout<<dist[n];
}
int main() {
int t;
//cin>>t;
// while(t--){
cin >> n >> m;
// for(int i=1;i<=n;++i) g[i].clear();
for(int i = 1 ; i <= m ; ++ i) {
int x, y, z;
cin >> x >> y >> z;
g[x].push_back(make_pair(y, z));
g[y].push_back(make_pair(x, z));
}
// cin>>start;
dijkstra(1);
cout<<"\n";
// }
}