Sie sind auf Seite 1von 7

#include <iostream> #include <string> #include <string.

h> #include <cstdlib> #include<cstdio> #include <ctime> #include <limits> #include <vector> #include <list> #define max 10000 using namespace std; struct node // bid node structure { int cid; //company double price; int norc; // no. of regions in one bid int region[max]; }; double tim; int nor; int nob; int noc; struct node tob[max]; // total no. of bids int h[max]; int bidcor[max]; int bidval[max]; bool com[max],reg[max],nextst[max]; //keeps record of which companies and region s can be selected in the remaining unprocessed bids bool bid[max]; // final bids bool bidfin[max]; //function to take input - read from console by redirection void readfile() { string g; scanf("%lf\n\n",&tim); scanf("%d\n\n",&nor); scanf("%d\n\n",&nob); scanf("%d\n\n",&noc); for(int i=0;i<nob;i++) { cout<<flush; string ch; getline(cin,ch); int t=0;int j=0; char ch1[max]; while(ch[t]!=' ') { ch1[j]=ch[t]; j++;t++; }

ch1[j]='\0'; tob[i].cid=atoi(ch1); ch1[0]='\0';j=0;t++; while(ch[t]!=' ') { ch1[j]=ch[t]; j++;t++; } ch1[j]='\0'; tob[i].price=strtod (ch1, NULL); t++; int x=0; int w=t; while(ch[t]!='#') { if(ch[t]==' ') { x++;} t++; } tob[i].norc=x; t=w; for(int qq=0;qq<x;qq++) { ch1[0]='\0';j=0; while(ch[t]!=' ') { ch1[j]=ch[t]; j++;t++; } t++; ch1[j]='\0'; tob[i].region[qq]=atoi(ch1); } getline(cin,g); } } bool checknext(); void fill(int); double hillclimbing(); void clashing(); void fillnextstates(int); void nonclashing(); void nonclashhelper(int); bool checkReg(int); void getrandom() //modify this function to produce the best output(following the conditions menti oned in the assignment) { int num1,i; num1=rand()%nob; fill(num1); for(i=(num1+1)%nob;i!=num1;i=(i+1)%nob) { if(com[tob[i].cid] || checkReg(i)) continue; fill(i); }

} double hillclimbing() { int num1,i; num1=rand()%nob; fillnextstates(num1); clashing(); while(checknext()) { //pick the best next bid from h filled by clashing int hval=0; int bidno; for (int i=0; i<nob; i++) { if(h[i]>hval) { hval=h[i]; bidno=i; } } fillnextstates(bidno); clashing(); } // getrandom(); /* while(true) { nonclashing(); int bval=0; int corno=-1; for (int i=0; i<nob; i++) { if(bidval[i]>bval) { bval=bidval[i]; corno=i; } } if(corno==-1) break; else { bid[bidcor[corno]]=false; bid[corno]=true; } }*/ double sum=0.0; // std::vector<int> po; for(i=0;i<nob;i++) { if(bid[i]) { // cout<<i<<' '; sum+=tob[i].price; // for(int j=0;j<tob[i].norc;j++) // { // cout<<tob[i].region[j]<<" "; // po.push_back(tob[i].region[j]); // }

// //

cout<<"# "<<tob[i].cid; cout<<"# "<<tob[i].price;cout<<endl;

} } // cout<<"#"<<endl; // std::sort(po.begin(),po.end()); //for(int y=0;y<po.size();y++) // cout<<po.at(y)<<","; // cout<<"#"<<endl; //cout<<sum<<endl; return sum; } bool checknext() { for(int i=0;i<nob;i++) { if (nextst[i]!=true) { return true; } } return false; } void clashing() { std::fill_n(h, max, 0); for (int i=0; i<nob; i++) { if (nextst[i]==true) { continue; } int temp=0; for (int j=0; j<nob; j++) { if (nextst[j]==true) { continue; } if(i!=j) { for (int k=0; k<tob[i].norc; k++) { for (int l=0; l<tob[j].norc; l++) { if(tob[i].region[k]==tob[j].region[l]) { temp++; } } } }

} if(temp==0) h[i]=INT_MAX; else h[i]=tob[i].price/temp; } } void nonclashing() { std::fill_n(bidcor, max, INT_MAX); std::fill_n(bidval, max, INT_MIN); for (int i=0; i<nob; i++) { if(bid[i]) { nonclashhelper(i); } } } void nonclashhelper(int chkr) { for (int l=0; l<nob; l++) { int tflag=0; if(!bid[l]) { for (int k=0; k<nob; k++) { if(bid[k] && k!=chkr) { for (int m=0; m<tob[l].norc; m++) { for (int n=0; n<tob[k].norc; n++) { if(tob[k].region[n]==tob[l].region[m]) { tflag=1; goto label; } } } } } label: if(tflag==0 && (tob[l].price-tob[chkr].price)>bidval[l]) { bidcor[l]=chkr; bidval[l]=tob[l].price-tob[chkr].price; } } }

} void fillnextstates( int bidno) { fill(bidno); for (int i=0; i<nob; i++) { if (tob[i].cid==tob[bidno].cid) { nextst[i]=true;//true denotes that the company id is same or there i s a clash continue; } for(int j=0;j<tob[i].norc;j++) { if(reg[tob[i].region[j]]==true) { nextst[i]=true; break; } } } } //helper function of getrandom function void fill(int bidno) { com[tob[bidno].cid]=true; bid[bidno]=true; for(int i=0;i<tob[bidno].norc;i++) { reg[tob[bidno].region[i]]=true; } } bool checkReg(int bidno) { for(int i=0;i<tob[bidno].norc;i++) { if(reg[tob[bidno].region[i]]==true) return true; } return false; } int main() { clock_t t1,t2; t1=clock(); // cout<<t1<<endl; srand(time(0)); readfile(); double x=0.0; tim=tim*60.0-1.0; // cout<<tim<<endl; for (; ;) { std::fill_n(com, max, false);

std::fill_n(reg, max, false); std::fill_n(nextst, max, false); std::fill_n(bid, max, false); double temp=hillclimbing(); if(temp>x) {x=temp; for(int l=0;l<nob;l++) bidfin[l]=bid[l]; } t2=clock(); double diff ((double)t2-(double)t1); // cout<<diff<<endl; if(diff/1000000>tim/20) break; } // cout<<x<<endl; int i; for(i=0;i<nob;i++) { if(bidfin[i]) { cout<<i<<' '; } } cout<<"#"<<endl; return 0; }