-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcpputils.hpp
113 lines (98 loc) · 2.34 KB
/
cpputils.hpp
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
#ifndef CPP_UTILS
#define CPP_UTILS
#include <bits/stdc++.h>
const int N = 502, M = 52;
int num_districts = 0, num_labs = 0;
struct Lab
{
int id;
double lat, lon;
int district, type, capacity, backlog;
int initcapacity;
double demand;
};
struct District
{
int id;
std::string name;
double lat, lon;
int samples;
int total_capacity;
int extraslots;
};
Lab labz[N];
District districts[M];
std::string get_parts(std::string &line, int &i)
{
std::string word;
while (i < line.length())
{
if (line[i] == ',')
{
i++;
break;
}
else
word += line[i];
i++;
}
return word;
}
void get_lab(std::string &line, Lab &lab)
{
int i = 0;
lab.id = stoi(get_parts(line, i));
lab.id = stoi(get_parts(line, i));
lab.lat = stod(get_parts(line, i));
lab.lon = stod(get_parts(line, i));
lab.district = stoi(get_parts(line, i));
lab.type = stoi(get_parts(line, i));
lab.initcapacity = lab.capacity = stoi(get_parts(line, i));
lab.backlog = stoi(get_parts(line, i));
districts[lab.district].total_capacity += lab.capacity;
}
void get_district(std::string &line, District &d)
{
int i = 0;
d.id = stoi(get_parts(line, i));
d.id = stoi(get_parts(line, i));
d.name = get_parts(line, i);
d.lat = stod(get_parts(line, i));
d.lon = stod(get_parts(line, i));
d.samples = stoi(get_parts(line, i));
d.total_capacity = 0;
}
void getinput(char *distpath, char *labpath)
{
std::ifstream distin, labin;
distin.open(distpath);
labin.open(labpath);
std::string line;
getline(distin, line);
while (getline(distin, line))
{
get_district(line, districts[++num_districts]);
}
getline(labin, line);
while (getline(labin, line))
{
get_lab(line, labz[++num_labs]);
}
}
const double kmR = 6373.0;
double toRadians(double degree)
{
return (degree * M_PI) / 180;
}
double calcdist(double lat1d, double lon1d, double lat2d, double lon2d)
{
double lat1r, lon1r, lat2r, lon2r, u, v;
lat1r = toRadians(lat1d);
lon1r = toRadians(lon1d);
lat2r = toRadians(lat2d);
lon2r = toRadians(lon2d);
u = sin((lat2r - lat1r) / 2);
v = sin((lon2r - lon1r) / 2);
return 2.0 * kmR * asin(sqrt(u * u + cos(lat1r) * cos(lat2r) * v * v));
}
#endif