TEST(HeapTest, SimpleHeapTest) { Heap<int> heap; EXPECT_TRUE(heap.isEmpty()); heap.add(1); heap.add(2); heap.add(3); EXPECT_EQ(heap.getNumNodes(), 3); EXPECT_EQ(heap.getHeight(), 2); for (int i = 4; i <= 15; i++) { heap.add(i); } ASSERT_EQ(heap.getNumNodes(), 15); EXPECT_EQ(heap.getHeight(), 4); int* testArray = new int[15]; for (int i = 0; i < 15; i++) { testArray[i] = heap.peekTop(); heap.remove(); } for (int i = 0; i < 14; i++) { EXPECT_GE(testArray[i], testArray[i + 1]); } heap.clear(); EXPECT_TRUE(heap.isEmpty()); delete[] testArray; }
int main() { Heap hp; hp.positions[1] = -1; hp.sizze = 0; int m,n,a,b,edges; cin>>n>>m; edges = m; while(m--) { cin>>a>>b; if(a!=b) { nodes[a].push_back(make_pair(b,0)); nodes[b].push_back(make_pair(a,1)); } } for(int i=2;i<=n;i++) { distances[i] = MYMAX; hp.add(make_pair(i,MYMAX)); } distances[1] = 0; for(int i=0;i<nodes[1].size();i++) hp.update(nodes[1][i].first,nodes[1][i].second); while(!hp.isEmpty()) { pair<int,int> minEle = hp.removeMin(); distances[minEle.first] = minEle.second; for(int i=0;i<nodes[minEle.first].size();i++) hp.update(nodes[minEle.first][i].first, distances[minEle.first] + nodes[minEle.first][i].second); } if(0<=distances[n] && distances[n]<=edges) cout<<distances[n]; else cout<< -1; return 0; }
void heapTests(Heap &h, int n) { clock_t start, stop; cout << "Adding " << n << " elements..."; cout.flush(); start = clock(); for (int i = 0; i < n; i++) { h.add(rand()%100); } stop = clock(); cout << "done (" << ((double)(stop-start))/CLOCKS_PER_SEC << "s)" << endl; cout << "Removing " << n << " elements..."; cout.flush(); start = clock(); int p = h.remove(); for (int i = 1; i < n; i++) { assert(h.size() == n-i); int q = h.remove(); assert(p <= q); p = q; } stop = clock(); cout << "done (" << ((double)(stop-start))/CLOCKS_PER_SEC << "s)" << endl; }
void testHeap(){ Heap<int,std::string> myHeap; myHeap.add(std::make_pair(5,"5")); myHeap.add(std::make_pair(1,"1")); myHeap.add(std::make_pair(0,"0")); myHeap.add(std::make_pair(7,"7")); myHeap.add(std::make_pair(10,"10")); if(myHeap.getNumItems() != 5){ std::cout << "ERROR: Expected 5 items, but got " << myHeap.getNumItems() << std::endl; exit(1); } else { std::cout << "SUCCESS: Found 5 items, as expected." << std::endl; } std::pair<int,std::string> item; item = myHeap.remove(); if(item.first != 0){ std::cout << "ERROR: Expected key 0, but got " << item.first << std::endl; exit(1); } else { std::cout << "SUCCESS: Found item 0 as expected." << std::endl; } if(myHeap.getNumItems() != 4){ std::cout << "ERROR: Expected 4 items, but got " << myHeap.getNumItems() << std::endl; exit(1); } else { std::cout << "SUCCESS: Found 4 items, as expected." << std::endl; } item = myHeap.remove(); if(item.first != 1){ std::cout << "ERROR: Expected key 1, but got " << item.first << std::endl; exit(1); } else { std::cout << "SUCCESS: Found item 1 as expected." << std::endl; } item = myHeap.remove(); if(item.first != 5){ std::cout << "ERROR: Expected key 5, but got " << item.first << std::endl; exit(1); } else { std::cout << "SUCCESS: Found item 5 as expected." << std::endl; } item = myHeap.remove(); if(item.first != 7){ std::cout << "ERROR: Expected key 7, but got " << item.first << std::endl; exit(1); } else { std::cout << "SUCCESS: Found item 7 as expected." << std::endl; } item = myHeap.remove(); if(item.first != 10){ std::cout << "ERROR: Expected key 10, but got " << item.first << std::endl; exit(1); } else { std::cout << "SUCCESS: Found item 10 as expected." << std::endl; } }
int main() { int tp,qn,iq,n,i,j,cstout=0; float t,totaltime,currentime,pos,timed=0; event temp; customer* tcst=new customer; customer* entered=NULL; cin>>totaltime; currentime=0; cin>>qn; cin>>iq; queue q[qn]; Heap a; for (i=0;i<qn-1;i++) { cin>>q[i].lambda; } for (i=0;i<qn-1;i++) { cin>>tp; q[i].out=tp; for (j=0;j<tp;j++) { cin>>q[i].q[j]; cin>>q[i].pro[j]; if (j>0) q[i].pro[j]=q[i].pro[j]+pos; pos=q[i].pro[j]; } } pos=0; for (i=0;i<iq;i++) { t=currentime+gen_lam(q[i].lambda); a.add(t,i); } temp=a.del(); currentime=temp.t; while (currentime<totaltime) { if (temp.qno<iq) { pos=gen_r(); j=0; while (j<q[temp.qno].out) { if (pos<q[temp.qno].pro[j]) break; j++; } if (j==q[temp.qno].out) j--; tp=q[temp.qno].q[j]; customer* newcst=new customer; newcst->in_time=currentime; if (entered!=NULL) { entered->next=newcst; newcst->pre=entered; } entered=newcst; if (q[tp].pre==0) { q[tp].enqueue(newcst); t=currentime+gen_lam(q[tp].lambda); a.add(t,tp); } else { q[tp].enqueue(newcst); } t=currentime+gen_lam(q[temp.qno].lambda); a.add(t,temp.qno); } else { tcst=q[temp.qno].dequeue(); if (q[temp.qno].pre) { t=currentime+gen_lam(q[temp.qno].lambda); a.add(t,temp.qno); } pos=gen_r(); j=0; while (j<q[temp.qno].out) { if (pos<q[temp.qno].pro[j]) break; j++; } if (j==q[temp.qno].out) j--; tp=q[temp.qno].q[j]; if (tp==qn-1) { if (tcst->pre!=NULL&&tcst->next!=NULL) { tcst->pre->next=tcst->next; tcst->next->pre=tcst->pre; } else if (tcst->pre==NULL&&tcst->next!=NULL) tcst->next->pre=NULL; else if (tcst->pre!=NULL&&tcst->next==NULL) { tcst->pre->next=NULL; entered=tcst->pre; } else entered=NULL; delete tcst; timed=timed+(currentime-tcst->in_time); cstout++; } else { if (q[tp].pre==0) { q[tp].enqueue(tcst); t=currentime+gen_lam(q[tp].lambda); a.add(t,tp); } else { q[tp].enqueue(tcst); } } } temp=a.del(); currentime=temp.t; } cout<<timed/cstout<<endl; return 0; }