hdu 3832 Earth Hour(最短路 spfa dijkstra)

Status
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,
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

题意 :

有n个点 每个点都有一个能够照射到的半径 求在保证1 2 3 点能够被照射到的前提下 最多能关掉几个点

思路:

将求点转化为求两点间的线段 

先用dijkstra或spfa将三个点到各个点的最短路求出 (这里求的是线段数目 )

再枚举每个点到 1 2 3点的最短线段数(该点到1 2 3 点如果有线段重复 那必然不是最短)

最后得出结果 n-(1+minn)

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#define mem(a,b) memset(a,b,sizeof(a))
#define INF 1000000
using namespace std;
struct Point
{
    int x,y,r;
};
Point po[230];
int road[230][230];
int d[5][230];
int vis[230],first[230],next[230];
int n;
void dijkstra(int a)
{
    int i,j,y;
    mem(vis,0);
    for(j=1;j<=n;j++)
    {
        d[a][j]=INF;
    }
    d[a][a]=0;
    for(i=1;i<=n;i++)
    {
        int x,m=INF;
      for(y=1;y<=n;y++) if(!vis[y]&&d[a][y]<m) m=d[a][x=y];
        vis[x]=1;
      for(y=1;y<=n;y++)
        if(d[a][y]>d[a][x]+road[x][y])
            d[a][y]=d[a][x]+road[x][y];
    }
}

void spfa(int a)
{
    queue<int> q;
    bool inq[300];
    for(int i=0;i<=n;i++) d[a][i]=(i==a?0:INF);
    memset(inq,0,sizeof(inq));
    inq[a]=true;
    q.push(a);
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        inq[x]=false;
        for(int i=1;i<=n;i++)
        {
            if(d[a][i]>d[a][x]+road[x][i])
            {
                d[a][i]=d[a][x]+road[x][i];
              if(!inq[i])
              {
                inq[i]=true;
                q.push(i);
              }
            }

        }
    }
}
int main()
{
    int i,j;
    int t;
    cin>>t;
    while(t--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
            scanf("%d%d%d",&po[i].x,&po[i].y,&po[i].r);

        for(i=1;i<=n;i++)
            for(j=i+1;j<=n;j++)
            {
                if((po[i].x-po[j].x)*(po[i].x-po[j].x)+(po[i].y-po[j].y)*(po[i].y-po[j].y)<=(po[i].r+po[j].r)*(po[i].r+po[j].r))
                    road[j][i]=road[i][j]=1;
                else road[j][i]=road[i][j]=INF;
            }
        //for(i=1;i<=3;i++) dijkstra(i);
        for(i=1;i<=3;i++) spfa(i);
        int minn=INF;
        for(i=1;i<=n;i++)
        {
            if(d[1][i]+d[2][i]+d[3][i]<minn)
                minn=d[1][i]+d[2][i]+d[3][i];
        }
        if(minn==INF)
            cout<<"-1"<<endl;
        else
            cout<<n-minn-1<<endl;
    }
    return 0;
}

  

  

原文地址:https://www.cnblogs.com/sola1994/p/3906104.html