zoj 1914 && poj 2349 Arctic Network
2015-02-16 13:30
309 查看
题意:求最小生成树,不过可以减少s-1条最大的边。
思路:求出最小生成树所有的边,按升序排好序后输出第s-1大的边。
注意:在poj上用g++交double输出要用%f,不然会错。。。
代码:
思路:求出最小生成树所有的边,按升序排好序后输出第s-1大的边。
注意:在poj上用g++交double输出要用%f,不然会错。。。
代码:
#include <iostream> #include <algorithm> #include <climits> #include <cstdio> #include <cmath> using namespace std; const int maxn = 505; struct point{ double x,y; }; int s,p; double edge[maxn][maxn]; point a[maxn]; double lowcost[maxn]; double res[maxn]; double Dis(point &a, point &b){ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } void Init(){ cin>>s>>p; for(int i = 0;i < p; i++){ cin>>a[i].x>>a[i].y; } for(int i = 0; i < p-1; i++){ for(int j = i+1; j < p; j++){ edge[i][j] = edge[j][i] = Dis(a[i],a[j]); } } } double Prim(int u){ int num = 0; for(int i = 0; i < p; i++){ lowcost[i] = edge[u][i]; } lowcost[u] = -1; for(int i = 0; i < p-1; i++){ int min = INT_MAX; int v; for(int j = 0; j < p; j++){ if(lowcost[j] != -1 && lowcost[j] < min){ min = lowcost[j]; v = j; } } res[num++] = lowcost[v]; lowcost[v] = -1; for(int j = 0; j < p; j++){ if(edge[v][j] < lowcost[j]) lowcost[j] = edge[v][j]; } } sort(res,res+num); return res[num-1-(s-1)]; } int main(){ int t; cin>>t; while(t--){ Init(); printf("%.2lf\n",Prim(0)); } return 0; }
相关文章推荐
- zoj 1914 || poj 2349 Arctic Network【最小生成树 kruskal && prim】
- ZOJ 1914 Arctic Network (POJ 2349) MST
- zoj 1914 || poj 2349 Arctic Network(Prim~)
- ZOJ 1914 Arctic Network (POJ 2349 UVA 10369) MST
- ZOJ 1914 Arctic Network (POJ 2349 UVA 10369) MST
- zoj 1914 poj 2349 Arctic Network
- 2349 poj &&uva 10369 Arctic Network【最小生成树】
- POJ 2349&&ZOJ 1914 Arctic Network(kuangbin带你飞 专题六:最小生成树)
- poj 2029 && zoj 1716 Get Many Persimmon Trees
- zoj 1914 Arctic Network
- ZOJ 1857 && POJ 2607 Fire Station【SPFA】
- zoj 1914 Arctic Network
- poj 1032 && zoj 1265 Area
- POJ 2349 Arctic Network [最小生成树]
- ZOJ 1133 & POJ 1142 Smith Numbers
- HUD 1227 && POJ 1485 && ZOJ 1196
- poj 1637 & zoj 1992 混合图的欧拉回路
- ZOJ 1683 Fishnet (POJ 1408)(计算线段交点&叉积求面积)
- POJ 3660 && NYIST 211 Cow Contest && ZOJ 1060 Sorting It All Out【Floyd算法扩展应用】
- zoj 1657 || poj 2262 Goldbach's Conjecture