hdu 4463 有一条边必须加上 (2012杭州区域赛K题)

耐克店 和 苹果店必须相连

Sample Input
4
2 3
0 0
1 0
0 -1
1 -1
0

Sample Output
3.41

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <algorithm>
 5 # include <cmath>
 6 # define LL long long
 7 using namespace std ;
 8
 9 const int INF=0x3f3f3f3f;
10 const int MAXN=110;
11 bool vis[MAXN];
12 double lowc[MAXN];
13 int n ;
14 double cost[MAXN][MAXN] ;
15
16 struct poin
17 {
18     int x ;
19     int y ;
20 }p[MAXN];
21
22 double Prim()//点是0~n-1
23 {
24     double ans=0;
25     memset(vis,false,sizeof(vis));
26     vis[0]=true;
27     for(int i=1;i<n;i++)lowc[i]=cost[0][i];
28     for(int i=1;i<n;i++)
29     {
30         double minc=INF;
31         int p=-1;
32         for(int j=0;j<n;j++)
33             if(!vis[j]&&minc>lowc[j])
34             {
35                 minc=lowc[j];
36                 p=j;
37             }
38             if(minc==INF)return -1;//原图不连通
39             ans+=minc;
40             vis[p]=true;
41             for(int j=0;j<n;j++)
42                 if(!vis[j]&&lowc[j]>cost[p][j])
43                     lowc[j]=cost[p][j];
44     }
45     return ans;
46 }
47
48 int main()
49 {
50
51    // freopen("in.txt","r",stdin) ;
52     while(scanf("%d" , &n) != EOF)
53     {
54         if (n == 0)
55             break ;
56         int i , j ;
57         int pp , qq ;
58         scanf("%d %d" , &pp , &qq) ;
59         for (i = 0 ; i < n ; i++)
60             scanf("%d %d" , &p[i].x , &p[i].y) ;
61         for (i = 0 ; i < n ; i++)
62             for (j = i+1 ; j < n ; j++)
63             {
64                 double t = sqrt((double)(p[i].x - p[j].x) * (p[i].x - p[j].x) + (p[i].y - p[j].y) * (p[i].y - p[j].y)) ;
65                 cost[i][j] = t ;
66                 cost[j][i] = t ;
67             }
68         double k = cost[pp-1][qq-1] ;
69         cost[pp-1][qq-1] = 0 ;
70         cost[qq-1][pp-1] = 0 ;
71         k += Prim() ;
72         printf("%.2lf\n" , k) ;
73
74
75     }
76     return 0 ;
77 }

时间: 2024-07-31 06:41:41

hdu 4463 有一条边必须加上 (2012杭州区域赛K题)的相关文章

HDU 4463 Outlets (prime_杭州区域赛水题)

Problem Description In China, foreign brand commodities are often much more expensive than abroad. The main reason is that we Chinese people tend to think foreign things are better and we are willing to pay much for them. The typical example is, on t

hdu 5137 去掉一个点 使得最短路最大(2014广州区域赛K题)

题意:从2~n-1这几个点中任意去掉一个点,使得从1到n的最短路径最大,如果任意去掉一个点1~n无通路输出Inf. Sample Input4 51 2 31 3 71 4 502 3 43 4 23 21 2 302 3 100 0 Sample Output50Inf 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 #

hdu 5122 (2014北京区域赛 K题)

把一个序列按从小到大排序 要执行多少次操作 只需要从右往左统计,并且不断更新最小值,若当前数为最小值,则将最小值更新为当前数,否则sum+1 Sample Input255 4 3 2 155 1 2 3 4 Sample OutputCase #1: 4Case #2: 1 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5

HDU 4119 Isabella&#39;s Message (2011年成都赛区现场赛I题)

1.题目描述:点击打开链接 2.解题思路:本题是一道模拟题,要求模拟一个解密的过程,练习这么久第一次做模拟题1Y了,内心还是很激动的~.只需要根据题意,记录* 所在的位置即可,然后每次都是先解密,后顺时针旋转90度.把每次解密的信息放到一个vector里,接下来就是连接它们,得到解密后的字符串,在map中查找这些单词是否存在即可.如果都存在,就把这条解密信息放到ans中,最后对ans排序,输出ans[0]就是答案. 3.代码: //#pragma comment(linker, "/STACK:

HDU 4463 Outlets(一条边固定的最小生成树)

Outlets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2594    Accepted Submission(s): 1196 Problem Description In China, foreign brand commodities are often much more expensive than abroad. Th

HDU 5017 Ellipsoid(西安网络赛K题)

HDU 5017 Ellipsoid 题目链接 思路:模拟退火大法好! 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int D[8][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}}; dou

HDU 4115 Eliminate the Conflict (2011年成都赛区现场赛E题)

1.题目描述:点击打开链接 2.解题思路:本题属于2-SAT问题,第一次做这种题,还是很感兴趣的.2-SAT解决的是给定m条语句,每一条语句形如"xi为真或xj为假",问你是否可能让所有的语句都成立.那么不难发现,本题就属于一种2-SAT问题. 首先,根据题意可知,第i轮有2种出法,要么是x[i],要么是(x[i]+1)%3.我们令xi为真表示出x[i],xi为假表示出(x[i]+1)%3.对于w==1的情况,我们要分2种情况讨论:当x[u]==x[v]时,表示第u轮出x[u],那么第

HDU 6229 Wandering Robots(2017 沈阳区域赛 M题,结论)

题目链接  HDU 6229 题意 在一个$N * N$的格子矩阵里,有一个机器人. 格子按照行和列标号,左上角的坐标为$(0, 0)$,右下角的坐标为$(N - 1, N - 1)$ 有一个机器人,初始位置为$(0, 0)$. 现在这个矩阵里面,有一些障碍物,也就是说机器人不能通过这些障碍物. 若机器人当前位置为$(x, y)$,那么他下一个位置有可能为与当前格子曼哈顿距离为$1$的所有格子的任意1个. 也有可能停留在原来的位置$(x, y)$ 求经过无限长的时间之后,这个机器人的位置在给定区

HDU 5974 A Simple Math Problem ——(数论,大连区域赛)

给大一的排位赛中数论的一题.好吧不会做...提供一个题解吧:http://blog.csdn.net/aozil_yang/article/details/53538854. 又学了一个新的公式..如果x和y互质,那么x+y和x*y互质.证明如下:随便找一个x中有的因子c,因为x,y互质,因此c不是y的因子.同时c是x*y的因子,由同余模方程知(x+y)% c = x % c + y % c = 0 + y % c.因为c不是y的因子,所以不等于0,所以c不是x+y的因子.同理可以证得x和y中的