您的位置:首页 > 其它

poj 最小生成树1258

2010-05-16 11:30 295 查看

Agri-Net

Time Limit: 1000ms Memory limit: 10000kB 题目描述 Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms. Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm. The distance between any two farms will not exceed 100,000. 输入 The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem. 输出 For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms. 样例输入 [code]4 0 4 9 21 4 0 8 17 9 8 0 16


[/code]
// Prim.cpp : Defines the entry point for the console application.
//

#define MAX_VERTEX_NUM 101
#define INFINITY 0x11111111
#define TRUE 1
#define FALSE 0

typedef struct{
//int info;
}VertexType;

typedef struct{
int 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;
int 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 <iostream>
using namespace std;

int MiniSpanTree_PRIM(MGraph &G, int u) {
// 用普里姆算法从第u个顶点出发构造网G的最小生成树T,输出T的各条边。
int i,j,k, min_rode=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顶点

//cout<< closedge[k].adjvex+1 <<"-"<< k+1 <<", "<<G.arcs[closedge[k].adjvex][k].val<<endl; // 输出生成树的边
min_rode+=G.arcs[closedge[k].adjvex][k].val;
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 min_rode;
} // MiniSpanTree

MGraph g;

#include <stdio.h>
int main(int argc, char* argv[])
{
int n,m;
int i,j;

while( scanf("%d",&n)!=EOF)
{
g.vexnum = n;
memset(g.arcs,INFINITY,sizeof(g.arcs));
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
scanf("%d",&g.arcs[i][j].val);
}
int u = 1;
cout<<MiniSpanTree_PRIM(g,u-1)<<endl;
}
//system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: