HDU 3832 Earth Hour

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.