POJ.1287 Networking (Prim)
POJ.1287 Networking (Prim)
题意分析
- 可能有重边,注意选择最小的边。
- 编号依旧从1开始。
- 直接跑prim即可。
代码总览
#include <cstdio>
#include <cstring>
#define nmax 105
#define inf 1e8+7
using namespace std;
int mp[nmax][nmax];
bool isfind = true;
int n,m;
int totaldis =0;
void prim()
{
int lowcost[nmax];
int path[nmax];
for(int j = 1;j<=n;++j){
lowcost[j] = mp[1][j];
path[j] = 1;
}
path[1] = 0;
int nowmin,nowminid;
for(int i =2 ;i<=n;++i){
nowmin = inf;
nowminid = 0;
for(int j = 2;j<=n;++j){
if(nowmin > lowcost[j] && lowcost[j] != 0){
nowmin = lowcost[j];
nowminid = j;
}
}
if(nowmin == inf){
isfind = false;
return;
}
totaldis += nowmin;
lowcost[nowminid] = 0;
for(int j = 2;j<=n;++j){
if(mp[nowminid][j] < lowcost[j]){
lowcost[j] = mp[nowminid][j];
path[j] = nowminid;
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d",&n) != EOF && n){
scanf("%d",&m);
int sta,end,dis;
totaldis = 0;
isfind = true;
for(int i = 1; i<=n;++i)
for(int j = 1;j<=n;++j)
mp[i][j] = inf;
for(int i = 1; i<=m ;++i){
scanf("%d %d %d",&sta,&end,&dis);
if(mp[sta][end] != inf){
int temp = mp[sta][end];
if(dis < temp)
mp[sta][end] = mp[end][sta] = dis;
}else{
mp[sta][end] = mp[end][sta] = dis;
}
}
prim();
if(isfind == false) totaldis = 0;
printf("%d\n",totaldis);
}
return 0;
}
POJ.1287 Networking (Prim)的更多相关文章
- ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法
题目链接:problemCode=1372">ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds ...
- POJ 1287 Networking (最小生成树)
Networking Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit S ...
- POJ 1287 Networking (ZOJ 1372) MST
http://poj.org/problem?id=1287 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=372 和上次那题差 ...
- POJ 1287 Networking
题目链接: poj.org/problem?id=1287 题目大意: 你被分派到去设计一个区域的连接点,给出你每个点对之间的路线,你需要算出连接所有点路线的总长度. 题目输入: 一个数字n 代表有 ...
- POJ - 1287 Networking 【最小生成树Kruskal】
Networking Description You are assigned to design network connections between certain points in a wi ...
- POJ 1287 Networking【kruskal模板题】
传送门:http://poj.org/problem?id=1287 题意:给出n个点 m条边 ,求最小生成树的权 思路:最小生树的模板题,直接跑一遍kruskal即可 代码: #include< ...
- POJ 1287 Networking (最小生成树)
Networking 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/B Description You are assigned ...
- poj 1287 Networking【最小生成树prime】
Networking Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7321 Accepted: 3977 Descri ...
- POJ 1287 Networking(最小生成树)
题意 给你n个点 m条边 求最小生成树的权 这是最裸的最小生成树了 #include<cstdio> #include<cstring> #include<algor ...
随机推荐
- 「日常训练」Kefa and Company(Codeforces Round #321 Div. 2 B)
题意与分析(CodeForces 580B) \(n\)个人,告诉你\(n\)个人的工资,每个人还有一个权值.现在从这n个人中选出m个人,使得他们的权值之和最大,但是对于选中的人而言,其他被选中的人的 ...
- 分享一个 UiPath Studio 相关的公众号
RPA 和 UiPath 方面的资料比较少,因此我们自己创建了一个公众号,专门用于传播 UiPath 相关的知识. 会定期发布 UiPath 学习相关的信息.是目前难得的 UiPath 中文资源. 公 ...
- jmeter属性设置
使用${__setProperty(newuserid,1,)}函数对属性进行设置 2.使用${__P(userid)}函数在其他线程组中引用该属性 备注: 1.JMeter属性在测试脚本的任何地方都 ...
- Siki_Unity_1-1_Unity零基础入门_打砖块
1-1 Unity零基础入门 打砖块 任务1:素材源码 www.sikiedu.com/course/77 任务2:Unity介绍 王者荣耀,球球大作战等游戏都是用unity开发的 跨平台的游戏引擎 ...
- HDU - 6409:没有兄弟的舞会(数学+思维)
链接:HDU - 6409:没有兄弟的舞会 题意: 题解: 求出最大的 l[i] 的最大值 L 和 r[i] 的最大值 R,那么 h 一定在 [L, R] 中.枚举每一个最大值,那么每一个区间的对于答 ...
- 376. Binary Tree Path Sum【LintCode java】
Description Given a binary tree, find all paths that sum of the nodes in the path equals to a given ...
- BZOJ 4361 isn 容斥+dp+树状数组
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4361 题意概述: 给出一个长度为N的序列A(A1,A2...AN).如果序列A不是非降的 ...
- vue学习笔记(五):对于vuex的理解 + 简单实例
优点:通过定义和隔离状态管理中的各种概念并强制遵守一定的规则,我们的代码将会变得更结构化且易维护.使用vuex来引入外部状态管理,将业务逻辑切分到组件外,可以避免重复的从服务端抓取数据. 详情请参考官 ...
- python中argparse库的使用教程链接
这两篇文章详细介绍了argparse库的参数设置及使用包括位置参数与可选参数的用法 http://blog.csdn.net/guojuxia/article/details/44462381 htt ...
- lintcode-33-N皇后问题
33-N皇后问题 n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击. 给定一个整数n,返回所有不同的n皇后问题的解决方案. 每个解决方案包含一个明确的n皇后放置布局,其中" ...