Question:
Some circles on the plane must be used to connect the circles on the three points of 1, 2, and 3 with the least circle.
Ideas:
If the two circles can be connected, they are connected by edges and then the problem is transformed into a graph theory model. Then we only need to find the shortest path from 1, 2, 3 to the other points and then enumerate the intermediate points respectively. the distance to these three points is equal
Why is it right to find the intermediate point ?? Although the shortest path of X-> 1 and X-> 2 may pass through Y, if y exists, the minimum value must be provided by Y. Even if X is used to calculate the incorrect answer it won't be the final answer.
Code:
#include<stdio.h>#define N 205#define inf 100000int t, n, ans;int x[N], y[N], r[N], maz[N][N], dis[5][N], vis[N];int min(int ff, int gg) {if (ff < gg)return ff;return gg;}void dij(int s) {int i, j, p, f;for (i = 1; i <= n; i++) {dis[s][i] = inf;vis[i] = 0;}dis[s][s] = 0;for (i = 1; i <= n; i++) {f = inf;for (j = 1; j <= n; j++) {if (!vis[j] && dis[s][j] < f) {f = dis[s][j];p = j;}}vis[p] = 1;for (j = 1; j <= n; j++) {if (!vis[j] && maz[p][j] != inf&& dis[s][j] > dis[s][p] + maz[p][j])dis[s][j] = dis[s][p] + maz[p][j];}}}int main() {int i, j;scanf("%d", &t);while (t--) {scanf("%d", &n);for (i = 1; i <= n; i++)scanf("%d%d%d", &x[i], &y[i], &r[i]);for (i = 1; i <= n; i++) {for (j = 1; j <= n; j++) {if ((x[i] - x[j]) * (x[i] - x[j])+ (y[i] - y[j]) * (y[i] - y[j])<= (r[i] + r[j]) * (r[i] + r[j]))maz[i][j] = 1;elsemaz[i][j] = inf;}maz[i][i] = 0;}for (i = 1; i <= 3; i++)dij(i);ans = inf;for (i = 1; i <= n; i++) {ans = min(ans, dis[1][i] + dis[2][i] + dis[3][i]);}if (ans == inf)printf("-1\n");elseprintf("%d\n", n - ans - 1);}return 0;}
HDU 3832 Earth Hour