1. 程式人生 > >hdu 3832(最短路拆點)

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。做完這題給我的感覺就是:有些圖已經是連通的了,那麼它的結構形態似乎不那麼重要了,可以將其重構成我們需要的樣子。

相關推薦

hdu 3832短路

Earth Hour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Problem Description Earth Hour is an a

一個人的旅行 HDU - 2066 短路

mis 一個人 ssi 輸出 int cstring col 個數 cto 一個人的旅行 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su

HDU-1548 A strange lift短路[Spfa || BFS]

題目連結: http://acm.hdu.edu.cn/showproblem.php?pid=1548 參考部落格: https://blog.csdn.net/idealism_xxm/article/details/47625691 BFS: //BFS #include

2017多校訓練賽第四場 HDU 6071短路

Lazy Running Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 101    Accepted S

HDU 6290短路

高玩小Q不僅喜歡玩尋寶遊戲,還喜歡一款升級養成類遊戲。在這個遊戲的世界地圖中一共有n個城鎮,編號依次為1到n。這些城鎮之間有m條單向道路,第i 條單項道路包含四個引數ui,vi,ai,bi,表示一條從ui號城鎮出發,在vi號城鎮結束的單向道路,因為是單向道路,這不意味著小Q可以從vi沿著該道路走到ui。小Q的

hdu 2544短路

Problem Description 在每年的校賽裡,所有進入決賽的同學都會獲得一件很漂亮的t-shirt。但是每當我們的工作人員把上百件的衣服從商店運回到賽場的時候,卻是非常累的!所以現在他們想要尋找最短的從商店到賽場的路線,你可以幫助他們嗎? Input

HDU 4786小生成樹 kruskal

desc cpp using tran soft fine put sea can 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=4786 Problem Description   Coach Pang is

POJ 1062 昂貴的聘禮短路中等題

clu inf fin 遍歷 () 交易 超過 給他 main 昂貴的聘禮 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 51879 Accepted: 15

New Game! 短路+建圖

std fab pre lse quest map n) 位置 str New Game! https://www.nowcoder.com/acm/contest/201/L 題目描述 Eagle Jump公司正在開發一款新的遊戲。Hifumi Takimoto作為其中

訪問短路+搜尋剪枝

2247: 訪問(deliver) 題目描述 給你一個n個頂點的鄰接矩陣(圖),以及每個頂點的訪問時限,要求從頂點1開始,尋找一個訪問序列,要求在每個頂點的訪問時限之前訪問,且每個頂點的訪問時間之和最小 輸入 第一行一個數n,2&l

HDU - 2112 HDU Today短路 dijkstra)

題目連結 題意:找兩個城市之間的最短路,沒有就輸出-1; 簡單題,把城市轉成數字做就行,但有坑點:起點和終點可以相同;   #include <iostream> #include <cstdio> #include <string> #

藍書演算法競賽進階指南刷題記錄——POJ3613 Cow Replays短路+矩陣乘法

題目:POJ3613. 題目大意:給出一張圖,然你求出經過N條邊後,S到T的最短路. 這道題一開始覺得挺容易的,用f[i][j]表示從起點到點i經過j的最短路,不斷更新就可以了. 但是突然發現數據巨大根本跑不過去... 然後就開始看書上的題解了... 書上居然要用矩陣乘法,好

Gym - 101986F Pizza Delivery 短路必經路徑

  題意:給你一個有向圖,每一條邊在第i天都會反向,問反向後的最短路是否有變化。每次反向都是獨立的。   解題思路:我們先把起點到所有點的最短路求出來,然後把所有邊反向,然後求終點到所有點的最短路。 這樣我們就記錄了兩個陣列d1,d2,分別記錄起點和終點

牛客練習賽27 水圖短路 or dfs

題目描述  小w不會離散數學,所以她van的圖論遊戲是送分的 小w有一張n個點n-1條邊的無向聯通圖,每個點編號為1~n,每條邊都有一個長度 小w現在在點x上 她想知道從點x出發經過每個點至少一

hdu 1081 大子矩陣和dp To The Max

Problem Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of

藍書演算法競賽進階指南刷題記錄——CH0602 黑暗城堡短路樹計數

題目大意:給出一張圖,求這張圖不同最短路樹的形態.期中最短路樹指的是對於任意一個點i,樹上1到i的路徑長度等於圖上1到i的最短路徑長度的生成樹. 我們發現這棵生成樹必須滿足的條件其實就是以1為根,1到任意一個點的路徑長度要是原圖的一條最短路. 我們用dis[i]表示原

3339 In Action 短路+01揹包

簡述題意: 給出N個供電站編號從1~N,然後給出M條邊:並且給出這N個供電站的電量。每輛坦克從基地出發去攻擊供電站,並且每輛坦克只能攻擊一個供電站,求要使的破壞總電量的一半以上,求這些坦克要走的最短距離。 難度:NOIP 演算法:首先,從基地跑一次最短路,求出到所有

bzoj 4398: 福慧雙修短路建模/構造

簡述題意: 給定一個有向圖,對於連線同兩個點的邊算作同一條,問不經過重複邊的最小正權環。                      保證沒有重邊(這個是指有向的),沒有自環。 演算法:最短路+構造 難度:NOIP+ 題解: 請見黃學長部落格,黃學長描述的再清楚不過了

2069)すぬけ君の地下鉄旅行 / Snuke's Subway Trip(短路建圖思路'剖解'分析+想象法~)

前言 題目 題目連結 題目大意 資料範圍 思路 建圖方法 關於實現 關於坑點 程式碼 前言 做了一下午的真心奉獻 題目 Time limit : 3sec / Memory limit : 256MB Score : 600

hdu 3007小圓覆蓋

題意:給平面上的一些點,用半徑最小的圓把所有點覆蓋了。 1、在點集中任取三個點A、B、C。 2、做一個包含ABC三點的最小圓,圓周可能通過這三點,也可能只通過其中兩點,但包含第三個點。後一種情況圓周上的兩點一定是位於圓周直徑的兩端。 3、在點集中找出距離第2步所建圓圓心最