hdu1162 Eddy's picture

作者在 2011-07-20 21:04:48 发布以下内容

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162

#define
MAX_VERTEX_NUM 105    
#define INFINITY 0xffffffff    
#define TRUE 1    
#define FALSE 0    
    
typedef struct{    
    //int info;    
}VertexType;    
    
typedef struct{    
    double val;    
    //int info;    
}ArcType,ArcMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];    
    
typedef struct{    
    int vexnum;    
    VertexType vexs[MAX_VERTEX_NUM];    
    ArcMatrix arcs;    
}MGraph;    
    
    
// 记录从顶点集U到V-U的代价最小的边的辅助数组定义    
typedef struct {    
    int adjvex;    
    double lowcost;    
}CloseEdge;    
    
    
int MiniEdge(CloseEdge closedge[],int cnt)    
{    
    int i;    
    int k = -1;    
    for (i=0;i<cnt;i++)    
        if (closedge[i].lowcost>0) // V-U    
            if (k==-1 || closedge[k].lowcost>closedge[i].lowcost) k=i;    
            return k;    
}    
    
#include <string.h>
#include <iomanip>  
#include <iostream>    
using namespace std;    
    
double MiniSpanTree_PRIM(MGraph &G, int u) {    
    // 用普里姆算法从第u个顶点出发构造网G的最小生成树T,输出T的各条边。    
    int i,j,k;    
    double summiniw=0;  
    CloseEdge closedge[MAX_VERTEX_NUM];    
    k = u;    
    for ( j=0; j<G.vexnum; ++j ) {     // 辅助数组初始化    
        if (j!=k)    
        { closedge[j].adjvex=k; closedge[j].lowcost=G.arcs[k][j].val; }    
    }    
    closedge[k].lowcost = 0;      // 初始,U={u}    
    for (i=1; i<G.vexnum; ++i) {  // 选择其余G.vexnum-1个顶点    
        k = MiniEdge(closedge,G.vexnum);      // 求出T的下一个结点:第k顶点    
    
//**********************************************************************  
         summiniw+=G.arcs[closedge[k].adjvex][k].val;  
        //cout<< closedge[k].adjvex+1 <<"-"<< k+1 <<", "<<endl; // 输出生成树的边    
    
//***********************************************************************        
        closedge[k].lowcost = 0;    // 第k顶点并入U集    
        for (j=0; j<G.vexnum; ++j)    
            if (G.arcs[k][j].val < closedge[j].lowcost) {    
                // 新顶点并入U后重新选择最小边    
                
// closedge[j] = { G.vexs[k], G.arcs[k][j].adj };    
                closedge[j].adjvex=k;    
                closedge[j].lowcost=G.arcs[k][j].val;    
            }    
    }    
    return summiniw;    
} // MiniSpanTree    
    
    
MGraph g;    
    
#include <stdio.h>    
#include <math.h>

struct point
{ double x,y; };

int main(int argc, char* argv[])    
{    
    int n;          
    point p[105];
    while(cin>>n)    
    {  
        if(n==0)break;  
        g.vexnum = n;    
        memset(g.arcs,INFINITY,sizeof(g.arcs));
        int i,j;        
        for(i=0;i<n;i++)cin>>p[i].x>>p[i].y;
        for(i=0;i<n;i++)
            for(j=i+1;j<n;j++)
            {    
                double w=sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)
                    +(p[i].y-p[j].y)*(p[i].y-p[j].y));
                    g.arcs[i][j].val = w;    
                    g.arcs[j][i].val = w;      
            }        
        int u = 1;  
        //c++中精度控制
        cout<<setiosflags(ios::fixed)<<setprecision(2)
            <<MiniSpanTree_PRIM(g,u-1)<<endl;    
    }  
    return 0;    
}    
 
图论 | 阅读 1067 次
文章评论,共0条
游客请输入验证码
最新评论