zoj 1203 Swordfish

來源:互聯網
上載者:User

標籤:style   blog   http   資料   2014   io   

連結:zoj 1203

題意:輸入n個城市的座標,輸出使n個城市連通的最短路線的長度

分析:通過座標可以將兩兩之間的長度即權值算出,再用最小產生樹的演算法

不過這個題要注意輸出時的格式問題,兩組資料間要空一行

#include<cstdio>#include<cmath>#include<algorithm>using namespace std;int f[110],n,m;struct stu{    int a,b;    double c;}t[5000];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];}double krus(){    int i,k=0,x,y;    double s;    for(i=1;i<m;i++){        x=find(t[i].a);        y=find(t[i].b);        if(x!=y){            s+=t[i].c;            k++;            if(k==n-1)                break;            f[x]=y;        }    }    return s;}int main(){    int i,j,k=0;    double s,x[110],y[110];    while(scanf("%d",&n)!=EOF){        if(n==0)            break;        if(k>=1)            printf("\n");        k++;        for(i=1;i<=n;i++){            scanf("%lf%lf",&x[i],&y[i]);            f[i]=i;        }        m=1;        for(i=1;i<=n;i++)            for(j=1;j<i;j++){                t[m].a=i;                t[m].b=j;                t[m++].c=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));       //計算兩兩間的距離            }        sort(t+1,t+m,cmp);        s=krus();        printf("Case #%d:\nThe minimal distance is: %.2lf\n",k,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.