POJ - 2421 Constructing Roads (最小生成树)
We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
Input
Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
Output
Sample Input
3
0 990 692
990 0 179
692 179 0
1
1 2
Sample Output
179
解题思路:
要修公路,输入一个n,表示n个村庄。接着输入n*n的矩阵,然后输入一个q 接下来的q行
每行包含两个数a,b,表示a、b这条边联通,就是已经有公路不用修了,要让所有村庄联通在一起问:修路最小代价?
最小生成树的变形,有的村庄已经连接了,就直接把他们的权值赋为0,一样的做最小生成树,Prim算法。
代码如下:
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std; int N;
int dis[][];
bool vis[][];
struct edge{
int u;
int v;
int w;
}e[];
int pre[];
int find(int x)
{
return (x==pre[x])?x:pre[x] = find(pre[x]);
}
bool cmp(edge a , edge b)
{
return a.w<b.w;
}
int Q;
int main()
{
scanf("%d",&N);
for(int i = ; i <= N;i++)
{
pre[i] = i;
}
for(int i = ; i <= N ;i++)
{
for(int j = ; j <= N ;j++)
{
scanf("%d",&dis[i][j]);
}
} int x , y;
scanf("%d",&Q);
int edge_num = ;
while(Q--)
{
scanf("%d%d",&x,&y);
dis[x][y] = ;
dis[y][x] = ;
}
for(int i = ;i <= N ;i++)
{
for(int j = ; j <= N; j++)
{ e[edge_num].u = i;
e[edge_num].v = j;
e[edge_num].w = dis[i][j];
edge_num++;
}
} sort(e,e+edge_num,cmp);
int u , v , w;
int fx ,fy;
int ans = ;
for(int i = ; i < edge_num ;i++)
{
u = e[i].u;
v = e[i].v;
w = e[i].w; fx = find(u);
fy = find(v); if(fx!=fy)
{
pre[fx] = fy;
ans += w;
}else
{
continue;
}
}
printf("%d\n",ans);
return ;
}
POJ - 2421 Constructing Roads (最小生成树)的更多相关文章
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- POJ 2421 Constructing Roads (最小生成树)
Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...
- POJ - 2421 Constructing Roads 【最小生成树Kruscal】
Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...
- POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )
Constructing Roads Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19884 Accepted: 83 ...
- POJ 2421 Constructing Roads(最小生成树)
Description There are N villages, which are numbered from 1 to N, and you should build some roads su ...
- [kuangbin带你飞]专题六 最小生成树 POJ 2421 Constructing Roads
给一个n个点的完全图 再给你m条道路已经修好 问你还需要修多长的路才能让所有村子互通 将给的m个点的路重新加权值为零的边到边集里 然后求最小生成树 #include<cstdio> #in ...
- Poj 2421 Constructing Roads(Prim 最小生成树)
题意:有几个村庄,要修最短的路,使得这几个村庄连通.但是现在已经有了几条路,求在已有路径上还要修至少多长的路. 分析:用Prim求最小生成树,将已有路径的长度置为0,由于0是最小的长度,所以一定会被P ...
- POJ - 2421 Constructing Roads(最小生成树&并查集
There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...
- poj 2421 Constructing Roads 解题报告
题目链接:http://poj.org/problem?id=2421 实际上又是考最小生成树的内容,也是用到kruskal算法.但稍稍有点不同的是,给出一些已连接的边,要在这些边存在的情况下,拓展出 ...
随机推荐
- wamp 初始化 修改mysql密码
1.设置phpmyadmin 在WampServer安装完成后,通过http://localhost/打开后可以看到WampServer自带的一个简单的页面,里面有phpinfo.phpmyadmin ...
- 微信小程序相关一、模仿京东静态登录页面
一.培训的第一天,基本上没有什么最新的东西,但是看到老师的代码收获的确实是不少. 1.1.首页代码很简洁,将共有的样式都提取的很好. 1.2.其次是每一个小块写样式的时候用到了好多子代选择器和后代选择 ...
- cdoj32-树上战争(Battle on the tree) 【记忆化搜索】
http://acm.uestc.edu.cn/#/problem/show/32 树上战争(Battle on the tree) Time Limit: 12000/4000MS (Java/Ot ...
- svn服务,svnserve开机启动
在服务器(我的环境是centos6.5)上部署完svn之后,一般都希望每次重启的时候svn服务能够随着启动.上网搜索了一下,一般有两种做法: 一.启动脚本+/etc/rc.local启动 1. 在某个 ...
- Python学习笔记_从CSV读取数据写入Excel文件中
本示例特点: 1.读取CSV,写入Excel 2.读取CSV里具体行.具体列,具体行列的值 一.系统环境 1. OS:Win10 64位英文版 2. Python 3.7 3. 使用第三方库:csv. ...
- 线上应用故障排查:高CPU占用
转自:hankchen,http://www.blogjava.net/hankchen 一个应用占用CPU很高,除了确实是计算密集型应用之外,通常原因都是出现了死循环. 以我们最近出现的一个实际故障 ...
- 基于weui的一个小插件
移动端项目当中大量的使用了weui,为了减少工作量,方便修改,自己写了个小插件,暂时只有toast和dialog部分,可能会更新actionSheet等其他部分 更新一个手机端预览的二维码,就直接放项 ...
- [C++] Memory Retrieval(内存检索)
Traverse the memory by (char*) , because every time it will increase by 1byte when i want get the i ...
- phpmailer配置qq邮箱
function send_email2($email = '*****@perspectivar.com'){ $this->autoRender = false; date_default_ ...
- C#中 Thread,Task,Async/Await,IAsyncResult 的那些事儿![转载]
说起异步,Thread,Task,async/await,IAsyncResult 这些东西肯定是绕不开的,今天就来依次聊聊他们 1.线程(Thread) 多线程的意义在于一个应用程序中,有多个执行部 ...