hdu 3832 几何构图最短路

Earth Hour

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)
Total Submission(s): 1165    Accepted Submission(s): 472


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,2,3三点做最短路,线性扫描一遍得出答案。

代码:

 

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<map>
#include<list>
#include<set>
#include<queue>
#include<stack>
using namespace std;
const int inf=1000000;
const int maxn=3010;
int dist[maxn],head[maxn],tol,m,n,id,pre[maxn],dis[5][maxn];
struct Edge
{
        int st,to,next,val;
}edge[100*maxn];
void add(int u,int v,int w)
{
        edge[tol].st=u;
        edge[tol].to=v;
        edge[tol].next=head[u];
        edge[tol].val=w;
        head[u]=tol++;
}
struct Node
{
        int id,dist;
        Node(int a=0,int b=0):id(a),dist(b){}
        bool operator < (const Node &b) const
        {
                return dist>b.dist;
        }
};
struct PP
{
       int x,y,r;
}pp[50*maxn];
bool judge(PP a,PP b)
{
        if((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)<=(a.r+b.r)*(a.r+b.r))return 1;
        return 0;
}
void fun(int st)
{
        int i,j,u,v;
        priority_queue<Node> q;
        q.push(Node(st,0));
        while(!q.empty())
        {
                Node ret=q.top();q.pop();
                u=ret.id;
                if(dist[u]<ret.dist)continue;
                int flag=1;
                for(i=head[u];i!=-1;i=edge[i].next)
                {
                        v=edge[i].to;
                        if(dist[v]>dist[u]+edge[i].val)
                        {
                                pre[v]=u;
                                dist[v]=dist[u]+edge[i].val;
                                q.push(Node(v,dist[v]));
                        }
                }
        }
}
int main()
{
        int i,j,k,T;
        scanf("%d",&T);
        while(T--)
        {
                scanf("%d",&n);
                for(i=1;i<=n;i++)scanf("%d%d%d",&pp[i].x,&pp[i].y,&pp[i].r);
                memset(head,-1,sizeof(head));tol=0;
                for(i=1;i<=n;i++)
                for(j=i+1;j<=n;j++)
                if(judge(pp[i],pp[j]))add(i,j,1),add(j,i,1);
                int ans=2147483647;
                for(i=1;i<=3;i++)
                {
                        for(j=1;j<=n;j++)dist[j]=inf;dist[i]=0;
                        fun(i);
                        for(j=1;j<=n;j++)dis[i][j]=dist[j];
                }
                for(i=1;i<=n;i++)
                {
                        j=dis[1][i]+dis[3][i]+dis[2][i];
                        if(ans>j)ans=j;
                }
                if(ans>=inf){puts("-1");continue;}
                ans=n-ans-1;printf("%d\n",ans);
        }
        return 0;
}

 

posted @ 2013-09-16 19:09  线性无关  阅读(121)  评论(0编辑  收藏  举报