HDU 6187 Destroy Walls(最大生成树)
来源:互联网 发布:淘宝直通车没有展现量 编辑:程序博客网 时间:2024/05/29 11:59
Long times ago, there are beautiful historic walls in the city. These walls divide the city into many parts of area.
Since it was not convenient, the new king wants to destroy some of these walls, so he can arrive anywhere from his castle. We assume that his castle locates at(0.6∗2–√,0.6∗3–√) .
There aren towers in the city, which numbered from 1 to n. The ith's location is (xi,yi) . Also, there are m walls connecting the towers. Specifically, the ith wall connects the tower ui and the tower vi (including the endpoint). The cost of destroying the ith wall is wi .
Now the king asks you to help him to divide the city. Firstly, the king wants to destroy as less walls as possible, and in addition, he wants to make the cost least.
The walls only intersect at the endpoint. It is guaranteed that no walls connects the same tower and no 2 walls connects the same pair of towers. Thait is to say, the given graph formed by the walls and towers doesn't contain any multiple edges or self-loops.
Initially, you should tell the king how many walls he should destroy at least to achieve his goal, and the minimal cost under this condition.
Since it was not convenient, the new king wants to destroy some of these walls, so he can arrive anywhere from his castle. We assume that his castle locates at
There are
Now the king asks you to help him to divide the city. Firstly, the king wants to destroy as less walls as possible, and in addition, he wants to make the cost least.
The walls only intersect at the endpoint. It is guaranteed that no walls connects the same tower and no 2 walls connects the same pair of towers. Thait is to say, the given graph formed by the walls and towers doesn't contain any multiple edges or self-loops.
Initially, you should tell the king how many walls he should destroy at least to achieve his goal, and the minimal cost under this condition.
For each test case:
The first line contains 2 integer n, m.
Then next n lines describe the coordinates of the points.
Each line contains 2 integers
Then m lines follow, the ith line contains 3 integers
4 4-1 -1-1 11 11 -11 2 12 3 23 4 14 1 2
1 1
题解:
让你求一颗最大生成树,输出点数n-选入生成树中的点数ans和边的值总和-生成树的值。。。前面输入点的坐标是废话,注意一开始wa了一次是因为还以为一定可以形成生成树第一个输出写成了m-n+1就错了,改成n-ans就对了
代码:
#include<iostream>#include<cstring>#include<stdio.h>#include<math.h>#include<string>#include<stdio.h>#include<queue>#include<stack>#include<map>#include<vector>#include<deque>#include<algorithm>using namespace std;#define INF 100861111#define ll long long#define eps 1e-7#define lson k*2#define rson k*2+1#define M (t[k].l+t[k].r)/2const int maxn=2e5+5;int pre[100005];struct node{ int f,t; int v;}a[maxn];int cmp(node x,node y){ return x.v>y.v;}int find(int x){ if(x!=pre[x]) pre[x]=find(pre[x]); return pre[x];}int main(){ int i,j,k,x,y,z,n,m,ans,d1,d2; ll sum,s; while(scanf("%d%d",&n,&m)!=EOF) { s=0; for(i=1;i<=n;i++) { scanf("%d%d",&x,&y); pre[i]=i; } for(i=0;i<m;i++) { scanf("%d%d%d",&a[i].f,&a[i].t,&a[i].v); s+=a[i].v; } sort(a,a+m,cmp); sum=0; ans=0; for(i=0;i<m;i++) { d1=find(a[i].f); d2=find(a[i].t); if(d1!=d2) { pre[d2]=d1; ans++; sum+=a[i].v; if(ans>=n-1) break; } } printf("%d %lld\n",m-ans,s-sum); } return 0;}
阅读全文
0 0
- HDU 6187 Destroy Walls(最大生成树)
- 【HDU 6187 】Destroy Walls 【平面图+最大生成树】
- HDU 6187 Destroy Walls(对偶图最小生成树)
- HDU-6187:Destroy Walls
- hdu 6187 Destroy Walls
- Destroy Walls
- HDU6187-Destroy Walls
- hdu 3367 Pseudoforest(最大生成树)
- hdu 4133 最大生成树
- HDU 3367 最大生成树
- HDU-3367最大生成树
- HDU 4940 Destroy Transportation system (瞎搞)
- HDU-#4786 Fibonacci Tree(最大最小生成树)
- HDU 3367 Pseudoforest(最大生成树+并查集)
- HDU 2377 Bad Cowtractors(最大生成树-Kruskal)
- HDU 3367 Pseudoforest(kruskal最大生成树变形)
- hdu 4313 Matrix(最大生成树)
- HDU 3367 Pseudoforest 最大生成树
- codeforces 851C C. Five Dimensional Points
- linux下core文件设置与查看
- NYOJ 329 循环小数
- 数学建模(10)——支持向量机的MATLAB应用
- BZOJ 3555: [Ctsc2014]企鹅QQ 哈希
- HDU 6187 Destroy Walls(最大生成树)
- 结构型设计模式
- POJ1077 Eight —— 正向BFS
- Root of AVL Tree
- Codeforces Round #432 (Div. 2) 题解
- Markdown语法入门
- WOJ1173-Josephus Permutation
- hash算法原理详解
- 老三长谈之Retrofit源码分析