Pagini recente » Cod sursa (job #1345787) | Cod sursa (job #313533) | Cod sursa (job #1320661) | Cod sursa (job #573122) | Cod sursa (job #1197386)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#define max 50005
#define pb push_back
#define inf 0x3f3f3f3f
using namespace std;
priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > q;
vector <pair <int,int> > g[max];
int n,m;
int d[max];
void read()
{
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++)
{int a,b,c;
scanf("%d %d %d",&a,&b,&c);
g[a].pb(make_pair(c,b));
}
}
void dijkstra()
{
q.push(make_pair(0,1));
for(int i=2;i<=n;i++)
d[i]=inf;
while(!q.empty())
{
int p=q.top().second;
q.pop();
for(vector <pair <int,int> >::iterator it=g[p].begin();it!=g[p].end();it++)
if(d[it->second]>d[p]+it->first)
{d[it->second]=d[p]+it->first;
q.push(*it);}
}
}
int main()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
read();
dijkstra();
for(int i=2;i<=n;i++)
{if(d[i]!=inf)
printf("%d ",d[i]);
else printf("0 ");}
return 0;
}