(最小生成树) Networking -- POJ -- 1287
链接:
http://poj.org/problem?id=1287
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7494 | Accepted: 4090 |
Description
Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.
Input
The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as i j or as j i.
Output
Sample Input
1 0 2 3
1 2 37
2 1 17
1 2 68 3 7
1 2 19
2 3 11
3 1 7
1 3 5
2 3 89
3 1 91
1 2 32 5 7
1 2 5
2 3 7
2 4 8
4 5 11
3 5 10
1 5 6
4 2 12 0
Sample Output
0
17
16
26
代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std; const int N = ;
const int INF = 0xfffffff; int n;
int J[N][N], dist[N];
bool vis[N]; int Prim()
{
int i, j, ans=;
dist[]=;
memset(vis, , sizeof(vis));
vis[]=; for(i=; i<=n; i++)
dist[i]=J[][i]; for(i=; i<n; i++)
{
int index=;
int MIN=INF;
for(j=; j<=n; j++)
{
if(!vis[j] && dist[j]<MIN)
{
index=j;
MIN=dist[j];
}
}
vis[index]=;
ans += MIN;
for(j=; j<=n; j++)
{
if(!vis[j] && dist[j]>J[index][j])
dist[j]=J[index][j];
}
}
return ans;
} int main ()
{
while(scanf("%d", &n), n)
{
int m, i, j, a, b, t;
scanf("%d", &m); for(i=; i<=n; i++)
for(j=; j<=i; j++)
J[i][j]=J[j][i]=INF; for(i=; i<=m; i++)
{
scanf("%d%d%d", &a, &b, &t);
J[a][b]=J[b][a]=min(J[a][b], t);
} int ans=Prim(); printf("%d\n", ans);
}
return ;
}
(最小生成树) Networking -- POJ -- 1287的更多相关文章
- Networking POJ - 1287 最小生成树板子题
#include<iostream> #include<algorithm> using namespace std; const int N=1e5; struct edge ...
- Networking POJ - 1287
题目链接:https://vjudge.net/problem/POJ-1287 思路:最小生成树板子题 #include <iostream> #include <cstdio&g ...
- B - Networking - poj 1287
有一些地方需要铺盖电缆,这些地方两点间的路可能不止一条,需要求出来至少需要多少电缆才能让所有的点都连接起来,当然,间接连接也算. /////////////////////////////////// ...
- POJ 1287 Networking (最小生成树)
Networking Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit S ...
- ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法
题目链接:problemCode=1372">ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds ...
- poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题
poj 1251 && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...
- POJ.1287 Networking (Prim)
POJ.1287 Networking (Prim) 题意分析 可能有重边,注意选择最小的边. 编号依旧从1开始. 直接跑prim即可. 代码总览 #include <cstdio> #i ...
- POJ 1287 Networking (最小生成树)
Networking 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/B Description You are assigned ...
- POJ 1287:Networking(最小生成树Kruskal)
id=1287">Networking Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5976 Acce ...
随机推荐
- Python 3 学习笔记(2)
输入输出 str() 函数人类可读,repr() 解释器可读 rjust() 靠右,ljust() 靠左,center() 居中.zfill() 填0. 名字空间 名字到对象的映射关系被称为名字空间. ...
- Java Magic. Part 1: java.net.URL
Java Magic. Part 1: java.net.URL @(Base)[JDK, url, magic, 黑魔法] 英文原文 转载请写明:原文地址 系列文章: -Java Magic. Pa ...
- Mysql 5.8安装报错
1 mysql安装过程中报下面这个错 解决方法:只要将目录改成反斜杠即可. 2 修改密码错误:ERROR 1064 (42000): You have an error in your SQL syn ...
- 尴尬!Jsp内置对象
今天挺尴尬的,上网络安全课做错了ppt ,尴尬到头皮发麻. JSP内置对象 JSP内置对象是Web容器创建的一组对象,不使用new关就可以使用的内置对象. <%int[ ]value= {60, ...
- java heap space解决方法和JVM参数设置
在JVM中如果98%的时间是用于GC(Garbage Collection)且可用的 Heap size 不足2%的时候将抛出异常信息,java.lang.OutOfMemoryError: Java ...
- Scrapy简单入门及实例讲解-转载
Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的, 也可以 ...
- 这两天的刷机+Root
最近为了研究Xposed框架的搭建使用,将自己红米Note2尝试root后,开不了机了.后使用(网上下载的线刷宝,刷的官网线刷宝成功开机了,然后里面会自带很多软件,然后通过下载KingRoot将手机自 ...
- Oracle完全卸载
停用oracle服务:进入计算机管理,在服务中,找到oracle开头的所有服务,右击选择停止 在开始菜单中,找到Universal Installer,运行Oracle Universal I ...
- xcode10 出现 框架 或者 pod 出错
1. 报错 Showing Recent Messages :-1: Multiple commands produce '/Users/apple/Library/Developer/Xcode/D ...
- Swift 项目中可能用到的第三方框架
这里记录下swift开发中可能用的框架 , 最近浏览了不少,积累在这里,以后用的时候方便查阅.顺便推荐给大家! 这里的框架都是纯swift的 , 感谢开源 ,感谢大神们 . 下拉刷新 BreakOut ...