Destroy Walls

本文解析了HDU6187题目,即如何通过去除最少数量的边并确保剩余边组成的图仍为连通图来解决特定问题。文中详细介绍了使用最大生成树算法求解的方法,并提供了完整的C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HDU 6187
这里写图片描述

给定n平面点一级m条边连接这些点,问最少去掉多少条边,以及在这情况下去掉边的点权最少,使得整个图是一个连通图。
其实仔细想一下就能发现满足这个条件当且仅当这些围墙不存在环,就是剩余的边形成的只会是树或森林,所以做一遍最大生成树就好,其他的都去掉

#include<bits/stdc++.h>
using namespace std;

const int Len = 500010;
struct edge {
    int u, v, w;
    edge(int u = 0, int v = 0, int w = 0):u(u), v(v), w(w){}
};
edge e[Len];
int f[Len];
int cnt;
long long ans;
int u, v, w;
int n, m;
int x[Len], y[Len];

int findfather(int x) {
    if (f[x] == x) return x;
    return f[x] = findfather(f[x]);
}

bool cmp(const edge& p, const edge& q) {
    return p.w > q.w;
}

int main() {
    while(scanf("%d%d", &n, &m) != EOF) {
        for (int i = 1; i <= n; i++) f[i] = i;
        for (int i = 1; i <= n; i++) scanf("%d%d", x+i, y+i);
        ans = 0;
        for (int i = 1; i <= m; i++) {
            scanf("%d%d%d", &u, &v, &w);
            e[i] = edge(u,v,w);
            ans += w;
        }
        sort(e+1, e+m+1, cmp);
        //printf("%lld\n", ans);
        cnt = 0;
        for (int i = 1; i <= m; i++) {
            int fa = findfather(e[i].u), fb = findfather(e[i].v);
            if (fa != fb) {
                f[fa] = fb;
                cnt++;
                //printf("%d %d %d\n", e[i].u, e[i].v, e[i].w);
                ans -= e[i].w;
            }
        }
        printf("%d %lld\n", m-cnt, ans);
    }
    return 0;
}
请作为资深开发工程师,解释我给出的代码。请逐行分析我的代码并给出你对这段代码的理解。 我给出的代码是: model new model title 'Testing Bonded Particle Model' ; Set the domain extent model domain extent -0.05 0.05 -0.05 0.05 -0.1 0.1 condition destroy contact cmat default model linear method deformability emod 1.4e9 kratio 0.0 contact cmat default property dp_nratio 0.47 ; create walls extending past the edges of the sample wall generate id 1 plane dip 0 dip-direction 0 position 0 0 0.04 wall generate id 2 plane dip 0 dip-direction 0 position 0 0 -0.04 wall generate id 3 plane dip 90 dip-direction 90 position -0.025 0 0 wall generate id 4 plane dip 90 dip-direction 90 position 0.025 0 0 wall generate id 5 plane dip 90 dip-direction 0 position 0 -0.025 0 wall generate id 6 plane dip 90 dip-direction 0 position 0 0.025 0 model random 10002 ball distribute porosity 0.3 radius 1.4e-3 2.0e-3 ... box -0.025 0.025 -0.025 0.025 -0.04 0.04 ball attribute density 2900 damp 0.7 ; Calm the system model cycle 1000 calm 10 ; Solve the system to a target limit (here the average force ratio) ; Use density scaling to quickly reach equilibrium model mechanical timestep scale model solve ratio-average 1e-4 model mechanical timestep auto model calm ; Delete side walls ; Be careful to include the keyword 'walls' or else facets will be deleted wall delete walls range id 3 6 wall generate id 7 cone base (0.0,0.0,0.04) height [0.02] radius [0.005] [0.01] cap true false wall generate id 9 cone base (0.0,0.0,-0.06) height [0.02] radius [0.01] [0.005] cap false true ; make cylinder ball delete range cylinder end-1 0 0 -0.04 end-2 0 0 0.04 rad 0.02 not wall delete walls range id 1 2 model save 'unbonded'
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值