Pagini recente » Cod sursa (job #1282050) | infoarena - comunitate informatica, concursuri de programare | infoarena - comunitate informatica, concursuri de programare | Cod sursa (job #1282188) | Cod sursa (job #3281936)
#include <iostream>
#include <set>
#include <fstream>
#include <vector>
#include <climits>
#define inf INT_MAX
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
vector <pair <int, int>> graph[50005];
int n, m, start;
int dist[50005];
set <pair <int, int>> node;
void receive_data();
void dijkstra();
void print_data();
int main()
{
receive_data();
dijkstra();
print_data();
return 0;
}
void print_data()
{
for(int i = 2; i <= n; i ++)
if(dist[i] == inf)
fout << "0 ";
else
fout << dist[i] << ' ';
}
void dijkstra()
{
for(int i = 1; i <= n; i ++)
dist[i] = inf;
node.insert(make_pair(0, start));
dist[start] = 0;
while(!node.empty())
{
pair <int, int> nigga;
nigga = *node.begin();
int current_node = nigga.second;
for(int i = 0; i < graph[current_node].size(); i ++)
{
int neighbor = graph[current_node][i].first;
int weight = graph[current_node][i].second;
if(dist[neighbor] > dist[current_node] + weight)
{
if(node.find({dist[neighbor], neighbor}) != node.end())
node.erase(make_pair(dist[neighbor], neighbor));
node.insert(make_pair(dist[current_node] + weight, neighbor));
dist[neighbor] = dist[current_node] + weight;
}
}
node.erase(node.begin());
}
}
void receive_data()
{
fin >> n >> m;
start = 1;
int first, second, weight;
for(int i = 1; i <= m; i ++)
{
fin >> first >> second >> weight;
graph[second].push_back(make_pair(first, weight));
}
}