不是很理解,之后再看看
/* * this code is made by LinMeiChen * Problem: * Type of Problem: * Thinking: * Feeling: */ #include<algorithm> #include<iostream> #include<string.h> #include<stdio.h> #include<math.h> #include<queue> #include<set> using namespace std; typedef long long lld; typedef unsigned int ud; #define oo 0x3f3f3f3f #define maxn 1010 #define maxm 1100000 struct Edge { int v; int w; int next; }E[maxm]; int head[maxn], tol; int q[maxn], front, rear; int mark[maxn], n; double L; int times[maxn]; struct Node { double x, y; int pos; friend bool operator<(const Node& n1, const Node& n2){ if (n1.x == n2.x) return n1.y < n2.y; return n1.x < n2.x; } }node[maxn]; set<pair<int, int> > index; double getdis(Node n1, Node n2) { return sqrt((n1.x - n2.x)*(n1.x - n2.x) + (n1.y - n2.y)*(n1.y - n2.y)); } void add_edge(int u, int v, int w) { E[tol].v = v; E[tol].w = w; E[tol].next = head[u]; head[u] = tol++; } int Spfa(int s, int t) { for (int i = 0; i <= n + 2; i++) { mark[i] = 0; times[i] = oo; } mark[s] = 1; times[s] = 0; front = rear = 0; q[rear++] = s; while (front < rear) { int u = q[front++]; mark[u] = 0; for (int i = head[u]; i != -1; i = E[i].next) { int v = E[i].v; if (times[u] + E[i].w < times[v]) { times[v] = times[u] + E[i].w; if (!mark[v]) { mark[v] = 1; q[rear++] = v; } } } } if (times[t] >= oo) return -1; return times[t]; } void Init() { memset(head, -1, sizeof head); tol = 0; } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); } bool Ok(int i, int j) { int ty = node[j].y - node[i].y; int tx = node[j].x - node[i].x; int y = ty / gcd(tx, ty); int x = tx / gcd(tx, ty); pair<int,int> temp(x, y); if (index.find(temp) != index.end()) return false; index.insert(temp); return true; } int main() { int T; scanf("%d", &T); while (T--) { Init(); scanf("%d%lf", &n, &L); for (int i = 1; i <= n + 2; i++) { scanf("%lf%lf", &node[i].x, &node[i].y); node[i].pos = i; } sort(node + 1, node + n + 3); for (int i = 1; i <= n + 2; i++) { index.clear(); for (int j = i + 1; j <= n + 2; j++) { double temp = getdis(node[i], node[j]); if (temp <= L&&Ok(i, j)) { add_edge(node[i].pos, node[j].pos, 1); add_edge(node[j].pos, node[i].pos, 1); } } } int ans = Spfa(1, 2); if (ans == -1) printf("impossible\n"); else printf("%d\n", ans - 1); } return 0; }