Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Dijkstra.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- int main()
- {
- setlocale(0, "");
- int C[10][10];
- int n, m;
- int x, y, c;
- cout << "Введите кол-во вершин ";
- cin >> n;
- cout << endl;
- cout << "Введите кол-во ребер ";
- cin >> m;
- cout << endl;
- for (int i = 1; i <= n; i++)
- for (int j = 1; j <= n; j++)
- C[i][j] = 100;
- for (int i = 1; i <= m; i++) {
- cout << "Введите откуда ребро, куда ребро, вес ребра " << endl;
- cin >> x >> y >> c;
- C[x][y] = c;
- }
- for (int i = 1; i <= n; i++) {
- cout << endl;
- for (int j = 1; j <= n; j++) {
- cout << " " << C[i][j];
- }
- }
- cout << endl;
- int S[10];
- int D[10];
- for (int i = 1; i <= n; i++) {
- S[i] = 0;
- D[i] = C[1][i];
- }
- S[1] = 1; int min, w;
- //
- for (int i = 1; i <= n-1; i++) {
- min = 100;
- for (int j = 2; j <= n; j++) {
- if ((S[j] == 0) && (D[j] < min)) {
- min = D[j];
- w = j;
- }
- }
- S[w] = 1;
- for (int j = 2; j <= n; j++) {
- if ((D[j] > D[w] + C[w][j]) && (S[j] == 0)) {
- D[j] = D[w] + C[w][j];
- }
- }
- }
- for (int i = 2; i <= n; i++) {
- cout << "k " << i << "-й вершине путь " << D[i] << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement