比赛 26暑假集训模拟赛3 评测结果 AAAAAAAAAAATTTTTTTTA
题目名称 城市建设 最终得分 60
用户昵称 Lixj 运行时间 43.234 s
代码语言 C++ 内存使用 3.87 MiB
提交时间 2026-07-06 11:16:59
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
int n,m,q,fa[100001];
struct Edge{
    int x,y,w,id;
}e[100001];

void init(){
    for(int i=1;i<=n;i++)
        fa[i]=i;
    return;
}
int find_set(int x){
    if(x==fa[x])
        return x;
    return fa[x]=find_set(fa[x]);
}
void union_set(int x,int y){
    int fx=find_set(x);
    int fy=find_set(y);
    if(fx!=fy)
        fa[fy]=fx;
    return;
}
bool cmp(Edge &a,Edge &b){
    return a.w<b.w;
}
void kruskal(){
    long long ans=0;
    init();
    sort(e+1,e+m+1,cmp);
    //for(int i=1;i<=m;i++)   
     //   cout<<e[i].x<<" "<<e[i].y<<" "<<e[i].w<<endl;
    for(int i=1;i<=m;i++){
        if(find_set(e[i].x)!=find_set(e[i].y)){
            union_set(e[i].x,e[i].y);
            ans+=e[i].w; 
        }
    }
    cout<<ans<<endl;
    return;
}
int main(){
    freopen("hnoi2010_city.in","r",stdin);
    freopen("hnoi2010_city.out","w",stdout);
    cin>>n>>m>>q;
    //init();
    for(int i=1;i<=m;i++){
        int x,y,z;
        cin>>x>>y>>z;
        e[i].x=x;
        e[i].y=y;
        e[i].w=z;
        e[i].id=i;
        //fa[y]=x;
    }
    for(int i=1;i<=q;i++){
        int x,y;
        cin>>x>>y;
        for(int i=1;i<=m;i++)
            if(e[i].id==x)
                e[i].w=y;
        kruskal();
    }
    
    return 0;
}