日韩性视频-久久久蜜桃-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 | 国产在线不卡一区 | 91玉足脚交白嫩脚丫 | 快色网站 | 日本高清不卡一区 | 国家队动漫免费观看在线观看晨光 | 国产精品一级片 | 婷婷中文 | 特黄做受又粗又大又硬老头 | 国产xxxx视频 | 成人激情自拍 | 一级性爱视频 | 97热视频 | 337p亚洲精品色噜噜噜 | 一区二区三区中文视频 | 日韩三级av在线 | 天天摸天天碰天天爽天天弄 | 欧美第七页| 韩国美女黄色片 | 日本性视频网站 | 免费黄色小视频在线观看 | 国产高清免费在线 | heyzo亚洲| 伊人网站在线观看 | 激情久久视频 | 日本不卡在线播放 | 91一区二区三区在线观看 | 我把护士日出水了视频90分钟 | 免费黄色网页 | 麻豆免费在线观看 | 国产夫妻性生活视频 | 99久久久无码国产 | 婷婷综合精品 | 免费视频网站在线观看入口 | 国产精品系列在线播放 | 国产一区二区三区视频 | 国产不卡二区 | 中日韩在线播放 | 国产成人+综合亚洲+天堂 | 噜噜在线视频 | 国产一区不卡在线 | 久久久久久久久久久久久久国产 | 日日干夜夜爽 | 中文字幕校园春色 | 国产人成精品 | 亚洲AV成人精品 | 国产美女精品一区二区三区 | 国产剧情av在线 | 免费在线色视频 | 国产亚洲av综合人人澡精品 | 综合色小说| av小说在线观看 | 成人免费网址 | 欧美日韩不卡在线 | ass日本寡妇pics | 色玖玖综合 | 日本高清一区二区视频 | 男人插女人的网站 | 国产一区二区三区观看 | 免费看特级毛片 | 成人交配视频 | 一级女性全黄久久生活片免费 | 中文字幕在线视频一区二区 | 国产免费av一区二区三区 | 午夜激情在线观看视频 | 日本黄色免费网站 | 午夜欧美成人 | 精品人妻少妇嫩草av无码 | 专干中国老太婆hd | 激情小说亚洲色图 | 国产美女精品在线 | 理论片中文字幕 | 日韩精品一区二区三区高清免费 | 综合网激情 | 67194成人在线 | 国产精品第6页 | 国产三级免费观看 | 国产亚洲一区在线 | 日噜| 午夜国产福利 | 国产乱子伦精品无码专区 | 国产最新网址 | 三级小视频在线观看 | 精品久久久av | 天天干网站 | 亚洲精品少妇一区二区 | 自拍偷拍日韩精品 | 亚洲成人av在线 | 国语av在线 | 日本在线高清 | xxx在线播放 | 狠狠狠狠狠 | 天天做夜夜操 | 日本一区二区三区视频在线播放 | 日本国产中文字幕 | 午夜福利电影一区二区 | 天天摸日日干 | jizz国产| 337p亚洲精品色噜噜狠狠 |