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 ...
随机推荐
- poj 1753 Flip Game 高斯消元
题目链接 4*4的格子, 初始为0或1, 每次翻转一个会使它四周的也翻转, 求翻转成全0或全1最少的步数. #include <iostream> #include <vector& ...
- C——货物管理系统
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> ...
- Linux串口编程详解(转)
串口本身,标准和硬件 † 串口是计算机上的串行通讯的物理接口.计算机历史上,串口曾经被广泛用于连接计算机和终端设备和各种外部设备.虽然以太网接口和USB接口也是以一个串行流进行数据传送的,但是串口连接 ...
- QString类的使用(无所不包,极其方便)
Qt的QString类提供了很方便的对字符串操作的接口. 使某个字符填满字符串,也就是说字符串里的所有字符都有等长度的ch来代替. QString::fill ( QChar ch, int size ...
- Delphi中三种方法获取Windows任务栏的高度
第一种:需要引用Windows单元 ShowMessage(IntToStr(GetSystemMetrics(SM_CYSCREEN)-GetSystemMetrics(SM_CYFULLSCREE ...
- 分布式配置管理平台 Disconf
Distributed Configuration Management Platform(分布式配置管理平台) 专注于各种 分布式系统配置管理 的通用组件/通用平台, 提供统一的配置管理服务. 包括 ...
- flexjson 的使用
日期转换 JSONSerializer serializer = new JSONSerializer(); serializer.exclude(new St ...
- break 与continue的区别
//break是结束整个循环体,continue是结束单次循环 比方说: while(x++ < 10){ if(x == 3) { break; } printf("%d\r\n&q ...
- 对象图(Object Diagram)—UML图(三)
一.用一张图来介绍一下对象图的基本内容 二.对象图与类图的基本差别 三.对象图实例
- JavaScript中的事件处理程序
JavaScript和HTML之间的交互是通过事件实现的.事件,就是文档或者浏览器窗口中发生的一些特定的交互瞬间.可以使用事件处理程序来预订事件,以便在事件发生的时候执行响应的代码.这种观察者模式的模 ...