Pagini recente » Cod sursa (job #1388599) | Cod sursa (job #2086928) | Cod sursa (job #566371) | Cod sursa (job #2278345) | Cod sursa (job #1611162)
#include <cstdio>
#include <iostream>
#include <queue>
#define nmax 50004
#define inf 0x7fffffff
using namespace std;
int n,m1;
vector< pair<int,int> > m[nmax];
int d[nmax];
class mycomparison
{
public:
bool operator () ( int a,int b)
{
return d[a]>d[b];
}
};
priority_queue<int,vector<int>,mycomparison> q;
int main()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
scanf("%d%d",&n,&m1);
int n1,n2,cst;
for(;m1;m1--)
{
scanf("%d%d%d",&n1,&n2,&cst);
m[n1].push_back(make_pair(n2,cst));
}
int i;
for(i=1;i<=n;i++) d[i]=inf;
d[1]=0;
int crt;
q.push(1);
while(!q.empty())
{
crt=q.top(); q.pop();
for(i=0;i<m[crt].size();i++)
if( d[m[crt][i].first] > d[crt]+m[crt][i].second)
{
q.push(m[crt][i].first);
d[m[crt][i].first]=d[crt]+m[crt][i].second;
}
}
for(int i=2;i<=n;i++) if(d[i]!=inf) printf("%d ",d[i]);
else printf("0 ");
fclose(stdin);
fclose(stdout);
return 0;
}