poj 2349 Arctic Network

來源:互聯網
上載者:User

標籤:

題目連結:http://poj.org/problem?id=2349

先求出每兩個頂點之間的距離,(注意:是double類型的),然後用普裡姆演算法(Prim)求最小產生樹。由於無線電的數目已給出m,所以需要把最小產生樹分成m份,即刪除m-1條邊,得到m個連通分量。關鍵是刪除哪些邊呢,題目要求最小的D,故把構成最小產生樹的邊從大到小排序,刪除前m-1條邊,第m條邊即所要求的最小D。

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<queue>#include<stack>#include<cmath>using namespace std;#define INF 0x3f3f3f3f#define maxn 2100int N,M;double a[maxn][maxn];int v[maxn];double ans[maxn];double dis[maxn];int k;struct point{ int x,y;}p[maxn];double dit(int i,int j){ return sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y));}void prim(int s){ for(int i=2;i<=M;i++) dis[i] = a[s][i]; dis[s] = 0; v[s] = 1; int index; for(int i=1;i<M;i++) { double Min = INF; for(int j=1;j<=M;j++) { if(!v[j]&&dis[j]<Min) { Min = dis[j]; index = j; } } ans[k++] = Min; v[index] = 1; for(int j=1;j<=M;j++) { if(dis[j]>a[index][j]) dis[j] = a[index][j]; } }}int main(){ int t; scanf("%d",&t); while(t--) { k = 0; memset(ans,0,sizeof(ans)); memset(v,0,sizeof(v)); scanf("%d%d",&N,&M); for(int i=1; i<=M; i++) scanf("%d%d",&p[i].x,&p[i].y); for(int i=1;i<M;i++) for(int j=i;j<=M;j++) { if(i == j) a[i][j] = INF; else a[i][j] = a[j][i] = dit(i,j); } prim(1); sort(ans,ans+M); printf("%.2f\n",ans[M-N]); } return 0;}

 

poj 2349 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.