Destroy Walls
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 236 Accepted Submission(s): 96
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.
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
Source
2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)
Recommend
liuyiding
有一个V个结点M条边的带边权无向平面图,有一个人在一个区域,要拆一些墙使得他可以到达任意一个区域,问最小花费。
首先可以发现题目就是要求一个平面图对偶图的最小生成树。
根据欧拉公式我们可以知道对于一个有k个连通分量的平面图的区域数 r=E−V+k+1 。那么那么对偶图生成树的边数为 r−1=E−V+k ,这些边也就是要删除的原图中的边,那么要留下的边数就是 V−k 这刚好就是原图每个连通分量生成树的边数之和。考虑保留原图每个连通分量的生成树,显然满足要求。题目要求花费最小,也就是留下的边权值最大,那么我们直接对每个连通分量求最大生成树即可,删除的边数就是总边数减去生成树的边数和,最小花费就是全部边的花费减去最大生成树的花费。
#include
#include
#include
#include
#include
using namespace std;
#define LL long long
const int MAXV=100000+3;
const int MAXE=200000+3;
struct Edge
{
int from, to, cost;
Edge(int f=0, int t=0, int c=0):from(f), to(t), cost(c){}
bool operator < (const Edge &other)const
{
return cost>other.cost;
}
}edge[MAXE];
int V, E;
int par[MAXV], high[MAXV];
int findfather(int x)
{
return par[x]=par[x]==x?x:findfather(par[x]);
}
bool unite(int a, int b)
{
int fa=findfather(a), fb=findfather(b);
if(fa==fb)
return false;
if(high[fa]>high[fb])
par[fa]=fb;
else
{
par[fb]=fa;
if(high[fa]==high[fb])
++high[fb];
}
return true;
}
void init()//初始化
{
for(int i=0;i<=V;++i)
{
par[i]=i;
high[i]=0;
}
}
int main()
{
while(~scanf("%d%d", &V, &E))
{
init();
for(int i=0;iint x, y;
scanf("%d%d", &x, &y);
}
LL ans=0;
for(int i=0;iscanf("%d%d%d", &edge[i].from, &edge[i].to, &edge[i].cost);
ans+=edge[i].cost;
}
sort(edge, edge+E);
int cnt=0;//保留的边数
for(int i=0;iif(unite(edge[i].from, edge[i].to))
{
ans-=edge[i].cost;
++cnt;
}
printf("%d %lld\n", E-cnt, ans);
}
return 0;
}