-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
147 lines (130 loc) · 2.53 KB
/
main.cpp
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
// #include <malloc.h>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include "node.h"
#include "Problem.h"
#include "Search.h"
#define max 10000
using namespace std;
float tim;
int nor;
int nob;
int noc;
struct node tob[max]; // total no. of bids
bool com[max],reg[max]; //keeps record of which companies and regions can be selected in the remaining unprocessed bids
bool bid[max]; // final bids
//function to take input - read from console by redirection
void readFile()
{
string g;
scanf("%f\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);
}
}
void fill(int);
bool checkReg(int);
void getRandom() //modify this function to produce the best output(following the conditions mentioned 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);
}
for(i=0;i<nob;i++)
{
if(bid[i]) {
cout<<i<<" ";
}
}
cout<<"#"<<endl;
}
//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;
}
}
//helper function of getRandom function
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()
{
readFile();
// getRandom();
cout<<"Initializing problem"<<endl;
Problem *problem = new Problem(tob, nob, noc, nor);
int* maxBids = problem->getMaxBids();
cout<<"Number of bids by companies are: "<<endl;
for (int i = 0; i < noc; ++i) {
cout<<i<<" : "<<maxBids[i]<<endl;
}
cout<<"Starting search"<<endl;
Search::localGreedySearch(problem, 45);
return 0;
}