Pagini recente » Cod sursa (job #1058410) | Cod sursa (job #1351255) | Cod sursa (job #3148708) | Cod sursa (job #1335892) | Cod sursa (job #2705554)
#include<iostream>
#include<fstream>
#include<vector>
#include<cstring>
#include<map>
#include<algorithm>
#include<set>
#include<deque>
#include<queue>
#define FAST cin.tie(NULL); ios_base::sync_with_stdio(false);
using namespace std;
ifstream fin("input");
void init(){
FAST
#ifndef ONLINE_JUDGE
cin.rdbuf(fin.rdbuf());
#endif
}
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
const int mx=60000;
const long long inf=2e18;
struct edge{
int destination;
long long cost;
};
struct node{
int index;
long long cost;
};
struct compare{
bool operator ()(node&a,node&b){
return a.cost>b.cost;
}
};
int n,m;
long long result[mx];
vector<edge> g[mx];
void read(){
in>>n>>m;
int a,b,c;
for(int i=0;i<m;i++){
in>>a>>b>>c;
g[a-1].push_back({b-1,c});
}
for(int i=0;i<n;i++)
result[i]=inf;
}
void solve(){
priority_queue<node,vector<node>,compare> q;
q.push({0,0});
result[0]=0;
while(!q.empty()){
node here=q.top();
q.pop();
if(result[here.index]<here.cost)
continue;
result[here.index]=here.cost;
for(edge k:g[here.index]){
int next=k.destination;
long long newcost=k.cost+here.cost;
if(newcost<result[next]){
q.push({next,newcost});
}
}
}
for(int i=1;i<n;i++){
if(result[i]==inf)
out<<0<<" ";
else out<<result[i]<<" ";
}
}
int main(){
init();
read();
solve();
return 0;
}