Peking University acm2236--wireless network~~ and check Collection

Source: Internet
Author: User

This question, the topic of the approximate meaning is: There are n computers, each other directly can communicate the maximum distance of T, these computers because of the earthquake damage, need to repair. After repair, you can communicate with other computers that have been repaired for less than or equal to T, and you need to answer whether the two computers can communicate.

Simple and check-set application, just add a limit condition.

Enter N and T, and the following n behaves n the coordinates of the computer.

The following is until the end of the file, enter O K, indicating that the K computer for repair. Enter S I j to ask if you can communicate with these two computers.

I use a dis array to save the distance between each of the two computers, which makes it easy to judge later.

The following is the AC code, with detailed comments:

#include <iostream> #include <cmath> #include <cstdio>using namespace std;double dis[1005][1005];                        The distance between each computer and other computers array int par[1005];                       and check set bool vis[1005]; Mark if the computer has been repaired int x[1005], y[1005];int N, T;int finds (int x)//and find function {if (x = = Par[x]) return X;elseret Urn Par[x] = finds (Par[x]);} void join (int x, int y)//And check-set merge function {x = finds (x); y = finds (y); if (x! = y) par[y] = x;} int main () {//freopen ("Data.txt", "R", stdin); int I, J, K, A, B;char str;scanf ("%d%d", &n, &t); for (i = 1; I <= N                             i++)//Enter each computer coordinate {scanf ("%d%d", &x[i], &y[i]);p ar[i] = i;                         Initialize and check set vis[i] = false; Marked as not repaired}getchar (); for (i = 1; I <= N; i++)//calculates the direct distance of each two computers {for (j = 1; J <= I and J + +) {if (i = = j) di S[I][J] = 0.0;elsedis[i][j] = dis[j][i] = sqrt (double ((X[i]-x[j]) + (X[i]-x[j]) * (Y[i]-y[j])));}} WhIle (scanf ("%c", &str)! = EOF) {if (str = = ' o ')//input is O, to be repaired {scanf ("%d", &k); GetChar (); for ( i = 1; I <= N;                i++)////with the repaired computer distance between the judge, determine whether can communicate {if (i! = k && vis[i] && dis[k][i] <= (double) T) {Join (I, k);                     Can be merged}}vis[k] = true;            Marked for repair over}else if (str = = ' S ')//To determine if communication {scanf ("%d%d", &a, &b) is possible, GetChar (), if (finds (a) = = finds (b)) belong to the same and check set, you can printf ("success\n"); elseprintf ("fail\n");}} return 0;}


Peking University acm2236--wireless network~~ and check Collection

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.