Pagini recente » Cod sursa (job #2685954) | Cod sursa (job #2695837) | Cod sursa (job #1439513) | Cod sursa (job #34547) | Cod sursa (job #3256813)
#include <bits/stdc++.h>
#define MOD 1000000007
#define NMAX 50500
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
struct poz{
int first, second;
bool operator<(poz b)const{
if(second!=b.second)
return second>b.second;
return first<b.first;
}
};
int n, m, dist[NMAX];
vector<pair<int,int>>v[NMAX];
priority_queue<poz>q;
void dijkstra()
{
q.push({1, 0});
dist[1]=0;
int x, y;
while(!q.empty())
{
x=q.top().first;
y=q.top().second;
q.pop();
if(dist[x]!=y)
continue;
for(auto i:v[x])
if(dist[i.first]>y+i.second)
{
dist[i.first]=y+i.second;
q.push({i.first,dist[i.first]});
}
}
}
int x, y, c;
int main()
{
fin>>n>>m;
for(int i=1;i<=n;++i)
{
fin>>x>>y>>c;
v[x].push_back({y,c});
}
for(int i=1;i<=n;++i)
dist[i]=MOD;
dijkstra();
for(int i=2;i<=n;++i)
if(dist[i]==MOD)
fout<<"0 ";else
fout<<dist[i]<<" ";
return 0;
}