Pagini recente » Cod sursa (job #398304) | Cod sursa (job #2856034) | Cod sursa (job #37709) | Cod sursa (job #1607275) | Cod sursa (job #2735827)
#include <bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define N 50005
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
typedef long long ll;
int n,m,dist[N],x,y,c;
bool use[N];
pair <int,int> t;
vector <pair<int,int > > g[N];
priority_queue<pair <int,int> , vector <pair <int,int> >, std::greater<pair <int,int> > > pq;
int main()
{
fin>>n>>m;
for(int i=1;i<=m;i++)
{
fin>>x>>y>>c;
g[x].pb({y,c});
}
for(int i=2;i<=n;i++)
dist[i]=INT_MAX;
pq.push({1,0});
while(!pq.empty())
{
t=pq.top();
use[t.st]=0;
pq.pop();
for(auto it : g[t.st])
{
if(dist[it.st]>dist[t.st]+it.nd)
{
dist[it.st]=dist[t.st]+it.nd;
if(!use[it.st])
{
use[it.st]=1;
pq.push(it);
}
}
}
}
for(int i=2;i<=n;i++)
fout<<dist[i]<<' ';
return 0;
}