日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

51NOD 1212 无向图最小生成树

發(fā)布時(shí)間:2023/12/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 51NOD 1212 无向图最小生成树 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

N個(gè)點(diǎn)M條邊的無向連通圖,每條邊有一個(gè)權(quán)值,求該圖的最小生成樹。

輸入

第1行:2個(gè)數(shù)N,M中間用空格分隔,N為點(diǎn)的數(shù)量,M為邊的數(shù)量。(2 <= N <= 1000, 1 <= M <= 50000) 第2 - M + 1行:每行3個(gè)數(shù)S E W,分別表示M條邊的2個(gè)頂點(diǎn)及權(quán)值。(1 <= S, E <= N,1 <= W <= 10000)

輸出

輸出最小生成樹的所有邊的權(quán)值之和。

輸入樣例

9 14 1 2 4 2 3 8 3 4 7 4 5 9 5 6 10 6 7 2 7 8 1 8 9 7 2 8 11 3 9 2 7 9 6 3 6 4 4 6 14 1 8 8

輸出樣例

37 #include<iostream> #include<queue> #include<algorithm> #include<set> #include<cmath> #include<vector> #include<map> #include<stack> #include<bitset> #include<cstdio> #include<cstring> //---------------------------------Sexy operation--------------------------//#define cini(n) scanf("%d",&n) #define cinl(n) scanf("%lld",&n) #define cinc(n) scanf("%c",&n) #define cins(s) scanf("%s",s) #define coui(n) printf("%d",n) #define couc(n) printf("%c",n) #define coul(n) printf("%lld",n) #define debug(n) printf("%d_________________________________\n",n); #define speed ios_base::sync_with_stdio(0) #define file freopen("input.txt","r",stdin);freopen("output.txt","w",stdout) //-------------------------------Actual option------------------------------// #define rep(i,a,n) for(int i=a;i<=n;i++) #define per(i,n,a) for(int i=n;i>=a;i--) #define Swap(a,b) a^=b^=a^=b #define Max(a,b) (a>b?a:b) #define Min(a,b) a<b?a:b #define mem(n,x) memset(n,x,sizeof(n)) #define mp(a,b) make_pair(a,b) #define pb(n) push_back(n) #define dis(a,b,c,d) ((double)sqrt((a-c)*(a-c)+(b-d)*(b-d))) //--------------------------------constant----------------------------------//#define INF 0x3f3f3f3f #define esp 1e-9 #define PI acos(-1) using namespace std; typedef pair<int,int>PII; typedef pair<string,int>PSI; typedef long long ll; //___________________________Dividing Line__________________________________/int n,m; int father[1100000]; struct node {int x;int y;int k; }Q[1100000]; int find(int x) {if(father[x]==x)return x;return father[x]=find(father[x]); } bool cmp(node a,node b) {return a.k<b.k; } int main() {while(~scanf("%d %d",&n,&m)){int cont=0,sum=0,st=0;for(int i=0;i<m;i++){scanf("%d %d %d",&Q[i].x,&Q[i].y,&Q[i].k);cont+=Q[i].k;}sort(Q,Q+m,cmp);for(int i=1;i<=n;i++) father[i]=i;for(int i=0;i<m;i++){int tx=find(Q[i].x);int ty=find(Q[i].y);if(tx!=ty){sum+=Q[i].k;st++;father[tx]=ty;if(st==n-1)break;}}printf("%d\n",sum);}return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的51NOD 1212 无向图最小生成树的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。