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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【BZOJ】3390: [Usaco2004 Dec]Bad Cowtractors牛的报复(kruskal)

發布時間:2025/5/22 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【BZOJ】3390: [Usaco2004 Dec]Bad Cowtractors牛的报复(kruskal) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://www.lydsy.com/JudgeOnline/problem.php?id=3390

。。

#include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <algorithm> #include <queue> using namespace std; #define rep(i, n) for(int i=0; i<(n); ++i) #define for1(i,a,n) for(int i=(a);i<=(n);++i) #define for2(i,a,n) for(int i=(a);i<(n);++i) #define for3(i,a,n) for(int i=(a);i>=(n);--i) #define for4(i,a,n) for(int i=(a);i>(n);--i) #define CC(i,a) memset(i,a,sizeof(i)) #define read(a) a=getint() #define print(a) printf("%d", a) #define dbg(x) cout << #x << " = " << x << endl #define printarr2(a, b, c) for1(i, 1, b) { for1(j, 1, c) cout << a[i][j]; cout << endl; } #define printarr1(a, b) for1(i, 1, b) cout << a[i]; cout << endl inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; } inline const int max(const int &a, const int &b) { return a>b?a:b; } inline const int min(const int &a, const int &b) { return a<b?a:b; }const int N=20005; int p[N], n, m, ans; struct ED { int x, y, w; }e[N]; bool cmp(const ED &a, const ED &b) { return a.w>b.w; } const int ifind(const int &x) { return x==p[x]?x:p[x]=ifind(p[x]); }int main() {read(n); read(m);for1(i, 1, m) read(e[i].x), read(e[i].y), read(e[i].w);sort(e+1, e+1+m, cmp);int tot=0;for1(i, 1, n) p[i]=i;for1(i, 1, m) {int fx=ifind(e[i].x), fy=ifind(e[i].y);if(fx!=fy) {p[fx]=fy;ans+=e[i].w;++tot;}}if(tot<n-1) puts("-1");else print(ans);return 0; }

?

?


?

?

Description

奶牛貝茜被雇去建設N(2≤N≤1000)個牛棚間的互聯網.她已經勘探出M(1≤M≤ 20000)條可建的線路,每條線路連接兩個牛棚,而且會苞費 C(1≤C≤100000).農夫約翰吝嗇得很,他希望建設費用最少甚至他都不想給貝茜工錢.?貝茜得知工錢要告吹,決定報復.她打算選擇建一些線路,把 所有牛棚連接在一起,讓約翰花費最大.但是她不能造出環來,這樣約翰就會發現.

Input

第1行:N,M. 第2到M+1行:三個整數,表示一條可能線路的兩個端點和費用.

Output

最大的花費.如果不能建成合理的線路,就輸出-1

Sample Input

5 8
1 2 3
1 3 7
2 3 10
2 4 4
2 5 8
3 4 6
3 5 2
4 5 17

Sample Output

42

連接4和5,2和5,2和3,1和3,花費17+8+10+7=42

HINT

Source

Silver

總結

以上是生活随笔為你收集整理的【BZOJ】3390: [Usaco2004 Dec]Bad Cowtractors牛的报复(kruskal)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。