Pagini recente » Cod sursa (job #1554610) | Cod sursa (job #2742075) | Cod sursa (job #602663) | Cod sursa (job #2118246) | Cod sursa (job #2791882)
#include <iostream>
#include<bits/stdc++.h>
#define NMAX 50002
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
vector < pair < int, int > > v[50002];
bitset<50005> visited;
int dist[50002];
struct comp
{
bool operator()(pair<int, int > a, pair<int,int> b)
{
return a.second>b.second;
}
};
priority_queue<pair<int,int>,vector<pair<int,int> >,comp>pq;
void bfs(int node)
{
dist[node]=0;
pq.push({1,0});
while(!pq.empty())
{
pair<int,int> curr=pq.top();
pq.pop();
if(!visited[curr.first])
{
visited[curr.first]=1;
pair<int,int> it;
for(it : v[curr.first])
{
if(!visited[it.first]&& dist[it.first]>dist[curr.first]+it.second)
{
dist[it.first]=dist[curr.first]+it.second;
pq.push({it.first,dist[it.first]});
}
}
}
}
}
int main()
{
int n,m;
fin>>n>>m;
for(int i=0; i<m; i++)
{
int x,y,cost;
fin>>x>>y>>cost;
v[x].push_back({y,cost});
}
for(int i=1; i<=n; i++)
dist[i]=2000000;
bfs(1);
for(int i=2; i<=n; i++)
if(dist[i]==2000000)
fout<<"0";
else
fout<<dist[i]<<" ";
return 0;
}