Description
国王的领地被一些城市和城市之间的城墙分成若干区域,国王的城堡在 (0.6∗2–√,0.6∗3–√) ( 0.6 ∗ 2 , 0.6 ∗ 3 ) 处,第 i i 个城市在处,第 ui u i 个城市和第 vi v i 个城市之间有一道城墙,破坏该城墙需要 wi w i 的花费,现在国王要破坏尽可能少的城墙使得他可以到达他领土的任意地方,且要求破坏城墙的总代价尽可能小
Input
多组用例,每组用例首先输入两个整数 n,m n , m 表示城市数和城墙数,之后 n n 行每行输入两个整数表示第 i i 个城市的位置,最后行每行三个整数 ui,vi,wi u i , v i , w i 表示 ui u i 城市和 vi v i 城市之间有一道城墙,破坏代价是 wi w i
(2≤n≤105,1≤m≤2⋅105,|xi|,|yi|≤105,0≤wi≤104) ( 2 ≤ n ≤ 10 5 , 1 ≤ m ≤ 2 ⋅ 10 5 , | x i | , | y i | ≤ 10 5 , 0 ≤ w i ≤ 10 4 )
Output
对于每组用例,输出最小的破坏城墙数量以及最小代价
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
Solution
要想国王可以到达任意地方,这些城市构成的图不能有环,为使破坏城墙数量最少且破坏总代价最小,即要使得剩余城墙数量尽可能多,且剩余城墙破坏总代价尽可能大,也即求该图的最大生成树
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=100001;
struct node
{
int u,v,w;
bool operator<(const node&b)const
{
return w>b.w;
}
}e[2*maxn];
int f[maxn];
int find(int x)
{
if(f[x]==x)return x;
return f[x]=find(f[x]);
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
int x,y,sum=0;
for(int i=1;i<=n;i++)scanf("%d%d",&x,&y),f[i]=i;
for(int i=0;i<m;i++)scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w),sum+=e[i].w;
sort(e,e+m);
int num=0,ans=0;
for(int i=0;i<m;i++)
{
int u=e[i].u,v=e[i].v,w=e[i].w;
u=find(u),v=find(v);
if(u==v)continue;
num++,ans+=w,f[u]=v;
}
printf("%d %d\n",m-num,sum-ans);
}
return 0;
}