题目
n(n<=1e5)个点,代表二维平面上上的若干点,
m(m<=2e5)条边,第i条边的权值wi(0<=wi<=1e4),
一条边构成一道墙,被墙围在一起的地方是不可达的
平面上有一个出发点(0.6√2,0.6√3),拆掉墙的代价为这条边的权值
要求在满足拆掉的墙数最少的情况下,使代价总和最小
思路来源
https://blog.youkuaiyun.com/qq_37383726/article/details/77984530
题解
注意到,给的图是一个平面图,任何两条边除了端点之外没有其他交点
虽然我觉得平面图也没什么用,一般很少遇到非平面图的图
逆向想这个问题,拆墙是因为有环,所以才有地方不可达
最后拆完之后的图,是无环图;删掉的边最少时,构成一棵树
代价最少,就是剩余边权最多,
就等价于求原图的最大生成树,再用总边权减去即可
n个点的读入没什么用,出发点的位置也没什么用
孤立点管都不用管,连边都没有管它干啥
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e5+10;
int n,m,par[maxn];
int x,y,cnt,ans;
struct edge
{
int u,v,w;
}e[maxn*2];
bool cmp(edge a,edge b)
{
return a.w>b.w;
}
int find(int x)
{
return par[x]==x?x:par[x]=find(par[x]);
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
ans=cnt=0;
for(int i=1;i<=n;++i)
{
par[i]=i;
scanf("%d%d",&x,&y);
}
for(int i=1;i<=m;++i)
{
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
ans+=e[i].w;
}
sort(e+1,e+m+1,cmp);
for(int i=1;i<=m;++i)
{
x=find(e[i].u),y=find(e[i].v);
if(x==y)continue;
ans-=e[i].w;
cnt++;
if(x<y)par[y]=x;
else par[x]=y;
}
printf("%d %d\n",m-cnt,ans);
}
return 0;
}