Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. using namespace std;
  4. #define maxchild 7
  5. #define maxsize 8
  6.  
  7. class Node {
  8.     int count;
  9.     int child[maxchild];
  10.     bool visited;
  11.     int parent;
  12.     int duration;
  13.     Node();
  14.     ~Node();
  15. };
  16.  
  17. Node::Node() {
  18.     count = 0;            
  19.     visited = 0;
  20.     parent = 0;
  21.     duration = 0;
  22. }
  23.  
  24. Node :: ~Node() {
  25. }
  26.  
  27. void read(Node * G[], FILE * fp , char s [20]) {
  28.     fp = fopen("s", "r");
  29.     int index;
  30.     for (int i = 0; i < maxsize; i++) {
  31.         G[i] = new Node;
  32.     }
  33.     for (int i = 0; i < maxsize; i++) {
  34.         fscanf(fp, "%d" , &index);
  35.         fscanf(fp, "%d", &G[index]->count);
  36.         for (int i = 0; i < G[index]->count; i++) {
  37.             fscanf(fp, "%d", &G[index]->child[i]);
  38.         }
  39.         fscanf(fp, "%d", &G[index]->duration);
  40.         fscanf(fp, "%d", &G[index]->visited);
  41.     }
  42. }
  43.  
  44. void initialization(Node * G[], int start, int size) {
  45.  
  46.     for (int i = 0; i < size; i++) {
  47.         G[i]->duration = 0;
  48.         G[i]->parent = -1;
  49.     }
  50.     G[start]-> duration = 4;
  51.     longestpath(G, start, size);
  52. }
  53.  
  54.  
  55.  
  56. void longestpath(Node * G[], int start, int size) {
  57.     bool found;
  58.     int w, sum, ed , min , index;
  59.     G[start]->visited = true;
  60.     for (int i = 0; i < G[start]->count; i++) {
  61.         w = G[start]->child[i];
  62.         ed = G[start]->duration;
  63.         sum = ed + G[start]->duration;
  64.         if (sum < G[w]->duration) {
  65.             G[w]->duration = sum;
  66.             G[w]->parent = start;
  67.         }
  68.     }
  69.  
  70.     found = false;
  71.     min = 100000;
  72.     for (int i = 0; i < size; i++) {
  73.         if (G[i]->visited = false) {
  74.             if (G[i]->duration < min) {
  75.                 found = true;
  76.                 min = G[i]->duration;
  77.                 index = i;
  78.             }
  79.         }
  80.     }
  81.     if (found) {
  82.         longestpath(G, index, size);
  83. }
  84. }
  85.  
  86. void print(Node * G[], int start, int end) {
  87.     if (G[end]->parent == start)
  88.         cout << start << "=>" << endl;
  89.     else {
  90.         print(G, start, G[end] -> parent);
  91.         cout << "=>" << endl;
  92.     }
  93. }
  94.  
  95. int main()
  96. {
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement