Pagini recente » Cod sursa (job #1478560) | Cod sursa (job #1994200) | Cod sursa (job #2220978) | Cod sursa (job #2372130) | Cod sursa (job #3197617)
#include <fstream>
#include <vector>
#include <queue>
#define inf 300001
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
struct node
{
int ind;
int cost;
bool operator < (const node &other) const
{
return (cost>other.cost);
}
};
priority_queue<node> q;
vector<vector<node>> gf(50005);
vector<int> d(50005,inf);
int x, y, c, n, m;
void bfs()
{
d[1]=0;
q.push({1,0});
while(!q.empty())
{
int nod = q.top().ind;
for(auto nodcrt : gf[nod])
{
if(d[nodcrt.ind] > d[nod] + nodcrt.cost)
{
d[nodcrt.ind] = d[nod] + nodcrt.cost;
q.push({nodcrt.ind, d[nodcrt.ind]});
}
}
q.pop();
}
for(int i=2; i<=n; i++)
cout<<d[i]<<' ';
}
int main()
{
cin>>n>>m;
for(int i=1; i<=m; i++)
{
cin>>x>>y>>c;
gf[x].push_back({y,c});
gf[y].push_back({x,c});
}
bfs();
return 0;
}