Pagini recente » Cod sursa (job #2702330) | Cod sursa (job #1331006) | Cod sursa (job #760025) | Cod sursa (job #268543) | Cod sursa (job #1049189)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int inf= 1<<30;
const int nmax= 50000;
struct str {
int x, y;
};
vector <str> v[nmax+1];
struct str_cmp {
bool operator() ( const str &x, const str &y ) {
if ( x.x==y.x ) {
return x.y>y.y;
} else {
return x.x>y.x;
}
}
};
priority_queue <str, vector <str>, str_cmp> q;
inline str mp ( int x, int y ) {
str sol;
sol.x= x;
sol.y= y;
return sol;
}
int d[nmax+1];
bool u[nmax+1];
void dijkstra( ) {
q.push(mp(1,0));
while ( !q.empty() ) {
str x;
x= q.top();
q.pop();
if ( u[x.x]==1 ) {
continue;
}
u[x.x]= 1;
for ( int i= 0; i<(int)v[x.x].size(); ++i ) {
if ( d[x.x]+v[x.x][i].y<d[v[x.x][i].x] ) {
d[v[x.x][i].x]= d[x.x]+v[x.x][i].y;
if ( u[v[x.x][i].x]==0 ) {
q.push(mp(v[x.x][i].x, d[v[x.x][i].x] ));
}
}
}
}
}
int main( ) {
int n, m;
fin>>n>>m;
for ( int i= 2; i<=n; ++i ) {
d[i]= inf;
}
for ( ; m>0; --m ) {
int a, b, c;
fin>>a>>b>>c;
v[a].push_back(mp(b, c));
}
dijkstra();
for ( int i= 2; i<=n; ++i ) {
if ( d[i]==inf ) {
d[i]= 0;
}
fout<<d[i]<<" ";
}
fout<<"\n";
return 0;
}