现在的位置: 首页 > 综合 > 正文

杭电 3832 多校联合赛 最短路 dijkstra算法

2013年07月09日 ⁄ 综合 ⁄ 共 3127字 ⁄ 字号 评论关闭

       做过这道题后,真的感觉自己太弱了,,,,,,还是做的题太少,看到题后,完全不知道从哪方面下手,,,神奇的这道题,竟然可以转化成最短路,,,。这道题就是找出一个点作为桥梁,让目标三个点到该点的距离只和最小即可。因此可以求出第一个点到各个点的最短路,第二个点到各个点的最短路,第三个点到各个点的最短路,,之后就容易了。。。。。。以后要更加努力了!题目:

Earth Hour

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

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
 

ac代码:

#include <iostream>
#include <string.h>
#include <cstdio>
#include <cmath>
#include <climits>
using namespace std;
const int N=210;
const int MAX=1000010;
int map[N][N],visted[N];
int n;
struct circle{
  double x,y,radius;
}cc[N];
bool check(int xx,int yy){
	double len=sqrt(((cc[xx].x-cc[yy].x)*(cc[xx].x-cc[yy].x)+(cc[xx].y-cc[yy].y)*(cc[xx].y-cc[yy].y)));
	if(len<=cc[xx].radius+cc[yy].radius)
		return true;
	return false;
}
void dijkstra(int x,int dis[]){
  for(int i=0;i<N;++i)
	  dis[i]=MAX;
  memset(visted,0,sizeof(visted));
  dis[x]=0;
  visted[x]=1;
  for(int i=0;i<n;++i){
	  for(int j=0;j<n;++j){
	    if(!visted[j]&&map[x][j]&&dis[x]+map[x][j]<dis[j])
			dis[j]=dis[x]+map[x][j];
	  }
	  int mmin=MAX;
	  for(int k=0;k<n;++k){
	    if(!visted[k]&&dis[k]<mmin)
		{
		  mmin=dis[k];
		  x=k;
		}
	  }
	  visted[x]=1;
  }

}
int main(){
	//freopen("1.txt","r",stdin);
  int ncase,dis1[N],dis2[N],dis3[N];
  scanf("%d",&ncase);
  while(ncase--){
	memset(map,0,sizeof(map));
	memset(dis1,0,sizeof(dis1));
	memset(dis2,0,sizeof(dis2));
	memset(dis3,0,sizeof(dis3));
    scanf("%d",&n);
	for(int i=0;i<n;++i)
		scanf("%lf%lf%lf",&cc[i].x,&cc[i].y,&cc[i].radius);
	for(int i=0;i<n;++i){
		for(int j=i+1;j<n;++j){
		  if(check(i,j))
			  map[i][j]=map[j][i]=1;
		}
	}
	dijkstra(0,dis1);   
	dijkstra(1,dis2);
	dijkstra(2,dis3);
	int mmax=MAX,num=0;
	for(int i=0;i<n;++i){
		if(dis1[i]!=MAX&&dis2[i]!=MAX&&dis3[i]!=MAX){
		  num=dis1[i]+dis2[i]+dis3[i];
		  if(num<mmax)
			  mmax=num;
		}
	}
	if(mmax==MAX)
		printf("-1\n");
	else
		printf("%d\n",n-mmax-1);
  }
  return 0;
}

抱歉!评论已关闭.