poj 2349 最小生成樹應用

2021-06-22 05:41:43 字數 1159 閱讀 9515

思路:由於有s個專門的通道,我們可以先求一次最小生成樹,然後對於最小生成樹上的邊從大到小排序,前s-1條邊用s-1個衛星通道連線,那麼第s大條邊就是我們要找的最小的d了。

1 #include2 #include3 #include4 #include5 #include6

using

namespace

std;

7#define maxn 555

8#define inf 1ll<<60910

struct

nodenode[maxn];

13double

map[maxn][maxn];

14double

lowcost[maxn];

15double

dist[maxn];

16bool

mark[maxn];

17int

n,m;

1819

double cal(int i,int

j)20

2526

int cmp(const

double &p,const

double &q)

2730

31double prim(int

u0)32

38 lowcost[u0]=0

;39 mark[u0]=true;40

for(int i=1;i)47}

48if(min==inf)break

;49 mark[v]=true

;50 dist[cnt++]=lowcost[v];

51for(int k=1;k<=m;k++)55}

56}57 sort(dist,dist+cnt,cmp);

58//

for(int i=0;i59

return dist[n-1

];60}61

6263

intmain()

6477

}78 printf("

%.2lf\n

",prim(1

));79}80

return0;

81}8283

8485

86

view code

poj 2349 Kruskal 最小生成樹

題目大意 題目分析 實現 c include include include includeusing namespace std define max node 505 點的資料結構 struct point vectorgpoints 邊的資料結構 struct edge vectorgedge...

POJ 1258 最小生成樹

include include struct fiberfiber 10000 struct farmsfarms 100 void exchange struct fiber fiber,int i,int j int partition struct fiber fiber,int p,int ...

最小生成樹 poj 2485

include include include include include include define m 505 define im 505 505 2 using namespace std int r im int w im int u im int v im int p m int c...