Pagini recente » Cod sursa (job #477370) | Cod sursa (job #1350792) | Cod sursa (job #200072) | Cod sursa (job #1993857) | Cod sursa (job #3148738)
// DIJKSTRA's Algorithm.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
//ifstream f("dijkstra.in.txt");
//ofstream g("dijkstra.out.txt");
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
const int MAX_LIMIT = 2147483647;
struct rule
{
bool operator()(pair<int, int> a, pair<int, int> b)
{
return a.second > b.second;
}
};
void Print_Graph(vector<pair<int, int>> *graph, int n)
{
for (int i = 1; i <= n; i++)
{
cout << i << " : ";
const int num = graph[i].size();
for (int j = 0; j < num; j++)
{
cout << graph[i][j].first << ", " << graph[i][j].second << " ";
}
cout << "\n";
}
cout << "\n\n";
}
void Dijkstra(vector<pair<int,int>>* graph, int n, int start)
{
int* distance;
bool* visited;
bool* in_queue;
distance = new int[n + 1];
visited = new bool[n + 1];
in_queue = new bool[n + 1];
int *answers;
answers = new int[n + 1];
for (int i = 0; i <= n; i++)
{
distance[i] = MAX_LIMIT;
visited[i] = false;
in_queue[i] = false;
answers[i] = 0;
}
priority_queue<pair<int, int>, vector<pair<int, int>>, rule> pq;
distance[start] = 0;
in_queue[start] = true;
pq.push(make_pair(start, distance[start]));
while (!pq.empty())
{
int node = pq.top().first;
int dst = pq.top().second;
visited[node] = true;
answers[node] = dst;
in_queue[node] = false;
pq.pop();
const int sz = graph[node].size();
for (int i = 0; i < sz; i++)
{
if (!visited[graph[node][i].first] && distance[node] + graph[node][i].second < distance[graph[node][i].first])
{
distance[graph[node][i].first] = distance[node] + graph[node][i].second;
if (!in_queue[graph[node][i].first])
{
pq.push(make_pair(graph[node][i].first, distance[graph[node][i].first]));
in_queue[graph[node][i].first] = true;
}
}
}
}
for (int i = 1; i <= n; i++)
{
if (i == start)
continue;
g << answers[i] << " ";
}
delete[] distance;
delete[] visited;
delete[] answers;
delete[] in_queue;
}
int main()
{
unsigned n, m; // 'n' nodes, 'm' edges
unsigned a, b, length;
vector<pair<int, int>> *graph;
f >> n >> m;
graph = new vector<pair<int, int>>[n + 1];
for (int i = 0; i < m; i++)
{
f >> a >> b >> length;
graph[a].push_back(make_pair(b, length));
}
f.close();
// Print_Graph(graph, n);
Dijkstra(graph, n, 1);
///
delete[] graph;
g.close();
return 0;
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file