Pagini recente » Cod sursa (job #1426848) | Cod sursa (job #1190580) | Cod sursa (job #2228159) | Cod sursa (job #616888) | Cod sursa (job #2035301)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#define INF 0x3f3f3f3f
#define NMAX 50005
using namespace std;
ifstream fi("dijkstra.in"); void input();
ofstream fo("dijkstra.out");
struct nodeData
{
int node, cost;
bool operator <(nodeData const &other) const {return cost>other.cost;}
};
int dist[NMAX];
vector<nodeData> graph[NMAX];
priority_queue<nodeData> q;
int N, M;
void Dijkstra(int beg)
{
dist[beg] = 0;
nodeData node;
node.cost = 0;
node.node = beg;
q.push(node);
while(!q.empty())
{
int cost = q.top().cost;
int x = q.top().node;
q.pop();
for(auto y:graph[x])
{
if(dist[x] + y.cost < dist[y.node])
{
dist[y.node] = dist[x] + y.cost;
q.push(y);
}
}
}
}
int main()
{
input();
Dijkstra(1);
for(int i=2; i<=N; ++i)
{
if(dist[i] != INF)
fo<<dist[i]<<' ';
else fo<<0<<' ';
}
}
void input()
{
fi>>N>>M;
int a,b,c;
for(; M; --M)
{
fi>>a>>b>>c;
nodeData node;
node.cost = c;
node.node = b;
graph[a].push_back(node);
node.node = a;
graph[b].push_back(node);
}
for(int i = 1; i<=N; ++i) dist[i] = INF;
}