Pagini recente » Cod sursa (job #309934) | Cod sursa (job #816971) | Cod sursa (job #1415920) | Cod sursa (job #2727438) | Cod sursa (job #1879947)
#include <iostream>
#include <vector>
#include <queue>
#include <fstream>
#define NMAX 50050
#define inf 0x3f3f3f3f
using namespace std;
ifstream f("date.in");
ofstream g("date.out");
vector <pair <int,int> > G[NMAX];
priority_queue <pair <int,int> > Q;
int n,m,dist[NMAX],viz[NMAX];
void Dijkstra(int x)
{
int i;
dist[x]=0;
Q.push(make_pair(x,0));
viz[x]=1;
while(!Q.empty())
{
int cost=-Q.top().second;
int top=Q.top().first;
Q.pop();
for(i=0;i<G[top].size();++i)
{
if(dist[G[top][i].first]>cost+G[top][i].second)
dist[G[top][i].first]=cost+G[top][i].second;
if(viz[G[top][i].first]==0)
{
Q.push(make_pair(G[top][i].first,-G[top][i].second));
viz[G[top][i].first]==1;
}
}
}
for(i=1;i<=n;++i)
if(dist[i]==inf) dist[i]=0;
}
int main()
{
int x,y,z;
f>>n>>m;
for(int i=1;i<=m;++i)
{
f>>x>>y>>z;
G[x].push_back(make_pair(y,z));
}
for(int i=1;i<=n;++i)
dist[i]=inf;
Dijkstra(1);
for(int i=2;i<=n;++i) g<<dist[i]<<" ";
return 0;
}