summaryrefslogtreecommitdiff
path: root/test/graph/TSP.cpp
blob: 930ec88b41dff079b92c50ce3f135792837f7df8 (plain)
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
#include "../util.h"
struct edge {
	ll dist;
	int to;
};
constexpr ll INF = LL::INF;
#include <graph/TSP.cpp>

vector<int> naive() {
	int n = sz(dist);
	vector<int> todo(n - 1);
	iota(all(todo), 1);
	vector<int> res;
	ll best = LL::INF;
	do {
		int last = 0;
		ll cur = 0;
		for (int x : todo) {
			cur += dist[last][x];
			last = x;
		}
		cur += dist[last][0];
		if (cur < best) {
			best = cur;
			res = todo;
			res.insert(res.begin(), 0);
			res.push_back(0);
		}
	} while (next_permutation(all(todo)));
	return res;
}

void stress_test() {
	ll queries = 0;
	for (ll i = 0; i < 100'000; i++) {
		int n = Random::integer<int>(1, 9);
		dist.assign(n, {});
		for (auto& v : dist) v = Random::integers<ll>(n, 0, 1000'000'000);

		auto expected = naive();
		auto got = TSP();
		
		if (got != expected) cerr << "error" << FAIL;
		queries += n;
	}
	cerr << "tested random queries: " << queries << endl;
}

constexpr int N = 19;
void performance_test() {
	timer t;
	dist.assign(N, {});
	for (auto& v : dist) v = Random::integers<ll>(N, 0, 1000'000'000);
	t.start();
	auto got = TSP();
	t.stop();

	hash_t hash = 0;
	for (int x : got) hash += x;
	if (t.time > 1500) cerr << "too slow: " << t.time << FAIL;
	cerr << "tested performance: " << t.time << "ms (hash: " << hash << ")" << endl;
}

int main() {
	stress_test();
	if (!sanitize) performance_test();
}