题目:

Out of Hay
Time Limit: 1000MS        Memory Limit: 65536K
Total Submissions: 10072        Accepted: 3885
DescriptionThe 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 (2 <= N <= 2,000) farms (numbered 1..N); Bessie starts at Farm 1. 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 1: Two space-separated integers, N and M. * Lines 2..1+M: Line i+1 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 1: A single integer that is the length of the longest road required to be traversed.
Sample Input3 3
1 2 23
2 3 1000
1 3 43
Sample Output43
HintOUTPUT DETAILS: In order to reach farm 2, Bessie travels along a road of length 23. To reach farm 3, Bessie travels along a road of length 43. With capacity 43, she can travel along these roads provided that she refills her tank to maximum capacity before she starts down a road.

View Question

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

 1 #include <stdio.h>
 2 #define MAXVEX 500
 3 #define INFINITY 65535
 4
 5 typedef char VertexType;
 6 typedef int EdgeType;
 7
 8 typedef struct {
 9     EdgeType arc[MAXVEX][MAXVEX];
10     int numVertexes,numEdges;
11 }MGraph;
12
13 MGraph G;
14
15 void CreateMGraph(MGraph *G){
16     int i,j,k,w;
17     scanf("%d %d",&G->numVertexes,&G->numEdges);//输入顶点和边数
18     for(i = 0;i <= G->numVertexes; i++){
19         for(j = 0;j <= G->numVertexes ;j++){
20             G->arc[i][j]=INFINITY;
21         }
22     }
23     for(k = 1;k<=G->numEdges;k++){
24         scanf("%d %d %d",&i,&j,&w);
25         G->arc[i][j]=w;
26         G->arc[j][i]=G->arc[i][j];//因为是无向图,所以对称
27     }
28 }
29
30 /* Prim算法生成最小生成树  */
31 int MiniSpanTree_Prim(MGraph G){
32     int min,i,j,k;
33     int adjvex[MAXVEX];     /* 保存相关顶点下标 */
34     int lowcost[MAXVEX];    /* 保存相关顶点间边的权值 */
35     int dist_max;
36     lowcost[1]=0;           /* 初始化第一个权值为0,即v0加入生成树 */
37                             /* lowcost的值为0,在这里就是此下标的顶点已经加入生成树 */
38     adjvex[1]=1;            /* 初始化第一个顶点下标为0 */
39     dist_max=0;
40     for(i=2;i<=G.numVertexes;i++){/* 循环除下标为0外的全部顶点 */
41         lowcost[i]=G.arc[1][i];/* 将v0顶点与之有边的权值存入数组 */
42         adjvex[i]=1;        /* 初始化都为v0的下标 */
43     }
44     for(i=2;i<=G.numVertexes;i++){
45         min=INFINITY;       /* 初始化最小权值为∞, */
46                             /* 通常设置为不可能的大数字如32767、65535等 */
47         j=2;k=1;
48         while(j<=G.numVertexes){/* 循环全部顶点 */
49                 if(lowcost[j]!=0 && lowcost[j]<min){/* 如果权值不为0且权值小于min */
50                     min = lowcost;/* 则让当前权值成为最小值 */
51                     k=j;    /* 将当前最小值的下标存入k */
52                 }
53                 j++;
54             }
55         if(G.arc[1][k] > dist_max && G.arc[1][k] < INFINITY){
56             dist_max=G.arc[1][k];
57             //printf("%d\n",)
58         }
59         //printf("(%d,%d)\n",adjvex[k],k);  /* 打印当前顶点边中权值最小的边 */
60
61         lowcost[k]=0;/* 将当前顶点的权值设置为0,表示此顶点已经完成任务 */
62
63         for(j=2;j<=G.numVertexes;j++){/* 循环所有顶点 */
64             if(lowcost[j]!=0 && G.arc[k][j]<lowcost[j]){
65                 /* 如果下标为k顶点各边权值小于此前这些顶点未被加入生成树权值 */
66                 lowcost[j]=G.arc[k][j];/* 将较小的权值存入lowcost相应位置 */
67                 adjvex[j]=k;        /* 将下标为k的顶点存入adjvex */
68             }
69         }
70     }
71     return dist_max;
72 }
73
74 int main(){
75     int max;
76     CreateMGraph(&G);
77     max=MiniSpanTree_Prim(G);
78     printf("%d\n",max);
79     return 0;
80 }

AC代码来自冲神:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <queue>
 5 #include <cstring>
 6 #include <cmath>
 7
 8 using namespace std;
 9
10 const int MAX = 2005;
11 const int INF = 0x7fffffff;
12
13 struct node
14 {
15     int to,nxt,len;
16 } Map[20006];
17
18 int n,m;
19 int head[MAX],idx,dist[MAX];
20 bool vist[MAX];
21
22 void addNode(int cur,int to,int len)
23 {
24     Map[idx].to = to;
25     Map[idx].nxt = head[cur];
26     Map[idx].len = len;
27     head[cur] = idx ++;
28 }
29
30 void prim(int cur)
31 {
32     while(cur)
33     {
34         for(int i = head[cur]; i != -1; i = Map[i].nxt)
35         {
36             int to = Map[i].to;
37             if(vist[to] != true&&dist[to] > Map[i].len)
38                 dist[to] = Map[i].len;
39         }
40         cur = 0;
41         for(int i = 1; i <= n; i ++)
42         {
43             if(vist[i] == false && dist[i] < dist[cur])
44             {
45                 cur = i;
46             }
47         }
48         vist[cur] = true;
49     }
50 }
51
52 void init()
53 {
54     idx = 0;
55     memset(head,-1,sizeof(head));
56     fill(dist,dist+MAX,INF);
57     memset(vist,0,sizeof(vist));
58 }
59
60 int main()
61 {
62     int a,b,c;
63     while(scanf("%d%d",&n,&m)!=EOF)
64     {
65         init();
66         for(int i = 1; i <= m; i ++)
67         {
68             scanf("%d%d%d",&a,&b,&c);
69             addNode(a,b,c);
70             addNode(b,a,c);
71         }
72         vist[1] = true;
73         prim(1);
74         int Max = 0;
75         for(int i = 2; i <= n; i ++)
76         {
77             Max = max(Max,dist[i]);
78             //printf("%d ",dist[i]);
79         }
80         printf("%d\n",Max);
81     }
82     return 0;
83 }

转载于:https://www.cnblogs.com/wushuaiyi/p/3581292.html

POJ2395 最小生成树 - Prime算法相关推荐

  1. 最小生成树Prime算法

    洛谷p1546链接 Prime算法的核心也是贪心,但是不同的就是,它是一直维护一颗树, 直到变成一颗最小生成树, #include<bits/stdc++.h> using namespa ...

  2. 算法学习之路|最小生成树——prime算法

    摘要: 算法概述:对于一个带权的连通图,其顶点的集合 为V,边的集合为E.定义一个新的集合Vnew={空},第一步在图中任选一个顶点v加入Vnew,第二步寻找最短的边(u,v),其中u∈Vnew,v∈ ...

  3. 小白都能看懂最小生成树prime算法

    定义不多说,说说代码的实现. 用落谷上的题目来说明代码的正确性 落谷上的题目 邻接矩阵的实现 定义一个生成树点的集合A,和图中其他点 的集合B 先任意选择一个点a加入到A中,即visit[a]=tru ...

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

    带权图分为有向和无向,无向图的最短路径又叫做最小生成树,有prime算法和kruskal算法:有向图的最短路径算法有dijkstra算法和floyd算法. 生成树的概念:联通图G的一个子图如果是一棵包 ...

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

    带权图分为有向和无向,无向图的最短路径又叫做最小生成树,有prime算法和kruskal算法:有向图的最短路径算法有dijkstra算法和floyd算法. 生成树的概念:联通图G的一个子图如果是一棵包 ...

  6. 数据结构——最小生成树之prime算法(与最短路径之迪杰斯特拉算法很像)

    最小生成树之prime算法 ***最小生成树:一个连通图的生成树中,所有边的权值加起来最小的生成树:称为最小生成树: [简介]:Prime算法可在加权连通图里搜索最小生成树.即:所有边的权值之和为最小 ...

  7. 最小生成树的Prime算法的思想

    Prime算法的核心步骤是:在带权连通图中V是包含所有顶点的集合, U已经在最小生成树中的节点,从图中任意某一顶点v开始,此时集合U={v},重复执行下述操作:在所有u∈U,w∈V-U的边(u,w)∈ ...

  8. prime算法详解【最小生成树】

    无以言表我对着代码懵了两个小时终于看懂了的鸡冻,手写程序大法好哇,[或者只是我太久没敲代码了..]个人感觉这个算法还是有点粗鲁,大量的遍历,比较中意最小生成树的另一个算法,一会搞懂它的代码再说. 最小 ...

  9. 数据结构与算法A实验六图论---7-4 公路村村通(最小生成树Prime和Kruskal算法)

    现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本. 输入格式: 输入数据包括城镇数目正整数N(≤1000)和候选道路数目M(≤3N) ...

  10. 数据结构——最短路径之Dijkstra算法(与最小生成树的prime算法很像,建议一起看)

    最短路径之Dijkstra算法 (一)Dijkstra算法 单源最短路径:就是从某一个顶点出发,到图中任意顶点之间的最短路径: [算法概述]:Dijkstra算法适用于解决单源最短路径的问题.即:从源 ...

最新文章

  1. 有关volatile unsigned long一些说明
  2. list array解析(总算清楚一点了)
  3. 微软发布架构师期刊阅读器
  4. 使用C#实现Form窗体的淡入淡出效果
  5. 最简单的视频编码器:基于libvpx(编码YUV为VP8)
  6. 一分钟学会看k线图_在股市中K线图怎么看,有什么意义?
  7. Frp后台自动启动的几个方法-内网穿透
  8. NYOJ题目71-独木舟上的旅行(贪心)
  9. libtorrent-bittorrent
  10. 计算机安装系统后鼠标无法使用,重装系统后鼠标不能用
  11. 夜神模拟器和虚拟机(docker) 在windows上设置不兼容
  12. python圣诞节_圣诞节,用Python给自己加顶“圣诞帽”
  13. 网页截图怎么截一整张_如何对整个网页页面进行截图
  14. Android程序员简历
  15. 安装配置ELK、安装配置ElasticSearch7.13、安装配置Kibana7.13、安装配置Logstash7.13、ElasticSearch7.13安装中文分词器
  16. TweenMax.to()的使用
  17. qt webview 忽略 ssl错误 ignoreSslErrors
  18. c++ 的vector、array和数组的比较
  19. 基于UGUI实现类似Excel表格功能
  20. 基于python的自媒体和官媒数据爬取、可视化分析及云词图制作

热门文章

  1. Sublime Text3搭建HTML环境
  2. metamask源码学习-ui/index.js
  3. 开启Mac原生NTFS支持
  4. 1.1.6版本Druid连接MSSQLServer 2008 R2报错The query timeout value -1 is not valid. #2210
  5. python 2 版本中的input() 和 raw_input() 函数的比较
  6. oracle主机修改IP后客户端无法连接
  7. 我的第一份博客,测试一下:P
  8. Spring Framework标记库初学指南
  9. c 连接mysql自动断开_ESQL/C连接和断开数据库(INFORMIX) | 学步园
  10. 向量外积_几何(立体几何,解析几何)中向量叉乘与行列式的运用