hdu 1875 最小生成树 prime版
最小生成树prime版
大致的步骤
首先选取一个到集合最近的点 然后标记起在集合内部 然后更新最短距离
畅通工程再续
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24846 Accepted Submission(s): 8035
每组数据首先是一个整数C(C <= 100),代表小岛的个数,接下来是C组坐标,代表每个小岛的坐标,这些坐标都是 0 <= x, y <= 1000的整数。
#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;}