Destroy Walls
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 377 Accepted Submission(s): 166
Problem Description
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.
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
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
Source
Recommend
题意:有一个V个结点M条边的带边权无向平面图,有一个人在一个区域,要拆一些墙使得他可以到达任意一个区域,问最小花费。
思路:首先可以发现题目就是要求一个平面图对偶图的最小生成树。
根据欧拉公式我们可以知道对于一个有k个连通分量的平面图的区域数r=E−V+k+1。
根据欧拉公式我们可以知道对于一个有k个连通分量的平面图的区域数r=E−V+k+1。
那么对偶图生成树的边数为r−1=E−V+k,这些边也就是要删除的原图中的边,那么要留下的边数就是V−k这刚好就是原图每个连通分量生成树的边数之和。
考虑保留原图每个连通分量的生成树,显然满足要求。
题目要求花费最小,也就是留下的边权值最大,那么我们直接对每个连通分量求最大生成树即可,删除的边数就是总边数减去生成树的边数和,最小花费就是全部边的花费减去最大生成树的花费。
1 //2017-11-16 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 7 using namespace std; 8 9 const int N = 110000; 10 const int M = 220000; 11 12 namespace DSU{ 13 int fa[N]; 14 void init(){ 15 for(int i = 1; i < N; i++)fa[i] = i; 16 } 17 int getfa(int x){ 18 return fa[x] = x == fa[x] ? x : getfa(fa[x]); 19 } 20 void merge(int a, int b){ 21 int af = getfa(a); 22 int bf = getfa(b); 23 if(af != bf){ 24 fa[bf] = af; 25 } 26 } 27 } 28 29 struct Edge{ 30 int u, v, w; 31 bool operator< (const Edge e) const { 32 return w > e.w; 33 } 34 }edge[M]; 35 36 int main() 37 { 38 freopen("input.txt", "r", stdin); 39 int n, m, x, y; 40 while(~scanf("%d%d", &n, &m)){ 41 for(int i = 0; i < n; i++) 42 scanf("%d%d", &x, &y); 43 int ans = 0; 44 for(int i = 0; i < m; i++){ 45 scanf("%d%d%d", &edge[i].u, &edge[i].v, &edge[i].w); 46 ans += edge[i].w; 47 } 48 DSU::init(); 49 sort(edge, edge+m); 50 int cnt = 0; 51 for(int i = 0; i < m; i++){ 52 int u = edge[i].u; 53 int v = edge[i].v; 54 if(DSU::getfa(u) != DSU::getfa(v)){ 55 ans -= edge[i].w; 56 DSU::merge(u, v); 57 cnt++; 58 } 59 } 60 printf("%d %d\n", m-cnt, ans); 61 } 62 63 return 0; 64 }