-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathg-trip.cpp
102 lines (85 loc) · 1.94 KB
/
g-trip.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
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <unordered_set>
#include <cstdint>
using namespace std;
struct town
{
long int x = 0, y = 0;
std::vector<uint16_t> v;
};
int tcount = 0;
std::vector<town> tt;
long int max_len = 0;
uint16_t tstart = 0, tend = 0;
int result = -1;
long int distance(uint16_t i, uint16_t j)
{
long int d = std::abs(tt[i].x - tt[j].x);
d += std::abs(tt[i].y - tt[j].y);
return d;
}
int main()
{
cin >> tcount;
tt.resize(tcount);
for(int i = 0; i < tcount; ++i)
{
town& t = tt[i];
cin >> t.x >> t.y;
}
cin >> max_len;
cin >> tstart >> tend;
for(uint16_t i = 0; i < tcount; ++i)
for(uint16_t j = i + 1; j < tcount; ++j)
{
if(i == j)
continue;
long int d = distance(i, j);
if(d > max_len)
continue;
tt[i].v.push_back(j);
tt[j].v.push_back(i);
}
vector<bool> mark(tcount, false);
size_t deep = 0;
unordered_set<uint16_t> ring;
ring.insert(tstart - 1);
while(!ring.empty())
{
bool finish = false;
for(uint16_t v : ring)
{
if(v == tend - 1)
{
finish = true;
result = deep;
break;
}
mark[v] = true;
}
if(finish)
break;
unordered_set<uint16_t> rr;
for(uint16_t v : ring)
{
const town& t = tt[v];
for(uint16_t r : t.v)
if(!mark[r])
rr.insert(r);
}
++deep;
swap(ring, rr);
}
cout << result << endl;
/*for(int i = 0; i < tcount; ++i)
cout << tx[i] << " ";
cout << endl;*/
/*for(auto& t : tt)
cout << t.x << " " << t.y << endl;
cout << max_len << endl;
cout << start << " " << end << endl;*/
return 0;
}