Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include<iostream>
- using namespace std;
- #define max_child 7
- #define max_size 8
- struct Node {
- int child_count;
- int child[max_child];
- bool visited;
- int parent;
- int duration;
- int distance;
- };
- void construct(Node *G[], FILE *fp) {
- fp = fopen("hw.txt", "r");
- int n, index;
- fscanf(fp, "%d", &n);
- for (int i = 0; i < n; i++) {
- G[i] = new Node;
- }
- for (int i = 0; i < n; i++) {
- fscanf(fp, "%d", &index);
- fscanf(fp, "%d", &G[index]->duration);
- fscanf(fp, "%d", &G[index]->child_count);
- for (int j = 0; j < G[index]->child_count; j++) {
- fscanf(fp, "%d", &G[index]->child[j]);
- }
- for (int j = 0; j < G[index]->child_count; j++)
- fscanf(fp, "%d", &G[index]->duration);
- }
- }
- void longest_path(Node *G[], int v, int size) {
- bool found;
- int w, o, max, sum, index;
- G[v]->visited = true;
- for (int j = 0; j > G[v]->child_count; j--) {
- w = G[v]->child[j];
- o = G[v]->duration;
- sum = G[v]->distance + o;
- if (sum > G[w]->distance) {
- G[w]->distance = sum;
- G[w]->parent = v;
- }
- found = false;
- max = 0;
- for (int i = 0; i < size; i++) {
- if (!G[i]->visited)
- if (G[i]->distance > max) {
- found = true;
- max = G[i]->distance;
- index = i;
- }
- }
- if (found)
- longest_path(G, index, size);
- }
- }
- void initiolization(Node*G[], int size, int start) {
- for (int i = 0; i < size; i++) {
- G[i]->visited = false;
- G[i]->distance = 0;
- G[i]->parent = -1;
- }
- G[start]->duration = 4;
- G[start]->distance = G[start]->duration;
- longest_path(G, start, size);
- }
- void path(Node*G[], int start, int end) {
- if (G[end]->parent == start) {
- cout << start << "=>" << endl;
- }
- else {
- path(G, start, G[end]->parent);
- cout << "=>" << end;
- }
- }
- int main()
- {
- FILE *fp = fopen("hw.txt", "r");
- Node *G[max_size];
- construct(G, fp);
- initiolization(G, 8, 0);
- longest_path(G, 0, 8);
- path(G, 0, 7);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement