-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
186 lines (155 loc) · 4.47 KB
/
Main.cpp
File metadata and controls
186 lines (155 loc) · 4.47 KB
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
#include <iostream>
#include <string>
#include <chrono>
#include <fstream>
#include "PCenter.h"
using namespace std;
using namespace std::chrono;
using namespace szx;
static const int ObjTable[15] = { 0, 108039, 50812, 32694, 24102, 18847, 9076, 5738, 4153, 3154, 2774, 2295, 1945, 1604, 1449 };
int find(int n) {
int i;
if (n <= 50) {
i = n / 10;
}
else {
i = n / 50 + 4;
}
return ObjTable[i];
}
void loadInput(istream& is, PCenter& pc) {
ios::sync_with_stdio(false); // 禁用同步
cin.tie(0);
cout.tie(0);
is >> pc.nodeNum >> pc.centerNum;
// 直接构造具有所需大小的向量
pc.coverages = vector<vector<NodeId>>(pc.nodeNum);
pc.coveredNodeNums = vector<NodeId>(pc.nodeNum);
pc.fixNodes.Nodes = vector<bool>(pc.nodeNum, false);
int t = 0;
for (int i = 0; i < pc.nodeNum; i++) {
NodeId coveredNodeNum;
is >> coveredNodeNum;
pc.coveredNodeNums[i] = coveredNodeNum;
pc.coverages[i] = vector<NodeId>(coveredNodeNum);
for (int j = 0; j < coveredNodeNum; j++) {
is >> pc.coverages[i][j];
}
if (coveredNodeNum == 1 && pc.coverages[i][0] == i) {
pc.fixNodes.Nodes[i] = true;
t++;
}
}
pc.fixNodes.Num = t;
EdgeId minEdgeLenRank;
EdgeId maxEdgeLenRank;
is >> maxEdgeLenRank >> minEdgeLenRank;
if (maxEdgeLenRank == 0) {
pc.obj = -1;
return;
}
int obj = find(pc.centerNum);
pc.L = minEdgeLenRank;
pc.U = maxEdgeLenRank;
pc.obj = obj;
pc.nodesWithDrops = vector<vector<NodeId>>(maxEdgeLenRank - minEdgeLenRank);
for (auto& r : pc.nodesWithDrops) {
NodeId nodeNumToDrop;
is >> nodeNumToDrop;
r = vector<NodeId>(nodeNumToDrop);
--maxEdgeLenRank;
for (auto& node : r) {
is >> node;
// 缩小半径到最小半径
if (maxEdgeLenRank >= obj) {
//cerr << maxEdgeLenRank << endl;
pc.coverages[node].pop_back();
pc.coveredNodeNums[node]--;
if (pc.coveredNodeNums[node] == 0) {
pc.fixNodes.Nodes[node] = true;
++pc.fixNodes.Num;
}
}
}
}
cerr << maxEdgeLenRank << endl;
}
void saveOutput(ostream& os, Centers& centers) {
for (auto center = centers.begin(); center != centers.end(); ++center) { os << *center << endl; }
}
void test(istream& inputStream, ostream& outputStream, long long secTimeout, int randSeed) {
cerr << "load input." << endl;
PCenter pc;
loadInput(inputStream, pc);
vector<Nodes> coverages;
int nodeNum;
//coverages = pc.coverages;
//nodeNum = pc.nodeNum;
cerr << "solve." << endl;
steady_clock::time_point start = steady_clock::now();
steady_clock::time_point endTime = steady_clock::now() + seconds(secTimeout);
Centers centers(pc.centerNum);
solvePCenter(centers, pc, [&]() { return duration_cast<milliseconds>(endTime - steady_clock::now()).count(); }, randSeed);
steady_clock::time_point end = steady_clock::now();
cerr << "save output." << endl;
saveOutput(outputStream, centers);
// DEBUG
// // 输出执行时间
// auto duration = duration_cast<milliseconds>(end - start);
// cerr << "Execution time: " << duration.count() << "ms" << endl;
// // 输出解
// cerr << "Solution centers: " << endl;
// for (NodeId i : centers) {
// cerr << i << ' ';
// }
// cerr << endl;
// // 输出未覆盖的节点
// cerr << "Uncovered centers: " << endl;
// vector<bool> flag(pc.nodeNum, 0);
// for (NodeId i : centers)
// for (NodeId j : pc.coverages[i])
// flag[j] = true;
// int tot = 0;
// for (NodeId i = 0; i < pc.nodeNum; i++) {
// if (!flag[i]) {
// cerr << i << endl;
// tot++;
// }
// }
// cerr << "uncovered center num: " << tot << endl;
}
void test(istream& inputStream, ostream& outputStream, long long secTimeout) {
return test(inputStream, outputStream, secTimeout, static_cast<int>(time(nullptr) + clock()));
}
void check(istream& ansStream, istream& refStream) {
vector<int> ans, ref;
int t;
while (ansStream >> t)
ans.push_back(t);
while (refStream >> t)
ref.push_back(t);
for (int i : ref) {
if (find(ans.begin(), ans.end(), i) == ans.end()) {
cerr << "Error: " << i << " not found in answer." << endl;
}
}
};
int main(int argc, char* argv[]) {
cerr << "load environment." << endl;
if (argc > 2) {
long long secTimeout = atoll(argv[1]);
int randSeed = atoi(argv[2]);
test(cin, cout, secTimeout, randSeed);
}
else {
ifstream ifs("instance/pcb3038p200r141.txt");
ofstream ofs("instance/solution.txt");
test(ifs, ofs, 30000); // for self-test.
ifs.close();
ofs.close();
//ifstream ref("instance/input1_ref.txt");
//ifstream ans("instance/solution.txt");
//check(ans, ref);
}
return 0;
}