poj 2485 Highways

來源:互聯網
上載者:User

標籤:style   http   io   for   re   c   

連結:poj 2485

題意:輸入n個城鎮相互之間的距離,輸出將n個城鎮連通費用最小的方案中修的最長的路的長度

這個也是最小產生樹的題,只不過要求的不是最小价值,而是最小產生樹中的最大權值,只需要加個判斷

比較最小產生樹每條邊的大小就行

#include<cstdio>#include<algorithm>using namespace std;int f[510],n,m;struct stu{    int a,b,c;}t[20100];int cmp(struct stu x,struct stu y){    return x.c<y.c;}int find(int x){    if(x!=f[x])        f[x]=find(f[x]);    return f[x];}int krus(){    int i,k=0,s=0,x,y;    for(i=1;i<m;i++){        x=find(t[i].a);        y=find(t[i].b);        if(x!=y){            if(t[i].c>s)           //比較最小產生樹中權值的大小                s=t[i].c;            k++;            if(k==n-1)                break;            f[x]=y;        }    }    return s;}int main(){    int T,i,j,c,s=0;    scanf("%d",&T);    while(T--){        scanf("%d",&n);        m=1;        for(i=1;i<=n;i++){            f[i]=i;            for(j=1;j<=n;j++){                scanf("%d",&c);                if(j<i){                    t[m].a=i;                    t[m].b=j;                    t[m++].c=c;                }            }        }        sort(t+1,t+m,cmp);        s=krus();        printf("%d\n",s);    }    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.