-
Arctic Network
- POJ - 2349
- 有s个卫星和p个村庄,将p个村庄连通,最少需要p-1条边,现在s个卫星可以替代s-1条边,剩下的边的权值,只要在d权值内都可以连通,求剩下p-s条边连通的最小代价d是多少? 思路:最小生成树,求第p-s条最小边。计算出所有村庄线路的权值以后,建最小生成树,将每一次的权值存入数组,最后升序排序,输出第p-s个数。 总结:稠密图用prime算法更加高效。稀疏图用kruskal
- Any two outposts with a satellite channel can communicate via the satellite, regardless of their location. Otherwise, two outposts can communicate by radio only if the distance between them does not exceed D, which depends of the power of the transceivers. Higher power yields higher D but costs more.
-
#include<algorithm> #include<iostream> #include<cmath> #include<iomanip> using namespace std; #define maxn 522 #define inf 0x3f3f3f3f double mmp[maxn][maxn]; double dis[maxn],ans[maxn]; int s,p,t; double x[maxn],y[maxn]; bool vis[maxn]; void prim() { dis[1]=0; for(int i=0; i<p; i++) { double temp=inf; int k=-1; for(int j=1; j<=p; j++) if(dis[j]<temp&&!vis[j]) temp=dis[k=j]; if(k==-1) break; vis[k]=1; ans[i]=dis[k]; for(int j=1; j<=p; j++) if(dis[j]>mmp[k][j]&&!vis[j]) dis[j]=mmp[k][j]; } sort(ans,ans+p); cout<<fixed<<setprecision(2)<<ans[p-s]<<endl; return ; } double cal(int i,int j) { return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); } int main() { cin>>t; while(t--) { cin>>s>>p; for(int i=1; i<=p; i++) { cin>>x[i]>>y[i]; dis[i]=inf; vis[i]=0; } for(int i=1; i<=p; i++) for(int j=1; j<=p; j++) mmp[i][j]=mmp[j][i]=cal(i,j); prim(); } return 0; }
Arctic Network-PRIM第K最小边
最新推荐文章于 2021-08-06 20:12:32 发布