Pagini recente » Cod sursa (job #275475) | Cod sursa (job #2861612) | Cod sursa (job #1492857) | Cod sursa (job #2880476) | Cod sursa (job #677715)
Cod sursa(job #677715)
//dijkstra cu priority_queue
#include<cstdio>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<set>
#include<queue>
#include<cstring>
#include<vector>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
struct muchie {
int vec,cost;
};
vector <muchie> G[50000+10];
int viz[50000+10],d[50000+10];
bool cmp(const pair<int,int> &a, const pair<int,int> &b)
{
if(a.first == b.first)
return a.second<b.second;
return a.first<b.first;
}
inline void dijkstra()
{
memset(d,0x3f,sizeof(d));
priority_queue < pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > Q;
d[1]=0;
Q.push(make_pair(0,1));
while(!Q.empty()) {
int nod=Q.top().second,cost=Q.top().first;
Q.pop();
for(vector<muchie>::iterator it=G[nod].begin(),stop=G[nod].end(); it<stop; ++it) {
if( d[it->vec] > cost + it->cost ) {
d[it->vec] = cost + it->cost;
Q.push( make_pair( d[it->vec], it->vec ) );
}
}
}
}
int main()
{
int n,m;
fin>>n>>m;
for(int i=1; i<=m; ++i) {
int from,to,cost;
fin>>from>>to>>cost;
G[from].push_back((muchie) {
to,cost
});
}
dijkstra();
for(int i=2; i<=n; ++i)
fout<<((d[i]==0x3f3f3f3f)?0:d[i])<<' ';
fout<<'\n';
return 0;
}