标签:
大致的步骤
首先选取一个到集合最近的点 然后标记起在集合内部 然后更新最短距离
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24846 Accepted Submission(s): 8035
#include<cstdio> #include<iostream> #include<string.h> #include<cmath> #define maxn 105 #define inf 9999999 struct node { double x,y; }stu[maxn]; int vis[maxn],n;//用来表示点是否在集合里 double mincost[maxn];//用来记录从集合出来到每个点的最小距离 double mapp[maxn][maxn]; using namespace std; double get_len(node a,node b) { return sqrt(pow(a.x-b.x,2.0)+pow(a.y-b.y,2.0)); } void build_map() { for(int i=1;i<=n;i++) { for(int j=i;j<=n;j++) { double len=get_len(stu[i],stu[j]); if(len>=10&&len<=1000) mapp[i][j]=mapp[j][i]=(i==j)?0:len; else mapp[i][j]=mapp[j][i]=inf; } } } void init() { memset(vis,0,sizeof(vis)); } double minn(double x,double y) { if(x-y>0) return y; else return x; } double prim() { fill(mincost,mincost+n+1,inf); mincost[1]=0; double res=0; while(1) { int v=-1; for(int i=1;i<=n;i++) if(!vis[i]&&(v==-1||mincost[i]<mincost[v])) v=i;//找出离集合最近的点 if(v==-1) break; vis[v]=1; res+=mincost[v]; for(int i=1;i<=n;i++) mincost[i]=minn(mincost[i],mapp[v][i]);//更新最小距离 } return res; } int main() { cin.sync_with_stdio(false); int t; scanf("%d",&t); while(t--) { init(); scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%lf %lf",&stu[i].x,&stu[i].y); build_map(); double temp=prim()*100.0; if(temp>inf) printf("oh!\n"); else printf("%.1f\n",temp); } return 0; }
#include<cstdio>#include<iostream>#include<string.h>#include<cmath>#define maxn 105#define inf 9999999struct node{ double x,y;}stu[maxn];int vis[maxn],n;//用来表示点是否在集合里 double mincost[maxn];//用来记录从集合出来到每个点的最小距离 double mapp[maxn][maxn];using namespace std;double get_len(node a,node b){return sqrt(pow(a.x-b.x,2.0)+pow(a.y-b.y,2.0));}void build_map(){for(int i=1;i<=n;i++){for(int j=i;j<=n;j++){double len=get_len(stu[i],stu[j]);if(len>=10&&len<=1000)mapp[i][j]=mapp[j][i]=(i==j)?0:len;else mapp[i][j]=mapp[j][i]=inf;}}}void init(){memset(vis,0,sizeof(vis));}double minn(double x,double y){if(x-y>0) return y;else return x;}double prim(){fill(mincost,mincost+n+1,inf);mincost[1]=0;double res=0; while(1) { int v=-1; for(int i=1;i<=n;i++)if(!vis[i]&&(v==-1||mincost[i]<mincost[v])) v=i;//找出离集合最近的点 if(v==-1) break; vis[v]=1;res+=mincost[v];for(int i=1;i<=n;i++) mincost[i]=minn(mincost[i],mapp[v][i]); } return res;}int main(){cin.sync_with_stdio(false);int t; scanf("%d",&t);while(t--) { init(); scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%lf %lf",&stu[i].x,&stu[i].y); build_map(); double temp=prim()*100.0; if(temp>inf) printf("oh!\n"); else printf("%.1f\n",temp); }return 0;}
标签:
原文地址:http://www.cnblogs.com/z1141000271/p/5799617.html