题目大意

题目链接

两支球队a, b,每支n人, 如果a球队两个队员之间没有其他人, 那么就可以传球, 问a队1号队员最少传多少次能传到a队n号队员, 输出次数, 传不到输出-1.

分析

思路很简单, 就是判断a球队的队员能不能直接传球,能的话就加一条边长为1的边, 注意如果两个球员之间有本队的球员也是不能直接传球的!!!

代码

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
/*
power by Solo_Dance
*/
#include <bits/stdc++.h>
#define eps 1e-8
using namespace std;
#define ms(a, b) memset((a), (b), sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int N = 1e4 + 5;
const int M = 1e4 + 5;
const int INF = 0x3f3f3f3f;
const ll ll_max = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;

inline ll read() {
ll res = 0;bool f = 0;char ch = getchar();
while (ch < '0' || ch > '9') {if (ch == '-') f = 1;ch = getchar();}
while (ch <= '9' && ch >= '0') {res = (res << 3) + (res << 1) + ch - '0';ch = getchar();}
return f ? (~res + 1) : res;
}

struct edge {
int u, v, w, ne;
} ed[M];

int vis[N], head[N], tim[N], dis[N];
int n, m, cnt;

void add(int u, int v, int w) {
ed[cnt] = {u, v, w, head[u]};
head[u] = cnt++;
}

void init() {
memset(vis, 0, sizeof vis);
memset(tim, 0, sizeof tim);
memset(head, -1, sizeof head);
memset(dis, INF, sizeof dis);
cnt = 0;
}
void dijk(int s) {
priority_queue<P, vector<P>, greater<P> > q;
dis[s] = 0;
q.emplace(0, s);
while (!q.empty()) {
int u = q.top().second; q.pop();
if (vis[u]) continue;
vis[u] = 1;
for (int i = head[u]; ~i; i = ed[i].ne) {
int v = ed[i].v, w = ed[i].w;
if (dis[v] > dis[u] + w)
dis[v] = dis[u] + w, q.emplace(dis[v], v);
}
}
}
struct point{
int x, y;
}a[N], b[N];
bool isLine(point x, point y, point z){
if (z.x < min(x.x, y.x) || z.x > max(x.x, y.x) || z.y < min(x.y, y.y) || z.y > max(x.y, y.y))
return false;
return (x.y - y.y) * (x.x - z.x) == (x.y - z.y) * (x.x - y.x);
}
bool is(int x, int y){
for (int i = 0; i < n; ++i)
if (isLine(a[x], a[y], b[i])) return false;
// 特别要注意判断如果中间有本队球员也是不能传球的!
for (int i = 0; i < n; ++i){
if (i == x || y == i) continue;
if (isLine(a[x], a[y], a[i])) return false;
}
return true;
}
int main(){
n = read();
init();
for (int i = 0; i < n; ++i) a[i].x = read(), a[i].y = read();
for (int i = 0; i < n; ++i) b[i].x = read(), b[i].y = read();
for (int i = 0; i < n; ++i){
for (int j = 0; j < n; ++j){
if (i == j) continue;
if (is(i, j)) add(i, j, 1);
}
}
dijk(0);
if (dis[n - 1] >= INF) dis[n - 1] = -1;
cout << dis[n - 1] << "\n";
return 0;
}
恰似你一低头的温柔,较弱水莲花不胜寒风的娇羞, 我的心为你悸动不休。  --mingfuyan

千万不要图快——如果没有足够的时间用来实践, 那么学得快, 忘得也快。