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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【POJ - 1789】【ZOJ - 2158】【SCU - 1832】Truck History (最小生成树)

發(fā)布時間:2023/12/10 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【POJ - 1789】【ZOJ - 2158】【SCU - 1832】Truck History (最小生成树) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題干:

Description

Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for bricks. The company has its own code describing each type of a truck. The code is simply a string of exactly seven lowercase letters (each letter on each position has a very special meaning but that is unimportant for this task). At the beginning of company's history, just a single truck type was used but later other types were derived from it, then from the new types another types were derived, and so on.?

Today, ACM is rich enough to pay historians to study its history. One thing historians tried to find out is so called derivation plan -- i.e. how the truck types were derived. They defined the distance of truck types as the number of positions with different letters in truck type codes. They also assumed that each truck type was derived from exactly one other truck type (except for the first truck type which was not derived from any other type). The quality of a derivation plan was then defined as?

1/Σ(to,td)d(to,td)


where the sum goes over all pairs of types in the derivation plan such that to?is the original type and td?the type derived from it and d(to,td) is the distance of the types.?
Since historians failed, you are to write a program to help them. Given the codes of truck types, your program should find the highest possible quality of a derivation plan.?

Input

The input consists of several test cases. Each test case begins with a line containing the number of truck types, N, 2 <= N <= 2 000. Each of the following N lines of input contains one truck type code (a string of seven lowercase letters). You may assume that the codes uniquely describe the trucks, i.e., no two of these N lines are the same. The input is terminated with zero at the place of number of truck types.

Output

For each test case, your program should output the text "The highest possible quality is 1/Q.", where 1/Q is the quality of the best derivation plan.

Sample Input

4 aaaaaaa baaaaaa abaaaaa aabaaaa 0

Sample Output

The highest possible quality is 1/3.

Source

CTU Open 2003

?

題意:

? ? ??給出 n 種卡車,每種卡車的類型是由七個字符組成的,一種卡車可以從另一種卡車派生而來,所需要的代價是兩種卡車類型不同的字符個數(shù),求出這 n 種卡車派生的最小代價, n 種車有一 種是開始就已經(jīng)有的,其余的 n-1 種是派生出來的。

? ? ? 用一個7位的string代表一個編號,兩個編號之間的distance代表這兩個編號之間不同字母的個數(shù)。一個編號只能由另一個編號“衍生”出來,代價是這兩個編號之間相應的distance,現(xiàn)在要找出一個“衍生”方案,使得總代價最小,也就是distance之和最小。

解題報告:

讀入字符串后先將字符串處理一下,并將權值算好后 入node結構體,然后對結構體排序等等 就是最小生成樹裸題了。

ac代碼:

#include<iostream> #include<cstdio> #include<algorithm> using namespace std; const int MAX = 10000 + 5 ;int f[MAX+5]; int n; char s[MAX + 5][10]; int ans; int top; struct Node {int u,v,dist; } node[50000000 + 5];int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v] ); } void merge(int u, int v) {int t1 = getf(u );int t2 = getf(v );if(t1 != t2) {f[t2] =t1;}} bool cmp(const Node & a,const Node & b) {return a.dist<b.dist; } int main() {int sum;while(scanf("%d",&n) && n ) {for(int i = 0 ; i<=n; i++) {f[i]=i;}ans = 0;top = 0;for(int i = 1; i<=n; i++) {scanf("%s",s[i]);}for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {sum = 0;for(int k = 0 ; k<7; k++) {if(s[i][k]!=s[j][k] ) sum++;}++top;node[top].u = i;node[top].v = j;node[top].dist = sum; // merge(i,j);}}sort(node+1,node+top+1,cmp);int m=0;//代表邊數(shù) for(int i = 1; i<=top; i++) {if(getf(node[i].u) != getf(node[i].v) ) {ans +=node[i].dist;merge(node[i].u,node[i].v);m++;if(m== n-1) break;}}printf("The highest possible quality is 1/%d.\n",ans); }return 0 ;}

總結:

? ? 1.這波太難受了啊。 ?

? ? ? ? ? 1RE。因為讀字符串的時候i<=MAX了!這樣是不對的,因為開的數(shù)組大小是MAX所以這里不能小于等于!所以最好就是讀數(shù)據(jù)的時候寫<=n,別太洋氣的寫<=MAX,如果要這么寫的話,你開數(shù)組就得寫MAX+c了!并且發(fā)現(xiàn)top和ans沒有初始化。

? ? ? ? ? ?2RE。因為數(shù)組開小了。node數(shù)組應該開MAX*MAX+c!

? ? ? ? ? ?3WA。因為輸出格式不對。。。。少了個句點你能信。、。。

? ? ? ? ? ?4AC ? ?

? ?2. 我們來算一下時間復雜度吧。你在讀數(shù)據(jù)后遍歷的時候,?兩層for循環(huán)到n,所以時間復雜度是n^2的還可以。加一個克魯斯科爾 排序 o(eloge)+遍歷o(e)的。

總結

以上是生活随笔為你收集整理的【POJ - 1789】【ZOJ - 2158】【SCU - 1832】Truck History (最小生成树)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 色香欲综合网 | 在线观看av网 | 中午字幕在线观看 | 国产视频在线观看视频 | 日韩欧美在线观看一区二区三区 | 国内免费毛片 | 亚洲精品v日韩精品 | 麻豆人妻少妇精品无码专区 | 精品成人免费一区二区在线播放 | 亚洲一二三区在线 | 男人吃奶视频 | 不卡中文字幕在线 | 国产精品成人国产乱一区 | 超碰777| 国产在线欧美日韩 | 亚洲天堂中文字幕在线 | 日本韩国在线观看 | 五月天婷婷色综合 | 久久久.www | 日韩黄色一级 | 韩国毛片一区二区三区 | 久久色网站| 1024中文字幕| 久久久久久久久久久av | 亚洲性猛交xxxx乱大交 | 中文字幕 国产精品 | 久色99| 日韩av一区二区三区在线 | 啪啪福利社 | 国产96在线 | www.天天射 | 国产在线一区二区三区四区 | 日本在线一区二区 | 激情一区二区三区 | 精品一区二区久久久久蜜桃 | 欧美人与动物xxx | 国产视频手机在线 | 亚洲一区二区自偷自拍 | 美女视频黄是免费 | 成人免费视频网站在线看 | 91精品久久人妻一区二区夜夜夜 | 久久亚洲伊人 | 肉丝美足丝袜一区二区三区四 | 日韩中文免费 | 亚洲精品在线不卡 | 国产素人在线观看 | 无码精品国产一区二区三区免费 | 黄色大片日本 | 亚洲成年人网站在线观看 | 四虎永久在线观看 | 99热这里只有精品在线观看 | 777奇米视频 | 天美麻花果冻视频大全英文版 | 国产毛片久久久久久 | 亚洲小说春色综合另类 | 久久综合鬼色 | 女人扒开腿免费视频app | 中文字幕第一页在线视频 | 欧美性猛交ⅹxxx乱大交3 | 黄色网入口 | 日韩一区二区影院 | 国产午夜电影在线观看 | 成人av网站在线观看 | h文在线观看 | 亚洲春色在线观看 | 日韩欧美一区二区三区免费观看 | 一边摸内裤一边吻胸 | 精品人妻一区二区三区四区久久 | 蜜桃视频在线观看www | 男女互操| av在线播放免费 | 亚洲欧美日韩中文字幕在线观看 | 91精品国产亚洲 | 成人av高清在线观看 | 成人黄色三级 | 日本伊人久久 | 五月综合色婷婷 | 天天爽 | 偷看洗澡一二三区美女 | 国产精品99久久久久久动医院 | 天天上天天干 | 亚洲精品国产成人无码 | 国产精品无码久久久久一区二区 | 国产精品va无码一区二区 | 天天插天天爽 | www五月天com| av在线精品 | 国产网红主播精品av | 亚洲超碰在线观看 | 国产三级在线播放 | 亚洲激情六月 | 久久久免费看片 | 亚洲性猛交 | 91精品视频在线看 | 黄色一级视频免费看 | 制服丝袜av一区二区三区下载 | 在线视频免费播放 | 伊人中文字幕在线 | 成人中文字幕+乱码+中文字幕 |