链接:

http://poj.org/problem?id=1287

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 7494   Accepted: 4090

Description

You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area. 
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 input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the number P of the given points, and the second the number R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers: the first two numbers identify the points, and the third gives the length of the route. The numbers are separated with white spaces. A data set giving only one number P=0 denotes the end of the input. The data sets are separated with an empty line. 
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

For each data set, print one number on a separate line that gives the total length of the cable used for the entire designed network.

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的更多相关文章

  1. Networking POJ - 1287 最小生成树板子题

    #include<iostream> #include<algorithm> using namespace std; const int N=1e5; struct edge ...

  2. Networking POJ - 1287

    题目链接:https://vjudge.net/problem/POJ-1287 思路:最小生成树板子题 #include <iostream> #include <cstdio&g ...

  3. B - Networking - poj 1287

    有一些地方需要铺盖电缆,这些地方两点间的路可能不止一条,需要求出来至少需要多少电缆才能让所有的点都连接起来,当然,间接连接也算. /////////////////////////////////// ...

  4. POJ 1287 Networking (最小生成树)

    Networking Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit S ...

  5. ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法

    题目链接:problemCode=1372">ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds     ...

  6. 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 ...

  7. POJ.1287 Networking (Prim)

    POJ.1287 Networking (Prim) 题意分析 可能有重边,注意选择最小的边. 编号依旧从1开始. 直接跑prim即可. 代码总览 #include <cstdio> #i ...

  8. POJ 1287 Networking (最小生成树)

    Networking 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/B Description You are assigned ...

  9. POJ 1287:Networking(最小生成树Kruskal)

    id=1287">Networking Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5976   Acce ...

随机推荐

  1. 【转载】 Java并发编程:深入剖析ThreadLocal

    原文链接:http://www.cnblogs.com/dolphin0520/p/3920407.html感谢作者的辛苦总结! Java并发编程:深入剖析ThreadLocal 想必很多朋友对Thr ...

  2. 迷你MVVM框架 avalonjs 学习教程22、avalon性能大揭密

    avalon之所以能在页面处理1W个绑定(angular对应的数字是2000),出于两个重要设计--基于事件驱动的双向绑定链及智能CG回收机制. avalon的双向绑定链是通过Object.defin ...

  3. Ansible 书写我的playbook

    mysql 创建数据库 - hosts: localhost  remote_user: root  tasks: - name: test mysql    mysql_db:      name: ...

  4. 部分真验货客户未取进FP IN_SALES_ORDER表有数据,前台规划页面没显示

    描述:部分真验货客户未取进FP,检查发现IN_SALES_ORDER表有数据630\600\610行项目数据,但前台只显示630数据,600和610前台没有显示 1.查看IN_SALES_ORDER表 ...

  5. mysql中树形结构表的操作

    一种是:邻接表模型(局限性:对于层次结构中的每个级别,您需要一个自联接,并且随着连接的复杂性增加,每个级别的性能自然会降低.在纯SQL中使用邻接列表模型充其量是困难的.在能够看到类别的完整路径之前,我 ...

  6. java内存模型:Happens-Before

    有序性:Java内存模型中的程序天然有序性可以总结为一句话:如果在本线程内观察,所有操作都是有序的:如果在一个线程中观察另一个线程,所有操作都是无序的.前半句是指“线程内表现为串行语义”,后半句是指“ ...

  7. poj1661 (DP)

    题目链接:http://poj.org/problem?id=1661 思路: 把初始位置看成左,右端点均为x0,即长度为0,高度为y0的一个平台,按照平台高度从低到高排序.用dp[i][0],dp[ ...

  8. Python3 enumerate() 函数

    Python3 enumerate() 函数  Python3 内置函数 描述 enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标 ...

  9. REVERSE!REVERSE!REVERSE!

    形式汇总: 206. Reverse Linked List 92. Reverse Linked List II:Given a string and an integer k, you need ...

  10. Java多线程超详细总结

    目录(?)[-] 一扩展javalangThread类 二实现javalangRunnable接口 三Thread和Runnable的区别 四线程状态转换 五线程调度 六常用函数说明 使用方式 为什么 ...