-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.1cost.c
executable file
·216 lines (187 loc) · 5.9 KB
/
4.1cost.c
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//FOUR WHEELER(.1) clustering algorithm with PROVIDER*ACCEPTOR matrix
#include<stdio.h>
#include<stdlib.h>
#define P 50 //max providers
#define A 50 //max acceptors
#define FUEL 54.0 //cost per unit of fuel
void input(void);
void output(void);
void getmin(int* mrptr, int* mcptr);
int makehigh(int r, int c);
void printmatrix(void);
void getnewdist(void);
void costcalc(void);
float pa[P][A]; //distance table(from DB)
int p,a; //(from DB)
int poola[A]; //-1 not pooled; number if pooled [!!poolp[P] NOT USED!!]
int poolp[P]; //-1 - not pooled; 1 - pooled
int poolpmax[P]; //max capacity of the vehicle, not counting the provider(from DB)
int Cpoolpmax[P]; //copy of poolpmax
int pm[P]; //mileage for all the provider vehicles(from DB)
float distp[P]; //distance travelled by the provider if he was travelling alone(using API)
float distpa[P]; //total distance travelled by the provider when pooling(using API)
float cost[P]; //money collected by the provider
int main()
{
int i,j,x;
int minr,minc; //to hold the row and column number of the minimum distance
input();
i=0;
j=0;
while(i<p && j<a)
{
//find min dist among the ungrouped using getmin
getmin(&minr,&minc);
//group the two selected
poola[minc]=minr;
poolpmax[minr]--;
poolp[minr]=1; //mark as pooled for costcalc
//use makehigh on the acceptor to exclude him, and also on provider if he doesnt have any room
x=makehigh(minr,minc);
//incr i and j to keep a count of the grouped providers and acceptors
i=i+x;
j++;
//print the present state of matrix to verify
//printmatrix();
}
//fill the distpa table for the pooling just concluded
getnewdist();
//calculate the cost
costcalc();
output();
return 0;
}
void input()
{
int i,j;
printf("Enter the number of providers: ");
scanf("%d",&p);
printf("Enter the number of acceptors: ");
scanf("%d",&a);
//printf("Enter the P*A distance table:\n");
for(i=0;i<p;i++)
{
poolp[i]=(-1);
for(j=0;j<a;j++)
{
//scanf("%f",&pa[i][j]);
pa[i][j]=(rand()%20)+1;
}
}
for(j=0;j<a;j++) //nobody is pooled
poola[j]=(-1);
//printf("Enter the max capacity table:\n");
for(i=0;i<p;i++)
{
//scanf("%d",&poolpmax[i]);
poolpmax[i]=(rand()%3)+1; //not counting the provider
Cpoolpmax[i]=poolpmax[i]; //poolpmax will be destroyed, so keep a copy
}
//printf("Enter the mileage table:\n")
for(i=0;i<p;i++)
{
//scanf("%d",&pm[i]);
pm[i]=16-(rand()%6); //typical 4 wheeler mileage of 11 to 16
}
//printf("Enter the distance travelled by the provider if he was travelling alone:\n");
for(i=0;i<p;i++)
{
//scanf("%d",&pm[i]);
distp[i]=(float)(((rand()%100)+1)/10); //0.1 to 10.0 [this is senseless]
printf("%f\n",distp[i]);
}
}
void output()
{
int i;
printf("------------RESULT------------\n");
for(i=0;i<a;i++)
{
printf("%d is taking a ride from %d and paying him %f Rs\n",i,poola[i],cost[poola[i]]);
}
}
void getmin(int* mrptr, int* mcptr)
{
int i,j,flag=1;
float min;
for(i=0;i<p;i++)
{
for(j=0;j<a;j++)
{
if(flag) //to find the first valid min distance
{
if(pa[i][j]!=999999)
{
min=pa[i][j];
*mrptr=i;
*mcptr=j;
flag=0;
}
}
else //to better the present min distance
{
if(pa[i][j]<min)
{
min=pa[i][j];
*mrptr=i;
*mcptr=j;
}
}
}
}
}
int makehigh(int r, int c)
{
int i;
for(i=0;i<p;i++)
pa[i][c]=999999;
if(poolpmax[r]==0) //makehigh only if there is no room for another person
{
for(i=0;i<a;i++)
pa[r][i]=999999;
return 1; //mark that a provider is full
}
return 0; //mark that the provider is not yet full
}
void printmatrix()
{
int i,j;
printf("Intermediate state of the distance table:\n");
for(i=0;i<p;i++)
{
for(j=0;j<a;j++)
{
printf("%15.1f",pa[i][j]);
}
printf("\n");
}
}
void getnewdist()
{
int i;
//printf("Enter the total distance travelled by the provider when pooling:\n");
for(i=0;i<p;i++)
{
if(poolp[i]!=(-1))
{
//scanf("%d",&pm[i]);
distpa[i]=distp[i]+(float)((((rand()%20)+1)/10)*Cpoolpmax[i]); //add 0.1 to 2.0 [this is senseless]
printf("%f\n",distpa[i]);
}
}
}
void costcalc()
{
int i;
for(i=0;i<p;i++)
{
if(poolp[i]!=(-1)) //if provider is pooled
{
cost[i]=((distpa[i]-(distp[i]/(Cpoolpmax[i]+1)))*((FUEL)/pm[i]))/Cpoolpmax[i];
printf("%d %f %f %d %d %f\n",i,distp[i],distpa[i],Cpoolpmax[i],pm[i],FUEL/pm[i]);
}
}
}
//can have a function to link all the acceptors to the providers using dynamic allocation
//for the number of acceptors linked to the provider
//this will be helpful during the distance retrival from GMaps API