Pagini recente » Cod sursa (job #2896354) | Cod sursa (job #3137082) | Cod sursa (job #2520213) | Cod sursa (job #1828228) | Cod sursa (job #145221)
Cod sursa(job #145221)
/*
* N log N
*/
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const long MAX = 50010;
const long inf = 1<<30;
struct muchie {long x,c; muchie *n;};
class nod {
public :
muchie *a;
nod() {
a = 0;
}
void add(long x, long y) {
muchie *tmp = new muchie;
tmp -> x = x; tmp -> c = y;
tmp -> n = a;
a = tmp;
return ;
}
} G[MAX];
inline long min (long x, long y) { return (x<y)?x:y; }
long n, m, s;
long D[MAX];
bool U[MAX];
struct comp {
bool operator()(long x, long y) {
return D[x] > D[y];
}
};
priority_queue<long, vector<long>, comp> Q;
void dijkstra(long source) {
for (long i=0; i<=n; ++i) D[i] = inf;
D[source] = 0;U[source] = true;
Q.push(source);
while ( 1 ) {
if ( Q.empty() ) break;
long x = Q.top(); Q.pop();
// fprintf(stderr, "-- %ld\n", x);
for (muchie *p=G[x].a; p; p=p->n)
if ( D[p->x] > D[x]+p->c ) {
D[p->x] = D[x] + p->c;
if ( !U[p->x] ) {
Q.push(p->x);
U[p->x] = true;
}
}
}
}
int main() {
long i;
freopen("dijkstra.in", "r", stdin);
scanf("%ld %ld", &n, &m);
for (i=0; i<m; ++i) {
long x,y,c;
scanf("%ld %ld %ld", &x, &y, &c);
G[x].add(y,c);
}
fclose(stdin);
dijkstra(1);
freopen("dijkstra.out", "w", stdout);
for (i=2; i<n; ++i)
if ( D[i]<inf )
printf("%ld ", D[i]);
else
printf("0 ");
if ( D[i]<inf )
printf("%ld\n", D[i]);
else
printf("0\n");
return 0;
}