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
题意:给你n个点坐标,两点之间有一堵墙,和拆掉这堵墙的花费,求到达任何一个地方的最小花费
题解:最大生成树,小边直接去掉
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int M=2e5+100;
const int N=1e5+100;
int f[N];
int n,m;
long long ans;
int num;
struct node
{
int u,v,w;
}mp[M];
void init()
{
for(int i=1;i<=n;i++)
f[i]=i;
num=0;
ans=0;
}
bool cmp(node a,node b)
{
return a.w>b.w;
}
int find(int x)
{
return x==f[x]?x:f[x]=find(f[x]);
}
bool merge(int a,int b)
{
int fa=find(a);
int fb=find(b);
if(fa==fb)
return 0;
f[fa]=fb;
return 1;
}
int main()
{
int x,y;
while(~scanf("%d%d",&n,&m))
{
init();
for(int i=0;i<n;i++)
scanf("%d%d",&x,&y);
for(int i=0;i<m;i++)
scanf("%d%d%d",&mp[i].u,&mp[i].v,&mp[i].w);
sort(mp,mp+m,cmp);
for(int i=0;i<m;i++)
{
if(!merge(mp[i].u,mp[i].v))
{
ans+=mp[i].w;
num++;
}
}
printf("%d %lld\n",num,ans);
}
return 0;
}