Destroy Walls HDU - 6187
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 are n 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.
Input
There are several test cases.
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 xi,yi.
Then m lines follow, the ith line contains 3 integers ui,vi,wi
|xi|,|yi|≤105
3≤n≤100000,1≤m≤200000
1≤ui,vi≤n,ui≠vi,0≤wi≤10000
Output
For each test case outout one line with 2 integers sperate by a space, indicate how many walls the king should destroy at least to achieve his goal, and the minimal cost under this condition.
Sample Input
4 4
-1 -1
-1 1
1 1
1 -1
1 2 1
2 3 2
3 4 1
4 1 2
Sample Output
1 1
坐标没用,直接求森林的最大生成树,然后用总到边数减去,生成树到边数即可
code:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
const int N = 1e5+7;
struct Edge{
int from,to,w;
Edge(){}
Edge(int x,int y,int z):from(x),to(y),w(z){}
bool operator < (const Edge &a)const{
return w > a.w;
}
}edge[N*2];
int pre[N];
bool vis[N];
int Find(int x){
return pre[x] == x ? x : (pre[x] = Find(pre[x]));
}
P kruskal(int n,int m){
int treenum = n;//一开始假设每棵树都是一个森林
for(int i = 1; i <= n; i++)
pre[i] = i;
sort(edge,edge+m);
int ans = 0;
for(int i = 0; i < m; i++){
Edge &e = edge[i];
if(Find(e.from) == Find(e.to)) continue;
pre[Find(e.from)] = Find(e.to);
treenum--;//每连接一条边就少一个森林
ans += e.w;
}
return P(treenum,ans);
}
int main(){
int n,x,y,u,v,w,m;
while(scanf("%d%d",&n,&m) != EOF){
for(int i = 1; i <= n; i++) scanf("%d%d",&x,&y);
int all = 0;
for(int i = 0; i < m; i++){
scanf("%d%d%d",&u,&v,&w);
edge[i] = Edge(u,v,w);
all += w;
}
P p = kruskal(n,m);
printf("%d %d\n",m-(n-p.first),all-p.second);
//总到点数减森林数是生成树到边数,总边数减去就是
// 应该删去到边
}
return 0;
}
本文介绍了一种利用最小生成树算法解决城市规划中拆墙问题的方法。国王希望拆除最少数量的墙并使成本最低,文章详细阐述了如何通过计算森林的最大生成树来实现这一目标,并给出了具体的代码实现。
1314

被折叠的 条评论
为什么被折叠?



