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 ...
随机推荐
- Consul初体验
Preface Today I'm gonna implement a consul in my environment to discover service of MySQL da ...
- 「国庆训练」Bomb(HDU-5934)
题意 给定\(n\)个炸弹,每个炸弹的坐标与代价与影响范围给定,炸弹会引爆影响范围内其他所有炸弹.求引爆所有炸弹的最小代价. 分析 先做\(n^2\)的循环,然后建图,对\(i\)能引爆\(j\)建边 ...
- cookie的介绍和自动化中cookie的操作
1 cookie是什么? cookie: 1. Cookie是一小段的文本信息:格式:python中的字典(键值对组成) 2. Cookie产生:客户端请求服务器,如果服务器需要记录该用户状态,就向客 ...
- 【xmlHttp_Class 远程访问类】使用说明
类名:xmlHttp_Class 说明:远程获取外部网站数据信息或执行一个外部网站程序 目录: 类型 名称 参数 返回 说明 属性 [必需] [xmlHttp].url = [urlString] - ...
- CryptoZombies学习笔记——Lesson1
CryptoZombies是一个学习以太坊开发的平台,我将在这里记录学习过程中的一些笔记. 课程网址:cryptozombies.io 首先是第一课——Lesson1:Making the Zombi ...
- POJ 2449 Remmarguts' Date(第k短路のA*算法)
Description "Good man never makes girls wait or breaks an appointment!" said the mandarin ...
- POJ 3608 Bridge Across Islands(计算几何の旋转卡壳)
Description Thousands of thousands years ago there was a small kingdom located in the middle of the ...
- H5页面 绝对定位元素被 软键盘弹出时顶起
H5页面 绝对定位元素被 软键盘弹出时顶起 在h5页面开发的过程中,我们可能会遇到下面这个问题,当页面中有输入框的时候,系统自带的软盘会把按钮挤出原来的位置.那么我们该怎么解决呢?下面列出一下的方法: ...
- Red and Black(DFS深搜实现)
Description There is a rectangular room, covered with square tiles. Each tile is colored either red ...
- 【IdentityServer4文档】- 整体情况
整体概况 大多数现代应用程序看起来或多或少像这样: 最常见的交互是: 浏览器与 Web 应用程序进行通信 Web 应用程序与 Web API 进行通信(有时是Web应用程序自己发起,有时代表用户发起) ...