forked from makrell38/CPSC8380
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedd_ip.h
105 lines (94 loc) · 2.23 KB
/
edd_ip.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#ifndef EDD_IP
#define EDD_IP
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <queue>
#include <vector>
#include <limits>
#include "graph.h"
using namespace std;
int minCost = std::numeric_limits<int>::max();
int **solution;
bool checkDone(Graph* g, int* T){
for(int i=0; i<g->numDestServers; i++){
if(T[g->R[i]-1] != 1)
return false;
}
return true;
}
int ** fillOut(Graph* g, int **path, int *T, int jump, int cost){
if(jump > g->dLimit){
return NULL;
}
int copy[g->v] ={0};
for(int i=0;i<g->v;i++)
copy[i] = T[i];
for(int i=1; i<=g->v; i++){
if(T[i-1] == 1){
for(int j=1;j<=g->v;j++){
if(g->graph[i][j] != 0 && T[j-1] != 1){
if(g->S[j-1] == 1){
copy[j-1] = 1;
path[i][j] = 1;
cost++;
}
}
}
}
}
if(checkDone(g, copy)){
if(cost < minCost){
minCost = cost;
solution = path;
return path;
}
return NULL;
}
fillOut(g, path, copy, jump+1, cost);
}
int ** findPath(Graph* g, int* comb, int len){
int T[g->v] = {0};
int **path = new int*[g->v+1];
for(int i=0;i<=g->v;i++){
path[i] = new int[g->v+1];
}
int cost = 0;
for(int i=0; i<len;i++){
T[comb[i]-1] = 1;
path[0][comb[i]] = 1;
cost += g->delta;
}
if(checkDone(g, T)){
if(cost < minCost){
minCost = cost;
solution = path;
return path;
}
return NULL;
}
path = fillOut(g, path, T, 1, cost);
return path;
}
int* combinations(Graph *g, int* comb, int last, int len){
int *ret = new int[len+1];
findPath(g, comb, len);
for(int i = 0; i < len; i++){
ret[i] = comb[i];
}
for(int i = last+1; i <= g->v; i++){
ret[len] = g->V[i];
combinations(g,ret, i, len+1);
}
return comb;
}
int** full(Graph *g){
int *comb = new int[1];
for(int i=1; i<=g->v; i++){
comb[0] = g->V[i];
combinations(g,comb, i, 1);
}
printPath(solution, g->v);
return solution;
}
#endif