POJ2421 Constructing Roads【最小生成树】
题意:
有N个点,有些点已经连接了,然后求出所有点的连接的最短路径是多少。
思路:
最小生成树的变形,有的点已经连接了,就直接把他们的权值赋为0,一样的就做最小生成树。
代码:
prime:
#include <cstdio>
#include <iostream> using namespace std;
#define maxn 100+5
#define inf 0x3f3f3f3f int maps[maxn][maxn];
bool vis[maxn];
int dis[maxn];
int n; int Prim()
{
for(int i=;i<=n;i++)
dis[i] = inf;
int sum=;
dis[] = ;
for(int i=;i<=n;i++)
{
int tmp = inf,k=;
for(int j=;j<=n;j++)
{
if(!vis[j]&&dis[j]<tmp)
{
tmp = dis[j];
k=j;
}
}
vis[k] = true;
sum+=tmp;
for(int i=;i<=n;i++)
{
if(!vis[i]&&dis[i]>maps[k][i])
dis[i] = maps[k][i];
}
}
return sum;
} int main()
{
cin>>n;
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
cin>>maps[i][j];
}
}
int k;
cin>>k;
for(int i=; i<k; i++)
{
int a,b;
cin>>a>>b;
maps[a][b]=maps[b][a] = ;
}
cout<<Prim()<<endl;
return ;
}
POJ2421 Constructing Roads【最小生成树】的更多相关文章
- POJ2421 Constructing Roads 最小生成树
修路 时限: 2000MS 内存限制: 65536K 提交总数: 31810 接受: 14215 描述 有N个村庄,编号从1到N,您应该修建一些道路,使每两个村庄可以相互连接.我们说两个村庄A ...
- hdu Constructing Roads (最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 /************************************************* ...
- HDU 1102 Constructing Roads (最小生成树)
最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...
- HDU1102&&POJ2421 Constructing Roads 2017-04-12 19:09 44人阅读 评论(0) 收藏
Constructing Roads Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) ...
- POJ2421 Constructing Roads
Constructing Roads 这道题很水,就是一个裸的最小生成树,最不过把已经连接的节点的值再设为0. 代码: #include<cstdio> #include<cstri ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- 【kruscal】【最小生成树】poj2421 Constructing Roads
SB题,求最小生成树,其中有些边已经给您建好啦. 随意暴力即可. #include<cstdio> #include<algorithm> #include<cstrin ...
- POJ - 2421 Constructing Roads (最小生成树)
There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...
- hdu 1102 Constructing Roads(最小生成树 Prim)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...
随机推荐
- MySQL在Read Uncommitted级别下写操作加X锁
很多文章认为MySQL在读未提交(Read Uncommitted)的隔离级别下,写操作是不加锁的,然而实际上并不是,在RU级别下,写操作加有X锁. 实践出真知 以前,我也认为RU隔离级别下,写操作不 ...
- python之tkinter使用-滚动条
# GUI:tkinter使用 # 通过调节滚动条改变标签中字体大小 import tkinter as tk def resize(ev=None): '''改变label字体大小''' label ...
- BZOJ3653谈笑风生——可持久化线段树+dfs序
题目描述 设T 为一棵有根树,我们做如下的定义: ? 设a和b为T 中的两个不同节点.如果a是b的祖先,那么称“a比b不知道 高明到哪里去了”. ? 设a 和 b 为 T 中的两个不同节点.如果 a ...
- 图之强连通、强连通图、强连通分量 Tarjan算法
原文地址:https://blog.csdn.net/qq_16234613/article/details/77431043 一.解释 在有向图G中,如果两个顶点间至少存在一条互相可达路径,称两个顶 ...
- 【HDU5778】abs(数学)
BUPT2017 wintertraining(16) #4 C HDU - 5778 题意 给定x,找出使|y-x|最小,且每个质因子都出现两次的y(\(y\le 2\))50组测试数据,\(1\l ...
- 自学Linux Shell2.2-GHOME Terminal仿真器
点击返回 自学Linux命令行与Shell脚本之路 2.2-GHOME Terminal仿真器 GNOME Terminal是GNOME桌面环境的默认终端仿真器.很多发行版,如RHEL.Fedora和 ...
- 自学Zabbix2.3-服务器端server安装过程
点击返回:自学Zabbix之路 ....
- 自学Zabbix12.1 Zabbix命令-zabbix_server
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix12.1 Zabbix命令-zabbix_server 1. zabbix核心:z ...
- 单片机I/O口的结构的详解
1.什么是源型 漏型?什么是上拉电阻?下拉电阻?什么是 线驱动输出 集电极开路输出,推挽式输出? 我们先来说说集电极开路输出的结构.集电极开路输出的结构如图1所示,右边的那个三极管集电极什么都不接,所 ...
- HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)
HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...