贪婪算法-货物装载问题
最优解问题
限制条件--------满足条件=可行解
优化函数--------满足限制=最优解
贪心准则: 选择的依据或者标准
货物装载
拓扑排序
有向图得遍历
最小生成树
1 // change the length of an array 2 3 //changeLength1D.h 4 5 #ifndef changeLength1D_ 6 #define changeLength1D_ 7 8 #include "myExceptions.h" 9 10 using namespace std; 11 12 template<class T> 13 void changeLength1D(T*& a, int oldLength, int newLength) 14 { 15 if (newLength < 0) 16 throw illegalParameterValue("new length must be >= 0"); 17 18 T* temp = new T[newLength]; // new array 19 int number =oldLength>newLength?newLength:oldLength; // number to copy 20 copy(a, a + number, temp); 21 delete [] a; // deallocate old memory 22 a = temp; 23 } 24 25 #endif
1 // container struct used greedyLoading 2 3 //container.h 4 #ifndef container_ 5 #define container_ 6 7 struct container 8 { 9 int id, weight; 10 operator int () const {return weight;} 11 }; 12 13 #endif
1 // heap sort a[1:n] 2 3 //heapSort.h 4 #ifndef heapSort_ 5 #define heapSort_ 6 7 #include "maxHeap.h" 8 9 using namespace std; 10 11 template <class T> 12 void heapSort(T a[], int n) 13 {// Sort a[1:n] using the heap sort method. 14 // create a max heap of the elements 15 maxHeap<T> heap(1); 16 heap.initialize(a, n); 17 18 // extract one by one from the max heap 19 for (int i = n - 1; i >= 1; i--) 20 { 21 T x = heap.top(); 22 heap.pop(); 23 a[i+1] = x; 24 } 25 26 // save array a from heap destructor 27 heap.deactivateArray(); 28 } 29 30 #endif
1 // heap implementation of a max priority queue 2 // derives from the ADT maxPriorityQueue 3 4 //maxHeap.h 5 #ifndef maxHeap_ 6 #define maxHeap_ 7 8 #include "maxPriorityQueue.h" 9 #include "myExceptions.h" 10 #include "changeLength1D.h" 11 #include <sstream> 12 #include <algorithm> 13 14 using namespace std; 15 16 template<class T> 17 class maxHeap : public maxPriorityQueue<T> 18 { 19 public: 20 maxHeap(int initialCapacity = 10); 21 ~maxHeap() {delete [] heap;} 22 bool empty() const {return heapSize == 0;} 23 int size() const 24 {return heapSize;} 25 const T& top() 26 {// return max element 27 if (heapSize == 0) 28 throw queueEmpty(); 29 return heap[1]; 30 } 31 void pop(); 32 void push(const T&); 33 void initialize(T *, int); 34 void deactivateArray() 35 {heap = NULL; arrayLength = heapSize = 0;} 36 void output(ostream& out) const; 37 private: 38 int heapSize; // number of elements in queue 39 int arrayLength; // queue capacity + 1 40 T *heap; // element array 41 }; 42 43 template<class T> 44 maxHeap<T>::maxHeap(int initialCapacity) 45 {// Constructor. 46 if (initialCapacity < 1) 47 {ostringstream s; 48 s << "Initial capacity = " << initialCapacity << " Must be > 0"; 49 throw illegalParameterValue(s.str()); 50 } 51 arrayLength = initialCapacity + 1; 52 heap = new T[arrayLength]; 53 heapSize = 0; 54 } 55 56 template<class T> 57 void maxHeap<T>::push(const T& theElement) 58 {// Add theElement to heap. 59 60 // increase array length if necessary 61 if (heapSize == arrayLength - 1) 62 {// double array length 63 changeLength1D(heap, arrayLength, 2 * arrayLength); 64 arrayLength *= 2; 65 } 66 67 // find place for theElement 68 // currentNode starts at new leaf and moves up tree 69 int currentNode = ++heapSize; 70 while (currentNode != 1 && heap[currentNode / 2] < theElement) 71 { 72 // cannot put theElement in heap[currentNode] 73 heap[currentNode] = heap[currentNode / 2]; // move element down 74 currentNode /= 2; // move to parent 75 } 76 77 heap[currentNode] = theElement; 78 } 79 80 template<class T> 81 void maxHeap<T>::pop() 82 {// Remove max element. 83 // if heap is empty return null 84 if (heapSize == 0) // heap empty 85 throw queueEmpty(); 86 87 // Delete max element 88 heap[1].~T(); 89 90 // Remove last element and reheapify 91 T lastElement = heap[heapSize--]; 92 93 // find place for lastElement starting at root 94 int currentNode = 1, 95 child = 2; // child of currentNode 96 while (child <= heapSize) 97 { 98 // heap[child] should be larger child of currentNode 99 if (child < heapSize && heap[child] < heap[child + 1]) 100 child++; 101 102 // can we put lastElement in heap[currentNode]? 103 if (lastElement >= heap[child]) 104 break; // yes 105 106 // no 107 heap[currentNode] = heap[child]; // move child up 108 currentNode = child; // move down a level 109 child *= 2; 110 } 111 heap[currentNode] = lastElement; 112 } 113 114 template<class T> 115 void maxHeap<T>::initialize(T *theHeap, int theSize) 116 {// Initialize max heap to element array theHeap[1:theSize]. 117 delete [] heap; 118 heap = theHeap; 119 heapSize = theSize; 120 121 // heapify 122 for (int root = heapSize / 2; root >= 1; root--) 123 { 124 T rootElement = heap[root]; 125 126 // find place to put rootElement 127 int child = 2 * root; // parent of child is target 128 // location for rootElement 129 while (child <= heapSize) 130 { 131 // heap[child] should be larger sibling 132 if (child < heapSize && heap[child] < heap[child + 1]) 133 child++; 134 135 // can we put rootElement in heap[child/2]? 136 if (rootElement >= heap[child]) 137 break; // yes 138 139 // no 140 heap[child / 2] = heap[child]; // move child up 141 child *= 2; // move down a level 142 } 143 heap[child / 2] = rootElement; 144 } 145 } 146 147 template<class T> 148 void maxHeap<T>::output(ostream& out) const 149 {// Put the list into the stream out. 150 copy(heap + 1, heap + heapSize + 1, ostream_iterator<T>(cout, " ")); 151 } 152 153 // overload << 154 template <class T> 155 ostream& operator<<(ostream& out, const maxHeap<T>& x) 156 {x.output(out); return out;} 157 158 #endif
1 // abstract class max priority queue 2 // all methods are pure virtual functions 3 4 //maxPriorityQueue.h 5 #ifndef maxPriorityQueue_ 6 #define maxPriorityQueue_ 7 8 using namespace std; 9 10 template<class T> 11 class maxPriorityQueue 12 { 13 public: 14 virtual ~maxPriorityQueue() {} 15 virtual bool empty() const = 0; 16 // return true iff queue is empty 17 virtual int size() const = 0; 18 // return number of elements in queue 19 virtual const T& top() = 0; 20 // return reference to the max element 21 virtual void pop() = 0; 22 // remove the top element 23 virtual void push(const T& theElement) = 0; 24 // add theElement to the queue 25 }; 26 #endif
1 // exception classes for various error types 2 3 //myexceptions.h 4 #ifndef myExceptions_ 5 #define myExceptions_ 6 #include <string> 7 #include <iostream> 8 using namespace std; 9 10 // illegal parameter value 11 class illegalParameterValue 12 { 13 public: 14 illegalParameterValue(string theMessage = "Illegal parameter value") 15 {message = theMessage;} 16 void outputMessage() {cout << message << endl;} 17 private: 18 string message; 19 }; 20 21 // illegal input data 22 class illegalInputData 23 { 24 public: 25 illegalInputData(string theMessage = "Illegal data input") 26 {message = theMessage;} 27 void outputMessage() {cout << message << endl;} 28 private: 29 string message; 30 }; 31 32 // illegal index 33 class illegalIndex 34 { 35 public: 36 illegalIndex(string theMessage = "Illegal index") 37 {message = theMessage;} 38 void outputMessage() {cout << message << endl;} 39 private: 40 string message; 41 }; 42 43 // matrix index out of bounds 44 class matrixIndexOutOfBounds 45 { 46 public: 47 matrixIndexOutOfBounds 48 (string theMessage = "Matrix index out of bounds") 49 {message = theMessage;} 50 void outputMessage() {cout << message << endl;} 51 private: 52 string message; 53 }; 54 55 // matrix size mismatch 56 class matrixSizeMismatch 57 { 58 public: 59 matrixSizeMismatch(string theMessage = 60 "The size of the two matrics doesn't match") 61 {message = theMessage;} 62 void outputMessage() {cout << message << endl;} 63 private: 64 string message; 65 }; 66 67 // stack is empty 68 class stackEmpty 69 { 70 public: 71 stackEmpty(string theMessage = 72 "Invalid operation on empty stack") 73 {message = theMessage;} 74 void outputMessage() {cout << message << endl;} 75 private: 76 string message; 77 }; 78 79 // queue is empty 80 class queueEmpty 81 { 82 public: 83 queueEmpty(string theMessage = 84 "Invalid operation on empty queue") 85 {message = theMessage;} 86 void outputMessage() {cout << message << endl;} 87 private: 88 string message; 89 }; 90 91 // hash table is full 92 class hashTableFull 93 { 94 public: 95 hashTableFull(string theMessage = 96 "The hash table is full") 97 {message = theMessage;} 98 void outputMessage() {cout << message << endl;} 99 private: 100 string message; 101 }; 102 103 // edge weight undefined 104 class undefinedEdgeWeight 105 { 106 public: 107 undefinedEdgeWeight(string theMessage = 108 "No edge weights defined") 109 {message = theMessage;} 110 void outputMessage() {cout << message << endl;} 111 private: 112 string message; 113 }; 114 115 // method undefined 116 class undefinedMethod 117 { 118 public: 119 undefinedMethod(string theMessage = 120 "This method is undefined") 121 {message = theMessage;} 122 void outputMessage() {cout << message << endl;} 123 private: 124 string message; 125 }; 126 #endif
1 //贪婪算法-货物装载问题 2 //container.cpp 3 4 #include "container.h" 5 #include "heapSort.h" 6 #include <iostream> 7 using namespace std; 8 void containerLoading(container* c, int capacity, int numberOfContainers, int *x) 9 { 10 //令 x[i]=1,当i被装载 i(i>=1) 11 //按重量递增排列 12 heapSort(c, numberOfContainers); 13 int n = numberOfContainers; 14 //初始化x 15 for (int i = 1; i <= n; i++) 16 x[i] = 0; 17 //按重量顺序选择货物 18 for (int i = 1; i <= n&&c[i].weight <= capacity; i++) 19 { 20 //对c[i].id有足够的容量 21 x[c[i].id] = 1; 22 cout << "\t装载货物编号为:" << c[i].id; 23 capacity -= c[i].weight; //剩余容量 24 cout << "\t剩余容量:" << capacity << endl; 25 } 26 } 27 int main() 28 { 29 container MyContainer[9] = { {},{ 1, 100 }, { 2, 200 }, { 3, 50 }, { 4, 90 }, { 5, 150 }, { 6, 50 }, { 7, 20 }, { 8, 80 } }; 30 int Array[8] = { 0 }; 31 containerLoading(MyContainer, 400, 8, Array); 32 33 /* 34 装载货物编号为:7 剩余容量:380 35 装载货物编号为:3 剩余容量:330 36 装载货物编号为:6 剩余容量:280 37 装载货物编号为:8 剩余容量:200 38 装载货物编号为:4 剩余容量:110 39 装载货物编号为:1 剩余容量:10 40 */ 41 system("pause"); 42 return 0; 43 }
来源:https://www.cnblogs.com/mjgw/p/12590748.html