当前位置: 代码迷 >> 综合 >> HDU 1233 还是畅通工程(MST裸题)
  详细解决方案

HDU 1233 还是畅通工程(MST裸题)

热度:13   发布时间:2023-12-08 11:15:04.0

题目链接:
HDU 1233 还是畅通工程
分析:
最小生成树模版题。

//1648K 296MS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;const int maxn=110;
const int INF=0x3f3f3f3f;int n,u,v,w,tot,fu,fv,ans;
int pre[maxn];struct Edge{int u,v,w;
}edge[maxn*maxn];int find(int x)
{return pre[x]==x?x:pre[x]=find(pre[x]);
}bool cmp(struct Edge a,struct Edge b)
{return a.w<b.w;
}int main()
{
#ifdef LOCALfreopen("in.txt","r",stdin);
#endifwhile(~scanf("%d",&n)&&n){for(int i=0;i<maxn;i++)pre[i]=i;tot=n*(n-1)/2;for(int i=0;i<tot;i++)scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);sort(edge,edge+tot,cmp);ans=0;for(int i=0;i<tot;i++){u=edge[i].u;v=edge[i].v;w=edge[i].w;fu=find(u);fv=find(v);if(fu!=fv){pre[fu]=fv;ans+=w;}}printf("%d\n",ans);}return 0;
}