HDU 3832 Earth Hour (最短路)

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 (最短路)

时间: 2024-10-27 16:40:44

HDU 3832 Earth Hour (最短路)的相关文章

HDU 3832 Earth Hour (最短路)

Earth Hour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)Total Submission(s): 1518    Accepted Submission(s): 607 Problem Description Earth Hour is an annual international event created by the WWF (World Wide Fun

hdu 3832 Earth Hour

http://acm.hdu.edu.cn/showproblem.php?pid=3832 1 #include <cstdio> 2 #include <iostream> 3 #include <cmath> 4 #include <cstring> 5 #include <vector> 6 #include <algorithm> 7 #include <queue> 8 #define maxn 1001 9

HDU 3832 Earth Hour(最短路)

题目地址:HDU 3832 这个题的这种方法我无法给出证明. 我当时这个灵感出来的时候是想的是要想覆盖的点最少,那就要尽量反复利用这些点,然后要有两个之间是通过还有一个点间接连接的,这样会充分利用那些点. 然后就这样写了一次,一直WA..然后中午睡觉的时候突然想到了有一种情况这样做是不正确的.那就是有个点作为中间点,与三个点相连的情况,这样的情况尽管也符合.可是会有反复边... 可是恰恰相反..反复边应该越多越好. . 那就充分利用了这些点.那么这个点应该怎么找呢?那就直接枚举好了.可是枚举每一

hdu 3832 Earth Hour (最短路变形)

Earth Hour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 1516    Accepted Submission(s): 606 Problem Description Earth Hour is an annual international event created by the WWF (World Wide Fu

HDU 1491 社交网络(最短路计数)

题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1491 题意:给出一个联通的无向图,边有权值.定义I(v)如下,计算每个点的I值. 思路:(1)最简单的就是那个floyd了...g[i][j]记录最短路长度,f[i][j]记录个数,不多说了: (2)下面说说我自己想出来的算法..枚举s和t,计算每个点到s和t的最短路..然后建立最短路树..之后求s到t的最短路个数..然后枚举删掉某个点再求最短路个数,则差值即为通过删掉点的最短路数目.

HDU 4856 Tunnels (最短路+状压DP)

题意:给你N*N的网格,'.'表示可以走,'#'表示不能走,m条管道,每条管道有起点和终点坐标, Bob每次可以走到相邻的网格花费1s,问Bob走完m条管道要花多少时间:Bob在管道内不计算时间 即计算Bob从管道 i 的出口走到管道 j 的入口的时间Dis(e[i],s[j])的最小和,起点可以任意: 思路:看了题解说是状态压缩DP然后深入理解了下. 首先算出d[e[i]][s[j]]的最短距离,不能到达为-1: dp[i][j] : 表示以 j 为起点状态为 i 的最小值.其中 i 是用十进

HDU 2833 WuKong(floyd最短路)

题目地址:HDU 2833 这题想到了最后是通过dis[s][t]==dis[s][i]+dis[i][j]+dis[j][t]的思路来判定是否属于最短路的一条..但是没想到可以用floyd来找最短路中的点数...最短路还是太渣了..好多性质都不会利用.. 这题的思路就是通过floyd对每两个点之间的最短路条数进行计数,然后通过上面的公式(对两条路线均要判定,都符合才说明都可以走),再找最短路中的最大点数. 代码如下: #include <iostream> #include <stdi

hdu 2962 Trucking (二分+最短路Spfa)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1763    Accepted Submission(s): 618 Problem Description A certain local trucking co

hdu 1690 Bus System(最短路)

问题: 链接:点击打开链接 题意: 思路: 代码: #include <iostream> #include <cstdio> #include <cstring> using namespace std; #define INF 1000000000000 typedef __int64 LL; const int N = 110; __int64 dis[N][N],place[N]; __int64 L1,L2,L3,L4,C1,C2,C3,C4; int n,m