(最小生成树) 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 ...
随机推荐
- TortoiseGit 使用 HTTP 方式每次 PUSH 无需输入密码的方法
由于 BitBucket 被墙,导致使用时只能用HTTPS代理的方式,但TortoiseGit貌似没有记忆密码的功能,以至于每次push时都要求输入密码,很是麻烦!在网上搜到的保存密码的方式也有点笨. ...
- JPEG和Variant的转换
unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, ...
- 吴裕雄 实战python编程(1)
import sqlite3 conn = sqlite3.connect('E:\\test.sqlite') # 建立数据库联接cursor = conn.cursor() # 建立 cursor ...
- python处理分隔大文件
4个.sql格式的文件,2G大小,直接插入mysql数据中,文件太大了,导入不进去. 太大的文件用python处理也很麻烦,处理不了,只能先分隔成小文件处理. 文件中数据格式:其中values里面的数 ...
- Mysql 内部默认排序
mysql默认的排序: https://forums.mysql.com/read.php?21,239471,239688#msg-239688 Do not depend on order whe ...
- Spring Boot中使用Websocket搭建即时聊天系统
1.首先在pom文件中引入Webscoekt的依赖 <!-- websocket依赖 --> <dependency> <groupId>org.springfra ...
- nginx accept() failed (24: Too many open files)
nginx服务器出现如下信息: [crit] 17221#0: accept4() failed (24: Too many open files) [crit] 17221#0: accept4() ...
- Spring框架的JDBC模板技术概述
1. Spring框架中提供了很多持久层的模板类来简化编程,使用模板类编写程序会变的简单 2. 提供了JDBC模板,Spring框架提供的 * JdbcTemplate类 3. Spring框架可以整 ...
- Spring框架之log日志的使用
1.Spring框架也需要引入日志相关的jar包 * 在spring-framework-3.0.2.RELEASE-dependencies/org.apache.commons/com.sprin ...
- Linq和EF 做 单一条件查询 和 复合条件 查询 以及 多表 联合查询 示例
单一条件查询: var table2Object = (from t1 in db.table1 join t2 in db.table2 on t1.id equals t2.id select t ...