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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【LightOJ - 1123】Trail Maintenance(在线维护最小生成树,删边思维)

發布時間:2023/12/10 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【LightOJ - 1123】Trail Maintenance(在线维护最小生成树,删边思维) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

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個點,m個邊順序添加,每次添加,要求在線輸出MST的權值,如果湊不出MST就輸出-1。

解題報告:

? 直接暴力肯定會超時。

? 考慮到n只有200:因為最差情況:大于n條邊的時候,肯定會生成一個環,那么最終生成環的這一條邊,肯定是我們最后遍歷到的,而恰好肯定是環內權值最大的(因為按邊權排序了之后再遍歷的。),所以這一條邊可以直接扔掉(簡化操作就是記錄一下這個邊的位置,然后遍歷結束后把最后一條邊直接賦值過來。)

? 下面給出簡要證明:為什么這一條邊e一定不會要,也就是,不管后面的邊如何添加的,這一條邊一定沒用了(也就是一定不會是MST中的邊):因為每次加邊操作都是只加一條,所以最多替換一條邊,考慮每次替換邊,都是去掉一條邊w1,換上一條邊w2,那么這個替換執行 當且僅當w1>w2,也就是新邊的權值肯定要更小,我們才考慮替換他。由上一段的分析得知,這條邊e的權值we>w1,所以這一條邊we>w2,所以肯定這條邊沒用了。因為他只要是棵樹,我們不需要看邊選的是啥,因為可以確定的是,樹上的點一定就是那一些,所以具體哪些邊沒影響。至此,我們可以放心大膽的刪掉這條邊了。

? 這次是體會到了常數的可怕。。。把自定義比較函數放在結構體內部確實是快(160ms級別和240ms級別的區別。)另外getf也是這樣,uv直接變成祖先節點才能沖進200ms,不然就是200多ms。不過如果不卡并查集的話問題就不大。

? ?另外對于這題,不能加cnt==n-1則break。因為可能需要刪除的邊在后面??傊@一點可以被卡。所以要注意。不過其實最多穩定在200條邊,所以加不加這個優化影響不大。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; int f[222]; struct Edge {int u,v,w;bool operator<(const Edge b) const {return w < b.w;} } e[MAX]; int tot,n,m; int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v]); } void init() {for(int i = 1; i<=n; i++) f[i] = i; } int klu() {sort(e+1,e+tot+1);int cnt = 0,pos=-1,res = 0;for(int i = 1; i<=tot; i++) {int u = getf(e[i].u),v = getf(e[i].v);if(u==v) pos = i;else {f[v]=u;res += e[i].w;cnt++;} // if(cnt == n-1) break; } if(pos != -1) {e[pos] = e[tot];tot--;}if(cnt == n-1) return res;else return -1; } int main() {int t,iCase = 0;scanf("%d",&t);while(t--) {printf("Case %d:\n",++iCase);tot=0;scanf("%d%d",&n,&m);for(int a,b,c,i = 1; i<=m; i++) {scanf("%d%d%d",&a,&b,&c);e[++tot].u = a;e[tot].v = b;e[tot].w = c;init();printf("%d\n",klu());}}return 0 ; }

換了一種優化反而更慢(161ms)注意這樣寫的話要提前賦值tot給tott,并且遍歷邊的時候還是遍歷到tott。因為你后面的那些邊雖然可能要刪除,但是前提是在你找到MST的時候,你在線就tot--了,可能人家本來就是第tot條邊湊出MST,但是你這樣操作就輸出-1了。

總結一下這樣慢的原因,其實沒必要加那個cnt==n-1就break。因為本來邊就不多,動態穩定在n-1附近,,所以沒必要,,因為反而增加了很多次if判斷,所以耗時了。

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; int f[222]; struct Edge {int u,v,w;bool operator<(const Edge b) const {return w < b.w;} } e[MAX]; int tot,n,m; int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v]); } void init() {for(int i = 1; i<=n; i++) f[i] = i; } int klu() {sort(e+1,e+tot+1);int cnt = 0,pos=-1,res = 0,tott=tot;for(int i = 1; i<=tott; i++) {int u = getf(e[i].u),v = getf(e[i].v);if(u==v) {e[i] = e[tot];tot--;}else {f[v]=u;res += e[i].w;cnt++;}if(cnt == n-1) {tot=i;break; }} // if(pos != -1) { // e[pos] = e[tot]; // tot--; // }if(cnt == n-1) return res;else return -1; } int main() {int t,iCase = 0;scanf("%d",&t);while(t--) {printf("Case %d:\n",++iCase);tot=0;scanf("%d%d",&n,&m);for(int a,b,c,i = 1; i<=m; i++) {scanf("%d%d%d",&a,&b,&c);e[++tot].u = a;e[tot].v = b;e[tot].w = c;init();printf("%d\n",klu());}}return 0 ; }

?

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的【LightOJ - 1123】Trail Maintenance(在线维护最小生成树,删边思维)的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 亚洲av毛片 | 亚洲成年人在线观看 | 天天色棕合合合合合合合 | 少女情窦初开的第4集在线观看 | 深夜成人在线观看 | 亚洲色图综合网 | 正在播放老肥熟妇露脸 | 日本h视频在线观看 | 亚洲在线视频 | aaa国产精品 | 国产ts在线视频 | 日本午夜三级 | 国产精品视频一区在线观看 | 国产群p| 国产一区二区在线观看免费 | 亚洲精品在线视频免费观看 | 亚洲视频精品一区 | 国产精品麻豆果冻传媒在线播放 | 国产婷婷色综合av蜜臀av | 欧美在线精品一区二区三区 | 一区二区三区www | 夜夜狠| 日本少妇一级 | 亚洲天堂视频网站 | 婷婷五月在线视频 | 美女被到爽高潮视频 | 淫综合网| 天天综合中文字幕 | 欧美视频一区二区三区四区在线观看 | 在线播放小视频 | 97人妻精品一区二区三区动漫 | 在线一区二区三区四区五区 | 日韩影视一区二区三区 | 欧美69av | 正在播放超嫩在线播放 | 成年人视频在线 | 亚洲色精品三区二区一区 | 久久久久亚洲av无码专区喷水 | 国产一区二区中文字幕 | 男人天堂a在线 | 蜜桃传媒| 久久久久久久极品内射 | 久久精品国产一区二区电影 | 在线播放黄色网址 | 91精品啪在线观看国产 | 日本在线播放 | 国产在线观看免费播放 | 亚洲精品国产成人久久av盗摄 | 三级不卡| 一级二级毛片 | 精品免费囯产一区二区三区 | 黄色片免费观看 | 夜晚福利视频 | 国产私拍视频 | 日韩一区二区中文字幕 | 苏晴忘穿内裤坐公交车被揉到视频 | 天堂中文字幕免费一区 | 亚洲一区二区三区免费在线观看 | 少妇做爰免费视看片 | 亚洲少妇色| 国产美女喷水 | 亚洲欧洲国产综合 | 中字幕视频在线永久在线观看免费 | 久久久国产精品久久久 | 成人欧美日韩 | 一级黄色片网站 | wwwxxx亚洲| 天天色综合久久 | 伊人五月综合 | 人人澡人人澡 | 三级在线网站 | 免费网站成人 | 亚洲欧美日韩成人在线 | 99在线国产 | 天天干少妇 | 国产91免费在线观看 | 69视频一区二区三区 | 狠狠看| 精品欧美一区二区三区免费观看 | 日韩电影在线观看一区二区 | 午夜剧场免费在线观看 | 亚洲精品久久久久 | 国产精品久久久久aaaa | 99国产精品人妻噜啊噜 | 亚洲免费大片 | 欧美高清hd19 | 毛片在线免费播放 | 丁香花高清在线 | 少妇粉嫩小泬喷水视频www | 精品久久久久久亚洲精品 | av免费看在线 | 男生操男生网站 | 黑料福利 | 国产社区在线 | 精品国产乱码久久久久 | 国产一区二区三区小说 | 91蝌蚪少妇| 性一交一乱一透一a级 | 波多野结衣伦理 |