POJ2395 最小生成树 - Prime算法

时间:2022-12-27 16:33:34

题目:

Out of Hay
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: Accepted:
Description The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay situation. There are N ( <= N <= ,) farms (numbered ..N); Bessie starts at Farm . She'll traverse some or all of the M (1 <= M <= 10,000) two-way roads whose length does not exceed 1,000,000,000 that connect the farms. Some farms may be multiply connected with different length roads. All farms are connected one way or another to Farm 1. Bessie is trying to decide how large a waterskin she will need. She knows that she needs one ounce of water for each unit of length of a road. Since she can get more water at each farm, she's only concerned about the length of the longest road. Of course, she plans her route between farms such that she minimizes the amount of water she must carry. Help Bessie know the largest amount of water she will ever have to carry: what is the length of longest road she'll have to travel between any two farms, presuming she chooses routes that minimize that number? This means, of course, that she might backtrack over a road in order to minimize the length of the longest road she'll have to traverse.
Input * Line : Two space-separated integers, N and M. * Lines ..+M: Line i+ contains three space-separated integers, A_i, B_i, and L_i, describing a road from A_i to B_i of length L_i.
Output * Line : A single integer that is the length of the longest road required to be traversed.
Sample Input Sample Output Hint OUTPUT DETAILS: In order to reach farm , Bessie travels along a road of length . To reach farm , Bessie travels along a road of length . With capacity , she can travel along these roads provided that she refills her tank to maximum capacity before she starts down a road.

View Question

以下代码来自《大话数据结构》,存在问题

 #include <stdio.h>
#define MAXVEX 500
#define INFINITY 65535 typedef char VertexType;
typedef int EdgeType; typedef struct {
EdgeType arc[MAXVEX][MAXVEX];
int numVertexes,numEdges;
}MGraph; MGraph G; void CreateMGraph(MGraph *G){
int i,j,k,w;
scanf("%d %d",&G->numVertexes,&G->numEdges);//输入顶点和边数
for(i = ;i <= G->numVertexes; i++){
for(j = ;j <= G->numVertexes ;j++){
G->arc[i][j]=INFINITY;
}
}
for(k = ;k<=G->numEdges;k++){
scanf("%d %d %d",&i,&j,&w);
G->arc[i][j]=w;
G->arc[j][i]=G->arc[i][j];//因为是无向图,所以对称
}
} /* Prim算法生成最小生成树 */
int MiniSpanTree_Prim(MGraph G){
int min,i,j,k;
int adjvex[MAXVEX]; /* 保存相关顶点下标 */
int lowcost[MAXVEX]; /* 保存相关顶点间边的权值 */
int dist_max;
lowcost[]=; /* 初始化第一个权值为0,即v0加入生成树 */
/* lowcost的值为0,在这里就是此下标的顶点已经加入生成树 */
adjvex[]=; /* 初始化第一个顶点下标为0 */
dist_max=;
for(i=;i<=G.numVertexes;i++){/* 循环除下标为0外的全部顶点 */
lowcost[i]=G.arc[][i];/* 将v0顶点与之有边的权值存入数组 */
adjvex[i]=; /* 初始化都为v0的下标 */
}
for(i=;i<=G.numVertexes;i++){
min=INFINITY; /* 初始化最小权值为∞, */
/* 通常设置为不可能的大数字如32767、65535等 */
j=;k=;
while(j<=G.numVertexes){/* 循环全部顶点 */
if(lowcost[j]!= && lowcost[j]<min){/* 如果权值不为0且权值小于min */
min = lowcost;/* 则让当前权值成为最小值 */
k=j; /* 将当前最小值的下标存入k */
}
j++;
}
if(G.arc[][k] > dist_max && G.arc[][k] < INFINITY){
dist_max=G.arc[][k];
//printf("%d\n",)
}
//printf("(%d,%d)\n",adjvex[k],k); /* 打印当前顶点边中权值最小的边 */ lowcost[k]=;/* 将当前顶点的权值设置为0,表示此顶点已经完成任务 */ for(j=;j<=G.numVertexes;j++){/* 循环所有顶点 */
if(lowcost[j]!= && G.arc[k][j]<lowcost[j]){
/* 如果下标为k顶点各边权值小于此前这些顶点未被加入生成树权值 */
lowcost[j]=G.arc[k][j];/* 将较小的权值存入lowcost相应位置 */
adjvex[j]=k; /* 将下标为k的顶点存入adjvex */
}
}
}
return dist_max;
} int main(){
int max;
CreateMGraph(&G);
max=MiniSpanTree_Prim(G);
printf("%d\n",max);
return ;
}

AC代码来自冲神:

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
#include <cmath> using namespace std; const int MAX = ;
const int INF = 0x7fffffff; struct node
{
int to,nxt,len;
} Map[]; int n,m;
int head[MAX],idx,dist[MAX];
bool vist[MAX]; void addNode(int cur,int to,int len)
{
Map[idx].to = to;
Map[idx].nxt = head[cur];
Map[idx].len = len;
head[cur] = idx ++;
} void prim(int cur)
{
while(cur)
{
for(int i = head[cur]; i != -; i = Map[i].nxt)
{
int to = Map[i].to;
if(vist[to] != true&&dist[to] > Map[i].len)
dist[to] = Map[i].len;
}
cur = ;
for(int i = ; i <= n; i ++)
{
if(vist[i] == false && dist[i] < dist[cur])
{
cur = i;
}
}
vist[cur] = true;
}
} void init()
{
idx = ;
memset(head,-,sizeof(head));
fill(dist,dist+MAX,INF);
memset(vist,,sizeof(vist));
} int main()
{
int a,b,c;
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(int i = ; i <= m; i ++)
{
scanf("%d%d%d",&a,&b,&c);
addNode(a,b,c);
addNode(b,a,c);
}
vist[] = true;
prim();
int Max = ;
for(int i = ; i <= n; i ++)
{
Max = max(Max,dist[i]);
//printf("%d ",dist[i]);
}
printf("%d\n",Max);
}
return ;
}