POJ-2421 Constructing Roads---确定部分边的MST
题目链接:
https://vjudge.net/problem/POJ-2421
题目大意:
还是给你n个点,然后求最小生成树。特殊之处在于有一些点之间已经连上了边。
思路:
和POJ-1751一样的,将已有的边的权值设置成0即可
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<sstream>
using namespace std;
typedef long long ll;
const int maxn = 2e2 + ;
const int INF = << ;
int dir[][] = {,,,,-,,,-};
int T, n, m, x;
int Map[maxn][maxn];//存图
int lowcost[maxn], mst[maxn];
void prim(int u)//最小生成树起点
{
int sum_mst = ;//最小生成树权值
for(int i = ; i <= n; i++)//初始化两个数组
{
lowcost[i] = Map[u][i];
mst[i] = u;
}
mst[u] = -;//设置成-1表示已经加入mst
for(int i = ; i <= n; i++)
{
int minn = INF;
int v = -;
//在lowcost数组中寻找未加入mst的最小值
for(int j = ; j <= n; j++)
{
if(mst[j] != - && lowcost[j] < minn)
{
v = j;
minn = lowcost[j];
}
}
if(v != -)//v=-1表示未找到最小的边,
{//v表示当前距离mst最短的点
//printf("%d %d %d\n", mst[v], v, lowcost[v]);//输出路径
mst[v] = -;
sum_mst += lowcost[v];
for(int j = ; j <= n; j++)//更新最短边
{
if(mst[j] != - && lowcost[j] > Map[v][j])
{
lowcost[j] = Map[v][j];
mst[j] = v;
}
}
}
}
//printf("weight of mst is %d\n", sum_mst);
cout<<sum_mst<<endl;
}
int main()
{
cin >> n;
for(int i = ; i <= n; i++)
{
for(int j = ; j <= n; j++)cin >> Map[i][j];
}
int x, y;
cin >> m;
while(m--)
{
cin >> x >> y;
Map[x][y] = Map[y][x] = ;
}
prim();
return ;
}
POJ-2421 Constructing Roads---确定部分边的MST的更多相关文章
- 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 解题报告
题目链接:http://poj.org/problem?id=2421 实际上又是考最小生成树的内容,也是用到kruskal算法.但稍稍有点不同的是,给出一些已连接的边,要在这些边存在的情况下,拓展出 ...
- 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(最小生成树)
Description There are N villages, which are numbered from 1 to N, and you should build some roads su ...
- 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
题意:要在n个城市之间建造公路,使城市之间能互相联通,告诉每个城市之间建公路的费用,和已经建好的公路,求最小费用. 解法:最小生成树.先把已经建好的边加进去再跑kruskal或者prim什么的. 代码 ...
- [kuangbin带你飞]专题六 最小生成树 POJ 2421 Constructing Roads
给一个n个点的完全图 再给你m条道路已经修好 问你还需要修多长的路才能让所有村子互通 将给的m个点的路重新加权值为零的边到边集里 然后求最小生成树 #include<cstdio> #in ...
随机推荐
- Mycat 读写分离详解
Mycat 的读写分离是依赖数据库级别的数据主从同步的基础上来实现的(Mysql 的主从配置链接),Mycat 的读写分离是在 schema.xml 配置的 dataHost 节点的 balance ...
- Algorithm --> 投资组和求最大利润
投资组和求最大利润 题目: 投资人出资一笔费用mount,投资给不同的公司(A,B,C....),求最大获取利润? 例如:投资400百万,给出两家公司A和B: 1.如果投资一百万给A公司,投资3百万给 ...
- Algorithm --> 阶乘和因子
阶乘和因子 要求:输入正整数n(1<n <101), 把阶乘n!=1x2x3x...xn分解成素因子相乘的形式,从小到大输出各个素数(2,3,5,...)的指数. 例如825=3x52x1 ...
- Oracle查询优化改写--------------------范围处理
一.定位连续值的范围 二.查找同一组或分区中行之间的差
- DevOps实践之Gitlab安装部署
All GitLab packages are posted to our package server and can be downloaded. We maintain five repos: ...
- 第2次作业:Wechat创作史
Wechat创作史 比尔盖茨曾经说过一句话:21世纪要么电子商务,要么无商可务. 2.1 介绍产品相关信息 -the information about Wechat 你选择的产品是? 选择微信作 ...
- C语言博客作业--一二维数组
一.PTA实验作业 题目1(7-6) (1).本题PTA提交列表 (2)设计思路 //天数n:数组下标i:小时数h,分钟数m:对应书号的标签数组flag[1001] //总阅读时间sum初始化为0,借 ...
- Alpha冲刺Day2
Alpha冲刺Day2 一:站立式会议 今日安排: 首先完善前一天的剩余安排工作量,其次我们把项目大体分为四个模块:数据管理员.企业人员.第三方机构.政府人员.数据管理员这一模块,数据管理员又可细分为 ...
- 第14、15週PTA題目的處理
題目1 選擇法排序 1.實驗代碼 #include <stdio.h> #include <stdlib.h> int main() { int n,index,exchang ...
- Android webview Mixed Content无法显示图片解决
转自:http://blog.csdn.net/crazy_zihao/article/details/51557425 前言 在使用WebView加载https资源文件时,如果认证证书不被Andro ...