POJ2349 Arctic Network 最小產生樹

來源:互聯網
上載者:User

標籤:c++

連結:

2349




題意:一個平面網路中 有M個衛星N個網站,每兩個網站之間可以用通訊器聯絡或用衛星聯絡,用通訊器聯絡的花銷和距離有關,用衛星聯絡則不需要花銷。給出每個網站的座標(x,y),求這個網路最小產生樹的最大邊。





題解:

M個衛星一共可以減去M-1條邊,通過prim演算法求出產生樹中的所有(n-1)條邊,通過排序,再減去(m-1)條權值最大的邊 即可得到最小產生樹中的最大邊。



代碼:

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;#define maxx  9999999double map[505][505];double lowdis[505],loc[505][2];int s,m,n;double ans[505];double cmp(double a,double b){    return a<b;}void prim(){    int i,j,pos,minn;    for(i=1; i<=m; i++)        lowdis[i]=map[1][i];    lowdis[1]=-1;    for(i=1; i<m; i++)    {        minn=maxx;        for(j=1; j<=m; j++)            if(lowdis[j]!=-1&&lowdis[j]<minn)                minn=lowdis[j],pos=j;        ans[s++]=lowdis[pos];        lowdis[pos]=-1;        for(j=1; j<=m; j++)            if(lowdis[j]>map[pos][j])                lowdis[j]=map[pos][j];    }    return;}int main(){    int t,i,j;    scanf("%d",&t);    while(t--)    {        s=0;        scanf("%d%d",&n,&m);        for(i=1; i<=m; i++)            scanf("%lf%lf",&loc[i][0],&loc[i][1]);        for(i=1; i<=m; i++)            for(j=i; j<=m; j++)            {                if(j==i)                    map[i][j]=maxx;                else                    map[i][j]=map[j][i]=sqrt(pow(loc[i][0]-loc[j][0],2)+pow(loc[i][1]-loc[j][1],2));            }        prim();        sort(ans,ans+s,cmp);        if(s-n<0)            cout<<0<<endl;        else            printf("%.2lf\n",ans[s-n]);    }    return 0;}


POJ2349 Arctic Network 最小產生樹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.