hdu 3832 Earth Hour

http://acm.hdu.edu.cn/showproblem.php?pid=3832

 1 #include <cstdio>
2 #include <iostream>
3 #include <cmath>
4 #include <cstring>
5 #include <vector>
6 #include <algorithm>
7 #include <queue>
8 #define maxn 1001
9 using namespace std;
10 const int inf=1<<28;
11
12 int g[maxn][maxn];
13 bool vis[maxn];
14 int dis1[maxn],dis2[maxn],dis3[maxn];
15 int n;
16 struct node
17 {
18 int x,y,r;
19 }p[maxn];
20 int sqr(int x)
21 {
22 return x*x;
23 }
24
25 void spfa(int src,int dis[])
26 {
27 queue<int>q;
28 memset(vis,false,sizeof(vis));
29 for(int i=0; i<=n; i++) dis[i]=inf;
30 dis[src]=0;
31 vis[src]=true;
32 q.push(src);
33 while(!q.empty())
34 {
35 int u=q.front(); q.pop();
36 vis[u]=false;
37 for(int i=0; i<n; i++)
38 {
39 if(dis[i]>dis[u]+g[u][i]&&g[u][i]!=inf)
40 {
41 dis[i]=dis[u]+g[u][i];
42 if(!vis[i])
43 {
44 q.push(i);
45 vis[i]=true;
46 }
47 }
48 }
49 }
50 }
51
52 int main()
53 {
54 int t;
55 scanf("%d",&t);
56 while(t--)
57 {
58 scanf("%d",&n);
59 for(int i=0; i<=n; i++)
60 {
61 for(int j=0; j<=n; j++)
62 {
63 if(i==j) g[i][j]=0;
64 else g[i][j]=inf;
65 }
66 }
67 for(int i=0; i<n; i++)
68 {
69 cin>>p[i].x>>p[i].y>>p[i].r;
70 }
71 for(int i=0; i<n; i++)
72 {
73 for(int j=i+1; j<n; j++)
74 {
75 if(sqrt(sqr(p[i].x-p[j].x)+sqr(p[i].y-p[j].y))<=(p[i].r+p[j].r))
76 {
77 g[i][j]=g[j][i]=1;
78 }
79 }
80 }
81 spfa(0,dis1);
82 spfa(1,dis2);
83 spfa(2,dis3);
84 int min1=inf;
85 for(int i=0; i<n; i++)
86 {
87 //printf("%d %d %d\n",dis1[i],dis2[i],dis3[i]);
88 min1=min(min1,dis1[i]+dis2[i]+dis3[i]);
89 }
90 if(min1==inf) printf("-1\n");
91 else printf("%d\n",n-min1-1);
92 }
93 return 0;
94 }

hdu 3832 Earth Hour,布布扣,bubuko.com

时间: 2024-12-09 17:56:17

hdu 3832 Earth Hour的相关文章

HDU 3832 Earth Hour (最短路)

Earth Hour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)Total Submission(s): 1518    Accepted Submission(s): 607 Problem Description Earth Hour is an annual international event created by the WWF (World Wide Fun

HDU 3832 Earth Hour (最短路)

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 el

HDU 3832 Earth Hour(最短路)

题目地址:HDU 3832 这个题的这种方法我无法给出证明. 我当时这个灵感出来的时候是想的是要想覆盖的点最少,那就要尽量反复利用这些点,然后要有两个之间是通过还有一个点间接连接的,这样会充分利用那些点. 然后就这样写了一次,一直WA..然后中午睡觉的时候突然想到了有一种情况这样做是不正确的.那就是有个点作为中间点,与三个点相连的情况,这样的情况尽管也符合.可是会有反复边... 可是恰恰相反..反复边应该越多越好. . 那就充分利用了这些点.那么这个点应该怎么找呢?那就直接枚举好了.可是枚举每一

hdu 3832 Earth Hour (最短路变形)

Earth Hour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Submission(s): 1516    Accepted Submission(s): 606 Problem Description Earth Hour is an annual international event created by the WWF (World Wide Fu

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

hdu图论题目分类

=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many Tables 基础并查集★ 1272 小希的迷宫 基础并查集★ 1325&&poj1308 Is It A Tree? 基础并查集★ 1856 More is better 基础并查集★ 1102 Constructing Roads 基础最小生成树★ 1232 畅通工程 基础并查集★ 123

题单二:图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小

图论五百题!

生死看淡不服就淦,这才是人生! =============================以下是最小生成树+并查集======================================[HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基础并查集★1325&&poj1308 Is It A Tree? 基础并查集★1856 More is better 基础并查集★1102 Constructing Roads 基础最小生成树★1232 畅通工程 基

图论精炼500题

忘了从哪转的了... =============================以下是最小生成树+并查集====================================== [HDU] 1213               How Many Tables                    基础并查集★ 1272               小希的迷宫                     基础并查集★ 1325&&poj1308    Is It A Tree?