hdu 3405 删掉某点后 求最小生成树

给出N个点的坐标 边的权值为两点间的距离 删掉其中某点 求最小生成树的权值和 要求这权值最小

因为最多50个点 所以具体是删哪个点 用枚举
假如有4个点 就要求4次最小生成树 分别是2 3 4 | 1 3 4 | 1 2 4 | 1 2 3 这些点的

Sample Input
2
5
0 0
1 0
18 0
0 1
1 1
3
0 0
1 0
0 1

Sample Output
3.00
1.00

prim

 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 double cost[MAXN][MAXN] ;
14
15 struct poin
16 {
17     int x ;
18     int y ;
19 }p[110] , q[110];
20
21 double Prim(int n)
22 {
23     int i , j ;
24     for (i = 0 ; i < n ; i++)
25         for (j = 0 ; j < n ; j++)
26             cost[i][j] = INF ;
27     for (i = 0 ; i < n ; i++)
28             for (j = i+1 ; j < n ; j++)
29             {
30                 double t = sqrt((double)(q[i].x - q[j].x) * (q[i].x - q[j].x) + (q[i].y - q[j].y) * (q[i].y - q[j].y)) ;
31                 cost[i][j] = t ;
32                 cost[j][i] = t ;
33             }
34     double ans=0;
35     memset(vis,false,sizeof(vis));
36     vis[0]=true;
37     for(int i=1;i<n;i++)lowc[i]=cost[0][i];
38     for(int i=1;i<n;i++)
39     {
40         double minc=INF;
41         int p=-1;
42         for(int j=0;j<n;j++)
43             if(!vis[j]&&minc>lowc[j])
44             {
45                 minc=lowc[j];
46                 p=j;
47             }
48             if(minc==INF)return -1;//原图不连通
49             ans+=minc;
50             vis[p]=true;
51             for(int j=0;j<n;j++)
52                 if(!vis[j]&&lowc[j]>cost[p][j])
53                     lowc[j]=cost[p][j];
54     }
55     return ans;
56 }
57
58 int main()
59 {
60
61    // freopen("in.txt","r",stdin) ;
62     int T ;
63     scanf("%d" , &T) ;
64     while(T--)
65     {
66         int n ;
67         scanf("%d" , &n) ;
68         int i , j ;
69         double MIN = INF ;
70         for (i = 0 ; i < n ; i++)
71             scanf("%d %d" , &p[i].x , &p[i].y) ;
72
73         for (i = 0 ; i < n ; i++)
74         {
75             int k = 0 ;
76             for (j = 0 ; j < n ; j++)
77             {
78                if (i == j)
79                   continue ;
80                q[k].x = p[j].x ;
81                q[k].y = p[j].y ;
82                k++ ;
83             }
84             double ans = Prim(n-1) ;
85             if (ans < MIN)
86                 MIN = ans ;
87         }
88         printf("%.2lf\n" , MIN) ;
89
90     }
91     return 0 ;
92 }

时间: 2024-10-09 22:17:56

hdu 3405 删掉某点后 求最小生成树的相关文章

poj 2117 Electricity(tarjan求割点删掉之后的连通块数)

题目链接:http://poj.org/problem?id=2117 题意:求删除一个点后,图中最多有多少个连通块. 题解:就是找一下割点,根节点的割点删掉后增加son-1(son为子树个数),非根节点删掉之后++ #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int N = 1e4 + 10; const int M = 1e6 + 10; st

HDU 4738 Caocao&#39;s Bridges tarjan求桥

Caocao's Bridges Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army still was not good at water battles, so he came up with another idea. He built many islands in the Chan

hdu 2489 Minimal Ratio Tree(dfs枚举 + 最小生成树)~~~

题目: 链接:点击打开链接 题意: 输入n个点,要求选m个点满足连接m个点的m-1条边权值和sum与点的权值和ans使得sum/ans最小,并输出所选的m个点,如果有多种情况就选第一个点最小的,如果第一个点也相同就选第二个点最小的........ 思路: 求一个图中的一颗子树,使得Sum(edge weight)/Sum(point weight)最小~ 数据量小,暴力枚举~~~~~dfs暴力枚举C(M,N)种情况. 枚举出这M个点之后,Sum(point weight)固定,进行prim或者K

【HDU 4408】Minimum Spanning Tree(最小生成树计数)

Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Kruskal algorithm of minimum spanning tree, XXX finds that there might be multiple solutions. Given an undirected weighted graph with n (1<=n<=100) vertex

HDU 1162 Eddy&#39;s picture(图论-最小生成树)

题目如下: Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7267    Accepted Submission(s): 3676 Problem Description Eddy begins to like painting pictures recently ,he is sure of himse

ListView中删掉某个item时出现整个ListView都看不见,但是数据源有改变

搞了一个下午都没找到问题根源在哪里,醉了!!!可能是功夫不够吧!最近在弄一个listview的item删除,在每个item中加了删除按钮,删掉某行时,数据源有发生改变,但是ListView居然不见了,像是被隐藏了,怪咖...当时我的ListView布局是这样的: <span style="font-size:14px;"> <ListView android:id="@+id/lv_collect_list" android:layout_widt

hdu 1536 S-Nim 博弈论,,求出SG&#39;函数就可以解决

S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4975    Accepted Submission(s): 2141 Problem Description Arthur and his sister Caroll have been playing a game called Nim for some time now

POJ 2135 Farm Tour &amp;&amp; HDU 2686 Matrix &amp;&amp; HDU 3376 Matrix Again 费用流求来回最短路

累了就要写题解,最近总是被虐到没脾气. 来回最短路问题貌似也可以用DP来搞,不过拿费用流还是很方便的. 可以转化成求满流为2 的最小花费.一般做法为拆点,对于 i 拆为2*i 和 2*i+1,然后连一条流量为1(花费根据题意来定) 的边来控制每个点只能通过一次. 额外添加source和sink来控制满流为2. 代码都雷同,以HDU3376为例. #include <algorithm> #include <iostream> #include <cstring> #in

Prim算法和Kruskal算法求最小生成树

Prim算法 连通分量是指图的一个子图,子图中任意两个顶点之间都是可达的.最小生成树是连通图的一个连通分量,且所有边的权值和最小. 最小生成树中,一个顶点最多与两个顶点邻接:若连通图有n个顶点,则最小生成树中一定有n-1条边. Prim算法需要两个线性表来进行辅助: visited: 标记已经加入生成树的顶点:(它的功能可以由tree取代) 初始状态:生成树根节点为真,其它为0. tree: 记录生成树,tree[x]保存顶点x的直接根节点下标,若x为树的根节点则tree[x]为其自身. 初始状