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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【HDU - 1301】Jungle Roads(并查集+最小生成树)(内附最小生成树两种算法 克鲁斯特尔算法amp;amp;普里姆算法)

發(fā)布時(shí)間:2023/12/10 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【HDU - 1301】Jungle Roads(并查集+最小生成树)(内附最小生成树两种算法 克鲁斯特尔算法amp;amp;普里姆算法) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題干:

Jungle Roads
Time Limit: 2000/1000 MS (Java/Others) ? ?Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5505 ? ?Accepted Submission(s): 3976
Problem Description

The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems.?


The input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to other villages (before or after in the alphabet). In the sample input below, the first data set goes with the map above.?

?

The output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit.?

?

Sample Input
9
A ?2 ?B ?12 ?I ?25
B ?3 ?C ?10 ?H ?40 ?I ?8
C ?2 ?D ?18 ?G ?55
D ?1 ?E ?44
E ?2 ?F ?60 ?G ?38
F ?0
G ?1 ?H ?35
H ?1 ?I ?35
3
A ?2 ?B ?10 ?C ?40
B ?1 ?C ?20
0
?


Sample Output
216

30

解題報(bào)告:

? ? ? ? ? ? 最小生成樹再來一發(fā)。

ac代碼:

#include<cstdio> #include<algorithm> #include<iostream> using namespace std; struct Node {int u,v;int w;bool operator< ( const Node & b) const{return w<b.w;} } node[105];int f[100],n,cur,cnt,ans; void init() {cnt = 0;cur = 0;ans = 0;for (int i = 0; i <= 27; i++) {f[i] = i; } } int getf(int u) {return u==f[u] ? u : f[u]=getf(f[u]); } void merge(int u,int v) {int t1,t2;t1=getf(u);t2=getf(v);if(t1!=t2) f[t2]=t1; } void input() {char ch[5];int a;for (int i = 1; i <= n - 1; i++) {scanf("%s %d", ch, &a);int u = ch[0] - 'A' + 1;for (int j = 1; j <= a; j++) {int b;scanf(" %s %d", ch, &b);int v = ch[0] - 'A' + 1;node[cnt].u = u;node[cnt].v = v;node[cnt].w = b; cnt++;}} }int main() {while (scanf("%d", &n) ) {getchar();//如果不用getchar 那input中就用%s別用%c if (n==0) break;init();input();sort(node,node+cnt);for(int i = 0; i<cnt; i++) {if(getf(node[i].u) != getf(node[i].v) ) {merge(node[i].u,node[i].v);ans+=node[i].w;cur++;}if(cur==n-1) {break;}}printf("%d\n", ans);}return 0; }

貼克魯斯特爾算法ac代碼:(常用!ac代碼1就是用此法)

//hdu-1301-Jungle Roads(克魯斯卡爾) //題目大意:給出個(gè)村莊之間的道路及其維修費(fèi);從中選取一些路使道路的維修費(fèi)最小且保證各村莊 //之間道路暢通; //解題思路: //本題最核心的還是構(gòu)圖,由于輸入的村莊用字母代替這就需要在構(gòu)圖中將字母替換成數(shù)字依次給每個(gè)村莊編號(hào); //需要注意的由于輸入的有字符型數(shù)據(jù),這就要加一些 getchar()來吸收掉中間的換行空格啥的; //初始化 per[n] 數(shù)組一定要注意,把所有村莊都初始化就行啦; //具體如下: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int per[30]; int n,m; struct fun{ //定義結(jié)構(gòu)體數(shù)組road[100],用來存放起點(diǎn)、終點(diǎn)和距離 int s;int e;int p; }road[100]; int cmp(fun x,fun y) {return x.p < y.p; } void init() //初始化 per 數(shù)組 ;注意初始完 n個(gè)村莊即可; {int d;for(d=1;d < = n;d++)per[d]=d; } int find(int x) {int r=x;while(r!=per[r])r=per[r];return r; } bool link(int x,int y) {int fx=find(x),fy=find(y);if(fx!=fy){per[fx]=fy;return true;}return false; } int main() {int j,k,l,sum,i,cnt,a1,a2;char str1,str2;while(scanf("%d",&n)&&(n!=0)){getchar();memset(road,0,sizeof(road));memset(per,0,sizeof(per));init();for(i=1,cnt=1;i < n;i++) //輸入數(shù)據(jù),并構(gòu)圖; {scanf("%c%d",&str1,&m);for(j=1;j<=m;j++){getchar();scanf("%c%d",&str2,&l);road[cnt].s=str1-'A'+1; //將字母編號(hào)轉(zhuǎn)化為數(shù)字編號(hào)并存入結(jié)構(gòu)體數(shù)組中 road[cnt].e=str2-'A'+1;road[cnt].p=l;cnt++;}getchar();} //構(gòu)圖完成,剩下的就是克魯斯卡爾啦; sort(road,road+cnt,cmp);sum=0;for(j=1;j<=cnt;j++){if(link(road[j].s,road[j].e))sum+=road[j].p;}printf("%d\n",sum);}return 0; }

貼Prim算法ac代碼:

//hdu-1301(普里姆算法) //本題用普里姆算法求最小生成樹直接用普里姆的思想構(gòu)造函數(shù)即可; //需要注意的是本題再輸入數(shù)據(jù)的是時(shí)候要先對(duì) map[1000][1000]中的前 n*n //個(gè)元素初始化為 一個(gè)與 min相同或大于它的數(shù);例如,0xfffffff即可; //(n是結(jié)點(diǎn)的個(gè)數(shù),由于需要在 n*n 大小的矩陣中構(gòu)圖,所以初始化 n*n 就行); //為什么要對(duì) map數(shù)組初始化呢?一是由于在prim函數(shù)中 map要對(duì)s[1000]數(shù)組賦初值;如果不對(duì)map初始化; //那么,s[]數(shù)組中沒有數(shù)據(jù)的就一直為零,與下面的 min比較時(shí),min是永遠(yuǎn)大于它們的;這樣得到的min就一直為零 //最終導(dǎo)致sum為零;還有,不對(duì)map按上述初始化,在更新時(shí)候也無法完成 ; #include<stdio.h> #include<string.h> #define N 0xfffff int map[1000][1000]; int n,m,sum,cnt; void input() {char c1,c2;int i,j,x,y,price;cnt=0;for(i=1;i<=n;i++) //對(duì) map數(shù)組初始化為 N ;{for(j=1;j<=n;j++)map[i][j]=N;}for(i=1;i<n;i++) //輸入數(shù)據(jù),并構(gòu)圖; {scanf("%c%d",&c1,&m);x=c1-'A'+1;for(j=1;j<=m;j++){getchar();scanf("%c%d",&c2,&price);y=c2-'A'+1;map[x][y]=map[y][x]=price;}getchar();} } void prim() //普里姆; {int i,j,k,min,v,a,l;int s[1000],vis[1000];memset(vis,0,sizeof(vis));for(i=1;i<=n;i++) s[i]=map[1][i]; //初始化 s 數(shù)組; s[1]=0; //本身到本身的距離為零 vis[1]=1; //標(biāo)記第一個(gè)點(diǎn); sum=0;for(v=1;v<n;v++){min=N;k=1;for(j=1;j<=n;j++)if(!vis[j]&&min>s[j]){min=s[j]; //尋找當(dāng)前最小權(quán)值; k=j;}vis[k]=1; //標(biāo)記頂點(diǎn)k,表示k已連上樹; sum+=min; //將當(dāng)前最小權(quán)值累加到sum中; for(a=1;a<=n;a++){if(!vis[a]&&s[a]>map[k][a]) //標(biāo)記當(dāng)前最小權(quán)值的頂點(diǎn)后,更新s[] 數(shù)組中的權(quán)值 ; s[a]=map[k][a];}}printf("%d\n",sum); } int main() {char c1,c2;int i,j,x,y,price;while(scanf("%d",&n)&&(n!=0)){getchar();input();prim();}return 0; }

?

總結(jié):

? ? ? 有的時(shí)候用%s讀字符,不是因?yàn)閯e的,就是因?yàn)楹锰幚砜崭?#xff0c;回車這些煩人的東西!!這件事情告訴我們,少用%c多用%s,少用char多用char[ ] !!

?

總結(jié)

以上是生活随笔為你收集整理的【HDU - 1301】Jungle Roads(并查集+最小生成树)(内附最小生成树两种算法 克鲁斯特尔算法amp;amp;普里姆算法)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 日韩精品一二 | 成人无码久久久久毛片 | 日韩第四页 | 中文综合网 | 国产亚洲综合精品 | 波多野结衣中文一区 | 欧美性大战久久久久久久 | 蜜桃久久久aaaa成人网一区 | 国产精品久久久久久久久久久久久久久 | 日本高清黄色 | 成人av影院 | 国产精品久久91 | 色噜噜成人| 91视频www| 亚洲天堂男人的天堂 | 免费在线观看你懂的 | 成人免费黄色大片 | 欧美日韩黑人 | 久久精品亚洲一区二区 | 日本精品免费一区二区三区 | 在线成人日韩 | 自拍中文字幕 | 国产制服在线 | 日本黄色动态图 | 国产美女三级无套内谢 | 免费毛片av| 亚洲精品成人久久 | 久久久久九九九九 | 日本欧美久久久久免费播放网 | 精品一区二区三区四区五区六区 | 狠狠干2017| 成人三级晚上看 | 强行挺进皇后紧窄湿润小说 | 中文亚洲av片在线观看 | 中文字幕人成乱码熟女香港 | 中文字幕一区二区av | 精品综合久久 | 一级黄色片看看 | 亚洲free性xxxx护士白浆 | 一区二区三区精品在线 | av成人亚洲 | 久久久久久久久久久久久久久久久久久久 | 久久人人妻人人人人妻性色av | 亚洲熟妇无码乱子av电影 | 成人天堂网| 久草免费在线视频观看 | hd极品free性xxx护士 | 国产高清片 | 日韩午夜片 | 色人阁av| 亚洲天堂视频一区 | 黄色三级网站 | 久久99亚洲精品 | 韩日成人 | 少妇特黄一区二区 | 色91| 激情内射亚洲一区二区三区爱妻 | 精品国产免费视频 | 日韩资源网| 亚洲视频在线播放 | 免费一级特黄特色毛片久久看 | 欧美日韩在线视频一区二区三区 | 美女扒开腿男人爽桶 | 亚洲v欧美v另类v综合v日韩v | 亚洲手机看片 | 99久久久久久久久久 | 欧美综合在线视频 | jizz免费| 另类小说色 | 天堂精品视频 | 色综合久久久久综合体桃花网 | 手机在线不卡av | 手机av片| 51成人精品网站 | 999福利视频 | 九九精品在线观看 | 番号动态图 | 国产精品99一区二区三区 | 亚洲天堂视频一区 | 国产精品一区二区免费视频 | 久久久久亚洲AV成人无在 | 日本精品在线 | 91免费版在线看 | 自拍偷拍视频网 | 在线观看国产日韩 | 不良视频在线观看 | 国产女主播在线一区二区 | 国产精品成人电影在线观看 | 一级片麻豆 | 日本大乳奶做爰 | www一级片| 久久综合加勒比 | 日韩中文在线字幕 | 国产精品成人免费看片 | 91综合网| 青青操在线观看 | 天天操操| 熟女av一区二区 | 91热精品|