Networking

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 19674   Accepted: 10061

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

Source

 
本题思路:最小生成树模版题,所以这就用了三种方法来练习自己的模版能力。
 
Kruskal算法参考代码:
 #include <cstdio>
#include <algorithm>
using namespace std; const int maxp = + , maxr = * / + ;
int p, r, ans, head[maxp], Rank[maxp];
struct Edge {
int u, v, w;
}edge[maxr]; void Make_Set() {
for(int i = ; i <= p; i ++) {
head[i] = i;
Rank[i] = ;
}
ans = ;
} int Find(int u) {
if(u == head[u]) return u;
return head[u] = Find(head[u]);
} void Union(int u, int v) {
int fu = Find(u), fv = Find(v);
if(fu == fv) return;
if(Rank[fu] > Rank[fv])
head[fv] = fu;
else {
head[fu] = fv;
if(Rank[fu] == Rank[fv]) Rank[fv] += ;
}
} bool cmp(Edge a, Edge b) {
return a.w < b.w;
} bool Is_same(int u, int v) {
return Find(u) == Find(v);
} void Kruskal() {
sort(edge + , edge + r + , cmp);
int cnt = ;
Make_Set();
for(int i = ; i <= r; i ++) {
if(!Is_same(edge[i].u, edge[i].v)) {
cnt ++;
ans += edge[i].w;
Union(edge[i].u, edge[i].v);
}
if(cnt == p - ) return;
}
} int main () {
while(~scanf("%d", &p) && p) {
scanf("%d", &r);
for(int i = ; i <= r; i ++) {
scanf("%d %d %d", &edge[i].u, &edge[i].v, &edge[i].w);
}
Kruskal();
printf("%d\n", ans);
}
return ;
}

Prim + 邻接矩阵

 #include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std; const int maxp = + , maxr = * / + , INF = 0x3f3f3f3f;
int p, r, ans, dist[maxp], G[maxp][maxp];
bool vis[maxp]; void Init() {
ans = ;
memset(vis ,false, sizeof vis);
for(int i = ; i <= p; i ++) {
for(int j = ; j <= p; j ++)
G[i][j] = INF;
}
} void prim(int source) {
dist[source] = ;
vis[source] = true;//初始状态下只有source为已经安装了network的点
for(int i = ; i <= p; i ++)
dist[i] = G[source][i];//初始化所有distance为source到他们的距离
for(int i = ; i <= p; i ++) {
int MIN = INF, k = -;
for(int j = ; j <= p; j ++) {//每次选择那个距离子最小生成树所有结点权值最小的结点,并将其连接Network
if(!vis[j] && MIN > dist[j]) {
k = j;
MIN = dist[j];
}
}
if(MIN == INF) return;//没找到就说明该此时已经没有可以探索的结点了
ans += MIN;
vis[k] = true;
for(int j = ; j <= p; j ++) {
if(!vis[j] && dist[j] > G[k][j])
dist[j] = G[k][j];//对于新增的结点k,动态更新最小生成树内结点到他们结点相邻的权值,很显然意思就是每新增一个结点就看是否此时会有一条更进的边可以到达j结点
}
}
} int main () {
int a, b, w;
while(~scanf("%d", &p) && p) {
scanf("%d", &r);
Init();
for(int i = ; i <= r; i ++) {
scanf("%d %d %d", &a, &b, &w);
if(w < G[a][b]) {//选择权值最小的那条边
G[a][b] = G[b][a] = w;
}
}
prim();
printf("%d\n", ans);
}
return ;
}

Prim + 最小堆优化 + 邻接表

这里堆是用STL优先队列实现的,我比较懒emm...(学了这么多需要堆优化的算法,结果现在连个最基本的堆都不会写,算法导论上说斐波纳挈堆优化的Prim超级快,所以打完国赛我会总结堆 + 斐波纳挈堆

还会更新出他们优化的算法)。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std; typedef pair <int, int> pii;
struct Edge {
int to, cost;
friend bool operator < (const Edge &a, const Edge &b) {
return a.cost > b.cost;
}
};
const int maxp = + , maxr = * / + , INF = 0x3f3f3f3f;
int p, r, ans, dist[maxp];
bool vis[maxp];
vector <Edge> edge[maxp]; void addedge(int u, int v, int w) {
edge[u].push_back({v, w});
} void Queue_Prim(int source) {
memset(vis, false, sizeof vis);
for(int i = ; i <= p; i ++) dist[i] = INF;
dist[] = ans = ;
priority_queue <Edge> Q;
Q.push({source, dist[source]});
while(!Q.empty()) {
Edge now = Q.top();
Q.pop();
if(vis[now.to]) continue;
vis[now.to] = true;
ans += now.cost;
for(unsigned int i = ; i < edge[now.to].size(); i ++) {
int v = edge[now.to][i].to;
if(dist[v] > edge[now.to][i].cost) {
dist[v] = edge[now.to][i].cost;
Q.push({v, dist[v]});
}
}
}
} int main () {
int a, b, c;
while(~scanf("%d", &p) && p) {
scanf("%d", &r);
for(int i = ; i < r; i ++) {
scanf("%d %d %d", &a, &b, &c);
addedge(a, b, c);
addedge(b, a, c);
}
Queue_Prim();
for(int i = ; i <= p; i ++) edge[i].clear();
printf("%d\n", ans);
}
return ;
}

POJ-1287.Network(Kruskal + Prim + Prim堆优化)的更多相关文章

  1. 求最小生成树(暴力法,prim,prim的堆优化,kruskal)

    求最小生成树(暴力法,prim,prim的堆优化,kruskal) 5 71 2 22 5 21 3 41 4 73 4 12 3 13 5 6 我们采用的是dfs的回溯暴力,所以对于如下图,只能搜索 ...

  2. Prim算法堆优化

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> ...

  3. hiho一下 第二十九周 最小生成树三·堆优化的Prim算法【14年寒假弄了好长时间没搞懂的prim优化:prim算法+堆优化 】

    题目1 : 最小生成树三·堆优化的Prim算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 回到两个星期之前,在成功的使用Kruscal算法解决了问题之后,小Ho产生 ...

  4. Electrification Plan 最小生成树(prim+krusl+堆优化prim)

    题目 题意: 无向图,给n个城市,n*n条边,每条边都有一个权值 代表修路的代价,其中有k个点有发电站,给出这k个点的编号,要每一个城市都连到发电站,问最小的修路代价. 思路: prim:把发电站之间 ...

  5. POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)

    Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14021   Accepted: 5484   Specia ...

  6. POJ 1861 Network (Kruskal求MST模板题)

    Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14103   Accepted: 5528   Specia ...

  7. 图论之堆优化的Prim

    本题模板,最小生成树,洛谷P3366 题目描述 如题,给出一个无向图,求出最小生成树,如果该图不连通,则输出orz 输入输出格式 输入格式: 第一行包含两个整数N.M,表示该图共有N个结点和M条无向边 ...

  8. 图论——最小生成树:Prim算法及优化、Kruskal算法,及时间复杂度比较

    最小生成树: 一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边.简单来说就是有且仅有n个点n-1条边的连通图. 而最小生成树就是最小权 ...

  9. 在 Prim 算法中使用 pb_ds 堆优化

    在 Prim 算法中使用 pb_ds 堆优化 Prim 算法用于求最小生成树(Minimum Spanning Tree,简称 MST),其本质是一种贪心的加点法.对于一个各点相互连通的无向图而言,P ...

随机推荐

  1. mssql 取数据指定条数(例:100-200条的数据)

    方法1:临时表 * into #aa from table order by time-- 将top m笔插入 临时表 select * from #aa order by time desc --d ...

  2. linux权限管理—基本权限

    目录 Linux权限管理-基本权限 一.权限的基本概述 二.权限修改命令chmod 三.基础权限设置案例 四.属主属组修改命令chown Linux权限管理-基本权限 一.权限的基本概述 1.什么是权 ...

  3. SSM中前台传数组。后台接受的问题

    当时写得时候,忘记考虑json的jar,做个记录. 第一步:先带入jar <dependency> <groupId>com.fasterxml.jackson.core< ...

  4. mysql 乐观锁、悲观锁、共享锁,排它锁

    mysql锁机制分为表级锁和行级锁,本文就和大家分享一下我对mysql中行级锁中的共享锁与排他锁进行分享交流. 共享锁又称为读锁,简称S锁,顾名思义,共享锁就是多个事务对于同一数据可以共享一把锁,都能 ...

  5. Linux中的sshd服务

    Linux中的sshd服务,主要用于pst终端,远程连接到linux服务中 看sshd服务状态 service sshd status 停止sshd服务 service sshd stop 启动ssh ...

  6. CSS3 结构性伪类选择器(2)

    CSS3 结构性伪类选择器—first-child “:first-child”选择器表示的是选择父元素的第一个子元素的元素E.简单点理解就是选择元素中的第一个子元素,记住是子元素,而不是后代元素. ...

  7. Spring如何解决循环依赖问题

    目录 1. 什么是循环依赖? 2. 怎么检测是否存在循环依赖 3. Spring怎么解决循环依赖 本文主要是分析Spring bean的循环依赖,以及Spring的解决方式. 通过这种解决方式,我们可 ...

  8. 使用Fabric在tomcat中部署应用的问题总结

    关闭tomcat时 A.为什么调用shutdown时,报错连接拒绝 结论——很可能是因为tomcat没启动或没完全启动:而这个时候调用shutdown就会出现此类报错 解决方法:time.sleep ...

  9. Jmeter的JDBC请求执行多条SQL语句

    注:有mysqlconnector/j 3.1.1以上版本才支持执行多条sql语句 1.     下载jdbc驱动为了连接Mysql数据库,还需要有个jdbc驱动:mysql-connector-ja ...

  10. Oracle DB 查看预警日志

    “Database(数据库)”主页>“Related Links相关链接)”区域> “Alert Log Content (预警日志内容)” 查看预警日志每个数据库都有一个alert_&l ...