Pagini recente » Cod sursa (job #1592850) | Cod sursa (job #2584327) | Cod sursa (job #932415) | Cod sursa (job #2329718) | Cod sursa (job #2073908)
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int NMAX = 50000;
const int INF = 2000000000;
int n;
bool vis[NMAX+5];
int dist[NMAX+5];
vector <pair <int, int> > graph[NMAX+5];
struct cmp
{
bool operator() (const pair <int, int> a, const pair <int, int> b)
{
return a.second > b.second;
}
};
void Dijkstra(int nod)
{
priority_queue < pair <int, int> , vector < pair <int, int> >, cmp > q;
dist[nod] = 0;
q.push(pair <int, int> (nod, 0));
while(!q.empty())
{
pair <int, int> top = q.top();
q.pop();
if(vis[top.first]) continue;
vis[top.first] = true;
for(int i=0; i<graph[top.first].size(); i++)
{
pair <int, int> New = graph[top.first][i];
if(!vis[New.first])
{
if(dist[top.first] + New.second < dist[New.first])
{
dist[New.first] = dist[top.first] + New.second;
q.push(pair <int, int> (New.first, dist[New.first]));
}
}
}
}
}
int main()
{
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
int n, m;
scanf("%d%d", &n, &m);
for(int i=0; i<m; i++)
{
int x, y, c;
scanf("%d%d%d", &x, &y, &c);
graph[x].push_back(pair <int, int> (y, c));
}
for(int i=1; i<=n; i++)
dist[i] = INF;
Dijkstra(1);
for(int i=2; i<=n; i++)
printf("%d ", dist[i] == INF ? 0 : dist[i]);
printf("\n");
return 0;
}