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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Lightoj 1123 - Trail Maintenance(最小增量生成树)

發(fā)布時(shí)間:2025/6/17 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Lightoj 1123 - Trail Maintenance(最小增量生成树) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目鏈接 https://vjudge.net/problem/LightOJ-1123

Tigers in the Sunderbans wish to travel freely among the N fields (numbered from 1 to N), even though they are separated by trees. The tigers wish to maintain trails between pairs of fields so that they can travel from any field to any other field using the maintained trails. Tigers may travel along a maintained trail in either direction.

The tigers do not build trails. Instead, they maintain deer trails that they have discovered. On any week, they can choose to maintain any or all of the deer animal trails they know about. Always curious, the tigers discover one new deer trail at the beginning of each week. They must then decide the set of trails to maintain for that week so that they can travel from any field to any other field. Tigers can only use trails which they are currently maintaining.

The tigers always want to minimize the total length of trail they must maintain. The tigers can choose to maintain any subset of the deer trails they know about, regardless of which trails were maintained the previous week. Deer trails (even when maintained) are never straight. Two trails that connect the same two fields might have different lengths. While two trails might cross, tigers are so focused; they refuse to switch trails except when they are in a field. At the beginning of each week, the tigers will describe the deer trail they discovered. Your program must then output the minimum total length of trail the tigers must maintain that week so that they can travel from any field to any other field, if there is such a set of trails.

Input
Input starts with an integer T (≤ 25), denoting the number of test cases.

Each case starts with two integers N (1 ≤ N ≤ 200) and W. W is the number of weeks the program will cover (1 ≤ W ≤ 6000).

Each of the next W lines will contain three integers describing the trail the tigers found that week. The first two numbers denote the end points (filed numbers) and the third number denotes the length of the trail (1 to 10000). No trail has the same field as both of its end points.

Output
For each case, print the case number in a line. Then for every week, output a single line with the minimum total length of trail the tigers must maintain so that they can travel from any field to any other field. If no set of trails allows the tigers to travel from any field to any other field, output “-1”.

Sample Input
1
4 6
1 2 10
1 3 8
3 2 3
1 4 3
1 3 6
2 1 2
Sample Output
Case 1:
-1
-1
-1
14
12
8

【題意】
從包含n個(gè)點(diǎn)的空?qǐng)D開始,依次加入m條帶權(quán)無向邊,每加入一條邊就立即輸出當(dāng)前圖中最小生成樹的權(quán)值,如果還未連通則輸出-1.

【思路】
題目描述即最小增量生成樹的定義,如果在每次加邊后都調(diào)用一次kruscal算法求解,那么復(fù)雜度是O(m^2logn),難以承受。正確的做法是根據(jù)生成樹的回路性質(zhì)刪除一些邊,回路性質(zhì)即圖中任意一條回路上邊權(quán)最大的邊一定不在最小生成樹當(dāng)中,所以我們還是仿照kruscal算法的過程,從空?qǐng)D開始不斷加邊,假設(shè)加入若干條邊以后求出了當(dāng)前的最小生成樹,那么下一次加邊后一定會(huì)構(gòu)成一個(gè)環(huán),我們只需要把這個(gè)環(huán)上邊權(quán)最大的邊刪除就會(huì)得到新的最小生成樹,怎么去找邊權(quán)最大的那條邊呢?其實(shí)根本不用找,因?yàn)樵谡{(diào)用kruscal算法之前我們肯定會(huì)對(duì)邊集按照邊權(quán)升序排序的,當(dāng)新加入的這條邊構(gòu)成環(huán)路時(shí),這條邊肯定是環(huán)路中權(quán)值最大的邊,也是我們要?jiǎng)h掉的邊,因?yàn)槊看尾僮髯疃鄤h一條邊,所以直接用最后一條邊覆蓋掉當(dāng)前邊即可,下一次調(diào)用前的sort會(huì)重新對(duì)邊集排序。

#include<bits/stdc++.h> using namespace std;const int maxn = 220; const int maxm = 6050;int n, m, cnt; int par[maxn];struct Edge {int from, to, dist;Edge(int f = 0, int t = 0, int d = 0) :from(f), to(t), dist(d) {} }edges[maxm];bool cmp(Edge x, Edge y) { return x.dist < y.dist; } int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); }int kruscal() {int cpy = cnt, num = 0, ans = 0;for (int i = 0; i <= n; ++i) par[i] = i;for (int i = 0; i < cpy; ++i) {int x = find(edges[i].from);int y = find(edges[i].to);if (x == y) {edges[i] = edges[cnt - 1];--cnt;continue;}++num;par[x] = y;ans += edges[i].dist;}return num < n - 1 ? -1 : ans; }int main() {int t;scanf("%d", &t);for (int kase = 1; kase <= t; ++kase) {scanf("%d%d", &n, &m);printf("Case %d:\n", kase);cnt = 0;for (int i = 0; i < m; ++i) {int u, v, c;scanf("%d%d%d", &u, &v, &c);edges[cnt++] = Edge(u, v, c);sort(edges, edges + cnt, cmp);int ans = kruscal();printf("%d\n", ans);}}return 0; }

轉(zhuǎn)載于:https://www.cnblogs.com/wafish/p/10465410.html

總結(jié)

以上是生活随笔為你收集整理的Lightoj 1123 - Trail Maintenance(最小增量生成树)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

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