码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3832 Earth Hour (最短路)

时间:2014-08-10 15:50:20      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:des   style   os   io   for   art   ar   cti   

Problem Description
Earth Hour is an annual international event created by the WWF (World Wide Fund for Nature/World Wildlife Fund), held on the last Saturday of March, that asks households and businesses to turn off their non-essential lights and electrical appliances for one hour to raise awareness towards the need to take action on climate change.
To respond to the event of this year, the manager of Hunan University campus decides to turn off some street lights at night. Each street light can be viewed as a point in a plane, which casts flash in a circular area with certain radius.
What‘s more, if two illuminated circles share one intersection or a point, they can be regarded as connected.
Now the manager wants to turn off as many lights as possible, guaranteeing that the illuminated area of the library, the study room and the dormitory are still connected(directly or indirectly). So, at least the lights in these three places will not be turned off.
 


Input
The first line contains a single integer T, which tells you there are T cases followed.
In each case:
The first line is an integer N( 3<=N<=200 ), means there are N street lights at total.
Then there are N lines: each line contain 3 integers, X,Y,R,( 1<=X,Y,R<=1000 ), means the light in position(X,Y) can illuminate a circle area with the radius of R. Note that the 1st of the N lines is corresponding to the library, the 2nd line is corresponding to the study room, and the 3rd line is corresponding to the dorm.
 


Output
One case per line, output the maximal number of lights that can be turned off.
Note that if none of the lights is turned off and the three places are still not connected. Just output -1.
 


Sample Input
3 5 1 1 1 1 4 1 4 1 1 2 2 1 3 3 1 7 1 1 1 4 1 1 2 4 1 1 3 1 3 1 1 3 3 1 4 3 1 6 1 1 1 5 1 1 5 5 1 3 1 2 5 3 2 3 3 1
 


Sample Output
-1 2 1


求出每条可能存在的边权值赋为1,分别以1,2,3为起点进行Dij,然后枚举每个点,以该点作为桥梁 联通1,2,3,当然这些联通的点经过的点越少越好,那么求出min(d1[i]+d2[i]+d3[i]) ,即为总联通的点数减1,再用n-1减去这个值就为可去掉的最大的值。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stack>
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
using namespace std;
typedef long long LL;
const int maxn = 305;
const int MAX = 0x3f3f3f3f;
const int mod = 1000000007;
int t, n, g[maxn][maxn];
int d[4][maxn], vis[maxn];
struct C1 {
    int x, y, r;
}in[maxn];

int getdis(int x1, int y1, int x2, int y2) {
    return  (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) ;
}

void In() {
    scanf("%d", &n);
    for(int i = 0; i < n; i++) scanf("%d%d%d", &in[i].x, &in[i].y, &in[i].r);
    memset(g, MAX, sizeof(g));
    for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) {
        int tmp = getdis(in[i].x, in[i].y, in[j].x, in[j].y);
        if( tmp <= (in[i].r + in[j].r)*(in[i].r + in[j].r) ) g[i][j] = 1;
    }
}

void DIJ(int st) {
    memset(d[st], MAX, sizeof(d[st]));
    memset(vis, 0, sizeof(vis));
    d[st][st] = 0;
    for(int i = 0; i < n; i++) {
        int tmp = MAX, pos;
        for(int j = 0; j < n; j++)
            if(vis[j] == 0 && d[st][j] < tmp) {
                tmp = d[st][j];
                pos = j;
            }
        vis[pos] = 1;
        for(int j = 0; j < n; j++)
            if(!vis[j] && d[st][j] > d[st][pos]+g[pos][j]) d[st][j] = d[st][pos]+g[pos][j];
    }
}

int main()
{
    scanf("%d", &t);
    while(t--) {
        In();
        DIJ(0);
        DIJ(1);
        DIJ(2);
        int ans = MAX;
        for(int i = 0; i < n; i++){
            if(d[0][i] != MAX && d[1][i] != MAX && d[2][i] != MAX)  // 如果没有这句话,三个数加起来可能会爆int
                ans = min(ans, d[0][i] + d[1][i] + d[2][i]);
        }
        if(ans == MAX) printf("-1\n");
        else printf("%d\n", n-1-ans);
    }
    return 0;
}




HDU 3832 Earth Hour (最短路),布布扣,bubuko.com

HDU 3832 Earth Hour (最短路)

标签:des   style   os   io   for   art   ar   cti   

原文地址:http://blog.csdn.net/u013923947/article/details/38469505

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!