HDU1233 還是暢通工程

來源:互聯網
上載者:User
還是暢通工程 Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 51   Accepted Submission(s) : 32 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description 某省調查鄉村交通狀況,得到的統計表中列出了任意兩村莊間的距離。省政府“暢通工程”的目標是使全省任何兩個村莊間都可以實現公路交通(但不一定有直接的公路相連,只要能間接通過公路可達即可),並要求鋪設的公路總長度為最小。請計算最小的公路總長度。
Input 測試輸入包含若干測試案例。每個測試案例的第1行給出村莊數目N ( < 100 );隨後的N(N-1)/2行對應村莊間的距離,每行給出一對正整數,分別是兩個村莊的編號,以及此兩村莊間的距離。為簡單起見,村莊從1到N編號。
當N為0時,輸入結束,該用例不被處理。
Output 對每個測試案例,在1行裡輸出最小的公路總長度。
Sample Input
31 2 11 3 22 3 441 2 11 3 41 4 12 3 32 4 23 4 50
Sample Output
35
Hint Hint
Huge input, scanf is recommended. Source

浙大電腦研究生複試上機考試-2006年

————————————————————————————————————

給出n個城市的兩兩之間資訊,求最小產生樹


#include<iostream>#include<cstdio>#include<queue>#include<cmath>#include<cstring>#include<algorithm>using namespace std;int m,n,pa[105];struct node{    int u,v,c;} s[100005];bool cmp(node a,node b){    return a.c<b.c;}void init(){    for(int i=0; i<105; i++)        pa[i]=i;}int fin(int x){    if(pa[x]!=x)    {        pa[x]=fin(pa[x]);    }    return pa[x];}void un(int a,int b){    int r1=fin(a);    int r2=fin(b);    if(r1!=r2)    {        pa[r1]=pa[r2];    }}void kruskal(){    int sum=0;    int num=0;    int flag=0;    for(int i=0; i<m; i++)    {        if(fin(s[i].u)!=fin(s[i].v))        {            un(s[i].u,s[i].v);            num++;            sum+=s[i].c;            if(num>=n-1)            {                break;            }        }    }    printf("%d\n",sum);}int main(){    int u,v,c,f;    while(scanf("%d",&n)&&n)    {        init();        m=n*(n-1)/2;        for(int i=0; i<m; i++)        {            scanf("%d%d%d",&u,&v,&c);            s[i].u=u;            s[i].v=v;            s[i].c=c;        }        sort(s,s+m,cmp);        kruskal();    }    return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.