hdu 3832(最短路拆点)

Earth Hour

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)


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的顶点连通。这道题让我想起了之前做的hdu 2833,它是找两条最短路之间有多少个重复的点。如何将它应用到本道题上呢?我的想法是在1,2,3三个顶点之间找两条路,假定为1->2和1->3,这样我们就可以找1->2和1->3这两条最短路的重复点,这个是需要我们保留的点,即我们需要尽量多的重复点来使得1,2,3都依靠这些点进行连通。由于只有1,2,3三个顶点,所以只需要枚举三次即可。这个算法的核心是依赖于Floyd算法,这里的数据量似乎行不通,TLE啦。。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn = 205;
const int inf = 0x3f3f3f3f;
struct Node
{
	int x,y,r;
}light[maxn];
int n,ans,map[maxn][maxn],dp[maxn][maxn];

int dist(Node a,Node b)
{
	return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}

void floyd()
{
	for(int k = 1; k <= n; k++)
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= n; j++)
			{
				if(map[i][j] > map[i][k] + map[k][j])
				{
					map[i][j] = map[i][k] + map[k][j];
					dp[i][j] = dp[i][k] + dp[k][j] - 1;
				}
				else if(map[i][j] == map[i][k] + map[k][j] && dp[i][j] < dp[i][k] + dp[k][j])
					dp[i][j] = dp[i][k] + dp[k][j] - 1;
			}
}

void solve(int s1,int e1,int s2,int e2)
{
	int res = 0;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++)
			if(map[s1][i] + map[i][j] + map[j][e1] == map[s1][e1] && map[s2][i] + map[i][j] + map[j][e2] == map[s2][e2])
				res = max(res,dp[i][j]);
	ans = max(ans,n - (map[s1][e1] + map[s2][e2] + 2 - res));
}

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i = 1; i <= n; i++)
			scanf("%d%d%d",&light[i].x,&light[i].y,&light[i].r);
		for(int i = 1; i <= n; i++)
		{
			for(int j = 1; j <= n; j++)
			{
				map[i][j] = inf;
				dp[i][j] = 2;
			}
			map[i][i] = 0;
			dp[i][i] = 1;
		}
		for(int i = 1; i < n; i++)
			for(int j = i + 1; j <= n; j++)
			{
				if(dist(light[i],light[j]) <= (light[i].r + light[j].r) * (light[i].r + light[j].r))
					map[i][j] = map[j][i] = 1;
			}
		floyd();
		ans = 0;
		solve(1,2,1,3);
		solve(2,1,2,3);
		solve(3,1,3,2);
		if(ans == 0) ans = -1;
		printf("%d\n",ans);
	}
	return 0;
}

参考了别人的博客,大多数都是分别以1,2,3为起点,去找最短路,接下来就是枚举顶点,使得该顶点为连接1,2,3的中心点,即1->i、2->i、3->i,三条路之间没有公共点。最开始在想这样会算重复,但仔细想想是不会的。因为只要1,2,3是连通的,就一定可以找到这样的i。做完这题给我的感觉就是:有些图已经是连通的了,那么它的结构形态似乎不那么重要了,可以将其重构成我们需要的样子。

这题的代码就不打了,参考一下别人的吧:http://blog.csdn.net/juststeps/article/details/8770123

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值