日韩性视频-久久久蜜桃-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ò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 久久久久成人网 | 国产九色91 | 色午夜av| 国产内射合集颜射 | 精品人妻少妇一区二区三区 | 日本少妇作爱视频 | 尤物视频在线免费观看 | 91丝袜呻吟高潮美腿白嫩 | 四虎黄色 | 亚洲精品视频在线观看免费 | 日韩av网址在线观看 | 婷婷人体 | 欧美在线视频a | 精品999久久久一级毛片 | 五月婷婷欧美 | 国产伦一区二区 | 播播成人网| 超碰在线97观看 | 性插视频在线观看 | 森泽佳奈作品在线观看 | 播放灌醉水嫩大学生国内精品 | 深夜福利亚洲 | 2019自拍偷拍 | 一区二区三区在线视频免费观看 | 日日爽爽 | 毛片av网站| 日韩精品一区二区三区国语自制 | 亚洲aa在线| 欧美猛操| 免费看黄色三级三级 | 欧美一区二区三区成人久久片 | 黄色片中文字幕 | 欧美美女一区二区三区 | 日韩两性视频 | 亚洲91视频 | 国产91精品ai换脸 | 国产三级日本三级在线播放 | 人妻饥渴偷公乱中文字幕 | 黄色av电影网站 | 中国精品一区二区 | 精品一区二区视频在线观看 | 色老板最新地址 | av中字在线 | 国产一区二区视频在线播放 | 伊人啪啪| www成人| 国产精品久久久久9999爆乳 | 日韩在线中文字幕视频 | 亚洲a在线播放 | 99精品视频在线看 | 韩国久久精品 | 99视频久久 | 性xxxx欧美老肥妇牲乱 | 欧美午夜精品久久久久久人妖 | 韩国美女主播跳舞 | 日韩一区二区三区视频在线观看 | 99久久精品日本一区二区免费 | 制服中文字幕 | 国产乱妇无码大片在线观看 | 精品国产免费一区二区三区 | 免费在线观看a视频 | 国内精品视频 | 国产精品色婷婷99久久精品 | 成人网页 | 女性高潮视频 | 亚洲熟妇毛茸茸 | 久久久久亚洲精品 | 国产91精品ai换脸 | 久久免费精彩视频 | 永久免费在线看片 | 粉嫩av蜜桃av蜜臀av | 91精品国产色综合久久不卡98口 | 深夜福利成人 | 成人免费在线视频网站 | 成人夜晚看av | 亚洲欧美一区在线 | 国产在线中文字幕 | 久久视频网 | 精品乱码一区二区三区 | 久久精品国产欧美亚洲人人爽 | 国产成人福利 | 日本高清视频在线观看 | 在线看国产精品 | 在线观看免费高清在线观看 | 日日骚网| 黄片毛片在线免费观看 | 成人午夜精品一区二区三区 | 女王脚交玉足榨精调教 | 国产a级免费 | 国产一级一片免费播放 | h片在线免费观看 | 女厕厕露p撒尿八个少妇 | 欧美日韩国产一区二区 | 国产亚洲欧美一区 | 97福利社 | 久久国产一区 | 伊人狠狠干 | 2018av在线| 高清国产一区 |