Pagini recente » Cod sursa (job #2837729) | Cod sursa (job #1457688) | Cod sursa (job #528973) | Cod sursa (job #374924) | Cod sursa (job #2406226)
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <queue>
#include <vector>
#include <climits>
#include <fstream>
using namespace std;
#define INF INT_MAX
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int sz = 10001;
vector<pair<int, int> > a[sz];
int dis[sz];
bool vis[sz] = { 0 };
void Dijkstra(int source, int n)
{
for (int i = 0; i<sz; i++)
dis[i] = INF;
class prioritize{ public: bool operator ()(pair<int, int>&p1, pair<int, int>&p2){ return p1.second>p2.second; } };
priority_queue<pair<int, int>, vector<pair<int, int> >, prioritize> pq;
pq.push(make_pair(source, dis[source] = 0));
while (!pq.empty())
{
pair<int, int> curr = pq.top();
pq.pop();
int cv = curr.first, cw = curr.second;
if (vis[cv])
continue;
vis[cv] = true;
for (int i = 0; i<a[cv].size(); i++)
if (!vis[a[cv][i].first] && a[cv][i].second + cw<dis[a[cv][i].first])
pq.push(make_pair(a[cv][i].first, (dis[a[cv][i].first] = a[cv][i].second + cw)));
}
}
int main()
{
int n, m, x, y, w;
fin >> n >> m;
for (int i = 0; i<m; i++)
{
fin >> x >> y >> w;
a[x].push_back(make_pair(y, w));
a[y].push_back(make_pair(x, w));
}
Dijkstra(1, n);
for (int i = 2; i <= n; i++)
{
//cout << "Vertex: " << i << " , Distance: ";
if (dis[i] != 0)
{
fout << dis[i] << " ";
}
}
return 0;
}