Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. // graph_test.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4. #include <iostream>
  5. #include <cstdio>
  6. #include <cassert>
  7. #include <fstream>
  8.  
  9. #define MAXV 1000
  10.  
  11. struct edgenode;
  12.  
  13.  struct edgenode
  14. {
  15.     size_t y;
  16.     int weight;
  17.     edgenode* next;
  18. };
  19. typedef struct
  20. {
  21.     edgenode* edges[MAXV];
  22.     int degree[MAXV];
  23.     size_t nverticles;
  24.     size_t nedges;
  25.     bool directed;
  26. } graph;
  27.  
  28. void initialize_graph(graph* g, bool directed)
  29. {
  30.     g->nverticles = 0;
  31.     g->nedges = 0;
  32.     g->directed = directed;
  33.     for (size_t i = 0; i < MAXV; ++i)
  34.     {
  35.         g->degree[i] = 0;
  36.         g->edges[i] = NULL;
  37.     }
  38. }
  39.  
  40. void insert_edge(graph* g, size_t x, size_t y, bool directed)
  41. {
  42.     edgenode* e;
  43.     e = (edgenode*)malloc(sizeof(edgenode));
  44.     assert(e);
  45.     e->y = y;
  46.     e->weight = 1; // Насчет веса в условии задачи ничего не сказано.. Пусть будет 1
  47.     e->next = g->edges[x];
  48.     g->edges[x] = e;
  49.     g->degree[x]++;
  50.     if (directed == false)
  51.         insert_edge(g, y, x, true);
  52.     else
  53.         g->nedges++;
  54. }
  55.  
  56. typedef struct
  57. {
  58.     bool visited[MAXV];
  59.     int distance[MAXV];
  60. } dijkstra_find_ctx;
  61.  
  62. void init_dijkstra_find_ctx(dijkstra_find_ctx* c)
  63. {
  64.     for (size_t i = 0; i < MAXV; ++i)
  65.     {
  66.         c->visited[i] = false;
  67.         c->distance[i] = INT32_MAX;
  68.     }
  69. }
  70.  
  71. void dijkstra(graph* g, dijkstra_find_ctx* c, size_t x)
  72. {
  73.     c->distance[x] = 0;
  74.     size_t min = x;
  75.     while (c->visited[min] == false)
  76.     {
  77.         for (edgenode* e = g->edges[min]; e != NULL; e = e->next)
  78.         {
  79.             if (c->distance[e->y] > c->distance[min] + e->weight)
  80.                 c->distance[e->y] = c->distance[min] + e->weight;
  81.         }
  82.         c->visited[min] = true;
  83.  
  84.         int dist = INT32_MAX;
  85.         min = 1;
  86.         for (size_t i = 1; i < g->nverticles; ++i)
  87.         {
  88.             if (c->visited[i] == false && c->distance[i] < dist )
  89.             {
  90.                 dist = c->distance[i];
  91.                 min = i;
  92.             }
  93.         }
  94.     }
  95. }
  96.  
  97. int main()
  98. {
  99.     std::ifstream ifs("input.txt");
  100.     std::ofstream ofs("output.txt");
  101.  
  102.     graph g;
  103.     initialize_graph(&g, false);
  104.  
  105.     size_t vcount;
  106.     ifs >> vcount;
  107.     g.nverticles = vcount;
  108.     for (size_t i = 0; i < vcount; ++i)
  109.     {
  110.         for (size_t j = 0; j < vcount; ++j)
  111.         {
  112.             int flag;
  113.             ifs >> flag;
  114.             if (flag)
  115.                 insert_edge(&g, i, j, false);
  116.         }
  117.     }
  118.  
  119.     size_t start;
  120.     std::cout << "Please input start vertex id.." << std::endl;
  121.     std::cin >> start;
  122.  
  123.     dijkstra_find_ctx dijkstra_ctx;
  124.     init_dijkstra_find_ctx(&dijkstra_ctx);
  125.     dijkstra(&g, &dijkstra_ctx, start);
  126.  
  127.     for (size_t i = 0; i < vcount; ++i)
  128.     {
  129.         ofs << dijkstra_ctx.distance[i] << " ";
  130.     }
  131.  
  132.     return 0;
  133. }
  134.  
  135. // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
  136. // Debug program: F5 or Debug > Start Debugging menu
  137.  
  138. // Tips for Getting Started:
  139. //   1. Use the Solution Explorer window to add/manage files
  140. //   2. Use the Team Explorer window to connect to source control
  141. //   3. Use the Output window to see build output and other messages
  142. //   4. Use the Error List window to view errors
  143. //   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
  144. //   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement