题目:

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 ;
}

POJ2395 最小生成树 - Prime算法的更多相关文章

  1. 最小生成树 prime算法 UVALive - 6437

    题目链接:https://vjudge.net/contest/241341#problem/D 这里有多个发电站,需要求出所有点都和发电站直接或间接相连的最小代价,那么就是求出最小生成树的问题了,有 ...

  2. 最小生成树prime算法模板

    #include<stdio.h> #include<string.h> using namespace std; int map[505][505]; int v, e; i ...

  3. hdoj 1233 还是畅通工程---最小生成树---prime算法

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1233 可以用Kruskal来做,不过当图的边比较稠密的时候用prime会更快一些. AC代码:296MS ...

  4. hdoj 1863 畅通工程 最小生成树---prime算法

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=1863 注意有可能出现无法生成树的情况. #include <iostream> #inclu ...

  5. 最小生成树---Kruskal/Prime算法

    1.Kruskal算法 图的存贮采用边集数组或邻接矩阵,权值相等的边在数组中排列次序可任意,边较多的不很实用,浪费时间,适合稀疏图.      方法:将图中边按其权值由小到大的次序顺序选取,若选边后不 ...

  6. 最小生成树之算法记录【prime算法+Kruskal算法】【模板】

    首先说一下什么是树: 1.只含一个根节点 2.任意两个节点之间只能有一条或者没有线相连 3.任意两个节点之间都可以通过别的节点间接相连 4.除了根节点没一个节点都只有唯一的一个父节点 5.也有可能是空 ...

  7. prime算法求最小生成树(畅通工程再续)

    连着做了四道畅通工程的题,其实都是一个套路,转化为可以求最小生成树的形式求最小生成树即可 这道题需要注意: 1:因为满足路的长度在10到1000之间才能建路,所以不满足条件的路径长度可以初始化为无穷 ...

  8. hdu 1233(还是畅通project)(prime算法,克鲁斯卡尔算法)(并查集,最小生成树)

    还是畅通project Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  9. 最小生成树(prime算法 & kruskal算法)和 最短路径算法(floyd算法 & dijkstra算法)

    一.主要内容: 介绍图论中两大经典问题:最小生成树问题以及最短路径问题,以及给出解决每个问题的两种不同算法. 其中最小生成树问题可参考以下题目: 题目1012:畅通工程 http://ac.jobdu ...

随机推荐

  1. codeforces 622C. Optimal Number Permutation 构造

    题目链接 假设始终可以找到一种状态使得值为0, 那么两个1之间需要隔n-2个数, 两个2之间需要隔n-3个数, 两个3之间隔n-4个数. 我们发现两个三可以放到两个1之间, 同理两个5放到两个3之间. ...

  2. HTML+CSS笔记 CSS笔记集合

    HTML+CSS笔记 表格,超链接,图片,表单 涉及内容:表格,超链接,图片,表单 HTML+CSS笔记 CSS入门 涉及内容:简介,优势,语法说明,代码注释,CSS样式位置,不同样式优先级,选择器, ...

  3. (Problem 74)Digit factorial chains

    The number 145 is well known for the property that the sum of the factorial of its digits is equal t ...

  4. JAVA中List、Map、Set的区别与选用

    类层次关系如下: Collection ├List│├LinkedList│├ArrayList│└Vector│ └Stack└SetMap├Hashtable├HashMap └WeakHashM ...

  5. 汇编语言学习——第二章 寄存器(CPU工作原理)

    1.一个典型的CPU由运算器.控制器.寄存器等器件组成,这些器件靠内部总线相连. 区别: 内部总线实现CPU内部各个器件之间的联系. 外部总线实现CPU和主板上其它器件的联系. 8086CPU有14个 ...

  6. 对象图(Object Diagram)—UML图(三)

    一.用一张图来介绍一下对象图的基本内容 二.对象图与类图的基本差别 三.对象图实例

  7. Android的电源管理框架

    Android的电源管理框架 Android通过锁和定时器来切换系统的状态,使系统的功耗降至最低,整个系统的电源管理框架分成五个部分:应用层,framework层,JNI层,HAL层和内核层.电源管理 ...

  8. HYSBZ1588 营业额统计【Splay】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4366582.html   ---by 墨染之樱花 [题目链接]http://www.lydsy ...

  9. C# Webservice

    c#webservice的简单示例   webservice传递的数据只能是序列化的数据,典型的就是xml数据. 下面以一个简单例子为例: (一)新建——-项目---Visual C#---web-- ...

  10. jQuery 1.9+ ajaxStart事件无效,无法被触发的原因。

    AJAX 事件需要绑定到document 在jQuery 1.9中, 全局的AJAX事件(ajaxStart, ajaxStop, ajaxSend, ajaxComplete, ajaxError, ...