POJ2395 最小生成树 - Prime算法
题目:
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算法的更多相关文章
- 最小生成树 prime算法 UVALive - 6437
题目链接:https://vjudge.net/contest/241341#problem/D 这里有多个发电站,需要求出所有点都和发电站直接或间接相连的最小代价,那么就是求出最小生成树的问题了,有 ...
- 最小生成树prime算法模板
#include<stdio.h> #include<string.h> using namespace std; int map[505][505]; int v, e; i ...
- hdoj 1233 还是畅通工程---最小生成树---prime算法
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1233 可以用Kruskal来做,不过当图的边比较稠密的时候用prime会更快一些. AC代码:296MS ...
- hdoj 1863 畅通工程 最小生成树---prime算法
题目: http://acm.hdu.edu.cn/showproblem.php?pid=1863 注意有可能出现无法生成树的情况. #include <iostream> #inclu ...
- 最小生成树---Kruskal/Prime算法
1.Kruskal算法 图的存贮采用边集数组或邻接矩阵,权值相等的边在数组中排列次序可任意,边较多的不很实用,浪费时间,适合稀疏图. 方法:将图中边按其权值由小到大的次序顺序选取,若选边后不 ...
- 最小生成树之算法记录【prime算法+Kruskal算法】【模板】
首先说一下什么是树: 1.只含一个根节点 2.任意两个节点之间只能有一条或者没有线相连 3.任意两个节点之间都可以通过别的节点间接相连 4.除了根节点没一个节点都只有唯一的一个父节点 5.也有可能是空 ...
- prime算法求最小生成树(畅通工程再续)
连着做了四道畅通工程的题,其实都是一个套路,转化为可以求最小生成树的形式求最小生成树即可 这道题需要注意: 1:因为满足路的长度在10到1000之间才能建路,所以不满足条件的路径长度可以初始化为无穷 ...
- hdu 1233(还是畅通project)(prime算法,克鲁斯卡尔算法)(并查集,最小生成树)
还是畅通project Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- 最小生成树(prime算法 & kruskal算法)和 最短路径算法(floyd算法 & dijkstra算法)
一.主要内容: 介绍图论中两大经典问题:最小生成树问题以及最短路径问题,以及给出解决每个问题的两种不同算法. 其中最小生成树问题可参考以下题目: 题目1012:畅通工程 http://ac.jobdu ...
随机推荐
- 05-C语言运算符
目录: 一.进制转换 二.常量 三.sizeof 四.运算符 五.赋值运算符 六.自增减运算符 七.关系运算符 八.逻辑运算符 九.取址寻址运算符 回到顶部 一.进制转换 1 进制转换是人们利用符号来 ...
- c++ cout 保留小数点位
需要头文件 <iomanip> 输出时需要用 fixed 和 setprecision() fixed代表输出浮点数,setprecision()设置精度. #include <io ...
- golang实现udp接入服务器
前端通过udp与接入服务器连接,接入服务器与后端tcp服务器维持tcp连接.目录结构及后端tcp服务器代码同上一篇博客. main.go package main import ( "lot ...
- GTW likes math(简单数学)
GTW likes math Accepts: 472 Submissions: 2140 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- Linux相关问题-CentOS6.5 x64版本号下Tomcat无法自启动的解决的方法
前段时间使用阿里云server.使用的是Linux CentOS6.5系统,在搭建完Tomcat后发现,Tomcat无法自启动. 将启动tomcat的命令为tomcat_home/bin/startu ...
- 关于PagedDataSource分页属性与DataSet和DataTable详解
Asp.net提供了三个功能强大的列表控件:DataGrid.DataList和Repeater控件,但其中只有DataGrid控件提供分页功能.相对DataGrid,DataList和Repeate ...
- JavaSE学习总结第15天_集合框架1
15.01 对象数组的概述和使用 public class Student { // 成员变量 private String name; private int age; // 构造方法 publ ...
- eclipse安装PyDev插件出错No software site found at jar:file:[离线包路径]!/. Do you wish to edit the location?
解决方法是直接将下载的离线包解压,得到plugins和features文件夹,放到Eclipse的dropins目录下.重启Eclipse,PyDev插件即可安装成功. 离线包下载地址:http:// ...
- Oracle语句优化规则(一)
1. 选用适合的ORACLE优化器 ORACLE的优化器共有3种: a. RULE (基于规则) b. COST (基于成本) c. CHOOSE (选择性) 设置缺省的优化 ...
- 面向对象程序设计-C++ Finial exam review NOTES【第十六次上课笔记】
写在前面: 我记得也不全,如果有记录的更全的同学可以留言,我会添加哒 :) 常量 内敛函数 为什么需要内敛函数 内敛函数适用于什么场合 内敛函数本身,最大优点是,避免了真正函数调用的开销 因为普通函数 ...