第四届华中区程序设计邀请赛暨武汉大学第十三届校赛 网络预选赛 Problem 1566 - C - Spanning Tree

本文介绍了一种使用最小生成树算法解决动态图问题的方法。通过不断加入边并重新计算最小生成树来更新总权重,适用于处理多次询问的情况。文章提供了完整的C++实现代码,包括输入输出示例。

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

Description
You are given a graph with N nodes and M edges. 
Then every time you are required to add an additional edge with weight Wi connecting the node Ai and Bi in the graph, and then calculate the sum of the edges’ weight of the Minimum Spanning Tree of the current graph. You’ll be asked to complete the mission for Q times.
The Minimum Spanning Tree of a graph is defined to be a set of N - 1 edges which connects all the N nodes of the graph and the sum of all the edges is as small as possible.
It's guaranteed that the graph is connected.
Input
First line of each case contains three numbers N , M and Q.(1 ≤  N,Q ≤ 1000, 1 ≤  M ≤ 100000 ,)

The following M lines contains three numbers Ai , Bi and Wi.( 1 ≤  Ai, Bi ≤ 1000, 1 ≤  Wi≤ 100000).

The last Q lines of this case contains three numbers Ai , Bi and Wi. ( 1 <= Ai, Bi <= 1000, 1 ≤  Wi≤ 100000 ). 
Output
Output the answer in a single line for each case.
Sample Input
3 3 3
2 1 8
3 1 4
1 2 6
1 2 4
2 3 1
1 1 4
3 3 3
2 1 7
3 2 8
3 3 6
1 3 3
2 2 3
2 2 3
Sample Output
8
5
5
10
10
10

用STL的vector容器写,直接内部排序了。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define PI acos(-1.0)
#define M 1000005  //10^6
#define eps 1e-8
#define LL long long
#define moo 1000000007
#define INF 9999999999
using namespace std;
#define maxm 100000+500
#define maxn 100000+500

struct Edge
{
    int u,v,w;
    bool operator <(const Edge &rhs) const
    {
        return w<rhs.w;
    }
    void read()
    {
        scanf("%d %d %d",&u,&v,&w);
    }

}edge[maxm];

vector<Edge> res;

int fa[maxm];

int findfa(int x)
{
    if(x==fa[x])
    return x;
    return fa[x]=findfa(fa[x]);
}

bool same(int a,int b)
{
    return findfa(a)==findfa(b);
}

void merge(int a,int b)
{

    int xx=findfa(a);
    int yy=findfa(b);
    if(xx>yy)
    fa[xx]=yy;
    else
    fa[yy]=xx;

}

int kruscal(vector<Edge> &e,int n)
{
    for(int i=1;i<=n;i++)
    fa[i]=i;

    vector<Edge>res;

    sort(e.begin(),e.end());
    int ans=0;

    for(int i=0;i<e.size();i++)
    {
        if(same(e[i].u,e[i].v))
        continue;

        merge(e[i].u,e[i].v);
        ans+=e[i].w;
        res.push_back(e[i]);
    }
    e=res;
    return ans;

}

int main()
{
    int m,n,q;
    while(~scanf("%d %d %d",&n,&m,&q))
    {

        for(int i=0;i<m;i++)
        edge[i].read();

        edge[m++].read();
        sort(edge,edge+m);
        res.clear();

        for(int i=1;i<=n;i++)
        fa[i]=i;

        int ans=0;

        for(int i=0;i<m;i++)
        {
            if(same(edge[i].u,edge[i].v))
            continue;

            merge(edge[i].u,edge[i].v);
            ans+=edge[i].w;
            res.push_back(edge[i]);
        }

        q--;
        printf("%d\n",ans);

        while(q--)
        {
            int u,v,w;
            scanf("%d %d %d",&u,&v,&w);
            res.push_back((Edge){u,v,w});
            printf("%d\n",kruscal(res,n));
        }

    }

    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值