361. 连接农场 Agri-Net(挑战程序设计竞赛)
作者:互联网
题目链接:https://www.papamelon.com/problem/361;
今天最后一道mst,明天再战;
Talk is cheap. Show me the code.
#include<bits/stdc++.h> using namespace std; const int num=1e4+10; int s[110]; struct edge { int u; int v; int w; }e[num]; int cnt; int ans; int n; bool cmp(edge a,edge b) { return a.w<b.w; } int find(int x) { if(x!=s[x]) s[x]=find(s[x]); return s[x]; } void kruskal() { int t=0; for(register int i=1;i<=n;i++) s[i]=i; sort(e+1,e+1+cnt,cmp); for(register int i=1;i<=cnt;i++) { int b=find(e[i].u); int c=find(e[i].v); if(b==c) continue; s[c]=b; ans+=e[i].w; t++; if(t==n-1) break; } } int main() { std::ios::sync_with_stdio(false); while(~scanf("%d",&n))
{
ans=0;
cnt=0; for(register int i=1;i<=n;i++) { for(register int j=1;j<=n;j++) { int temp; cin>>temp; if(temp) { e[++cnt].u=i; e[cnt].v=j; e[cnt].w=temp; } } } kruskal(); cout<<ans<<endl;
} return 0; }
标签:cnt,num,temp,int,ans,edge,Agri,Net,361 来源: https://www.cnblogs.com/LQS-blog/p/16211097.html