Constructing Roads POJ - 2421
题目链接:https://vjudge.net/problem/POJ-2421
思路:一些村庄,建一些路,使得所有村庄能相连,而且使得所有路长度之和最短。
题目说了,有些村庄之间已经建了路,说明有些路我们不需要建,那么在预处理的时候
把那些已经建过边的两个村庄的距离赋值为0,那么在跑最小生成树板子的时候就完成了
一些路已经建立的情况。
#include <stdio.h>
#include <iostream>
#include <queue>
using namespace std; const int inf = (int)1e9;
const int N = ;
int g[N][N];
int dis[N];
bool vis[N];
int n; struct node{
int loc;
int v; bool friend operator<(const node& a,const node& b){
return a.v > b.v;
}
}; priority_queue<node > que; int prime(){ que.push(node{,});
dis[] = ; while(!que.empty()){
int u = que.top().loc;
que.pop(); vis[u] = true; for(int v = ; v <= n; v++){
if(!vis[v] && dis[v] > g[u][v]){
dis[v] = g[u][v];
que.push(node{v,dis[v]});
}
}
} int ans = ;
for(int i = ; i <= n; i++){ // printf("%d ",dis[i]);
ans += dis[i];
} // printf("\n"); return ans;
} int main(){ scanf("%d",&n); for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
scanf("%d",&g[i][j]); for(int i = ; i <= n; i++)
dis[i] = inf; int m;
scanf("%d",&m); int u,v;
for(int i = ; i <= m; i++){
scanf("%d%d",&u,&v);
g[u][v] = g[v][u] = ;//已经有路的村庄
} printf("%d\n",prime()); return ;
}
Constructing Roads POJ - 2421的更多相关文章
- (最小生成树)Constructing Roads -- poj -- 2421
链接: http://poj.org/problem?id=2421 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 2113 ...
- Constructing Roads POJ - 2421 (最小生成树)
思路:首先使用二维数组dis[][]处理输入, 对于已经修好的路,将其对应的dis[i][j]置为零即可.最后再将 所有的dis[][]保存到边结构体中,使用Kruskal算法求得最小生成树. ...
- Constructing Roads POJ - 2421 最小生成树板子题
#include<iostream> #include<cstring> #include<algorithm> using namespace std; ; in ...
- 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 ...
- Constructing Roads——F
F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...
- Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)
Constructing Roads In JGShining's Kingdom HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...
随机推荐
- 01 《i》控制字体大小 v-for循环绑定类名 v-bind 结合三目运算 动态添加类
1==>控制字体图标的大小用 font-size:16px; <i class="el-icon-arrow-left right-show-aside-icon"&g ...
- excel工资表,转化为word工资单
1.新建excel表,.xls格式; .xlsx格式的添加到word中会报错‘无法打开数据源’(office 2007): 2.新建word文档.docx格式: 3.邮件——开始邮件合并——信函,选择 ...
- python 异常try/except语句
异常模式的写法 try: 执行正常的模块 except X: 执行异常X的代码 except: 其他的异常执行模块except 必须在except X之后 else: 没有异常,则会执行完try,而后 ...
- EM-高斯混合模型
EM-高斯混合模型 认识 前面为了直观认识 EM 算法, 用的"扔硬币"的案例, 是为了简化和直观, 而稍微偏应用和深入一点是高斯模型分类,这样一个话题. 就好比我们现在有一堆的数 ...
- CF1178D Prime Graph
题目链接 题意 构造一张有\(n(3\le n\le 1000)\)个点的无向图(无重边和自环).满足: 边的总数为素数 所有点的度数均为素数 输出方案 solution 如果所有点的度数确定了.那么 ...
- umi+dva+antd新建项目
首先全局安装dva+umiumi:npm install -g umidva:npm install -g dva-cli 通过脚手架创建项目 一: mkdir myapp && cd ...
- 百度APP移动端网络深度优化实践分享(三):移动端弱网优化篇
本文由百度技术团队“蔡锐”原创发表于“百度App技术”公众号,原题为<百度App网络深度优化系列<三>弱网优化>,感谢原作者的无私分享. 一.前言 网络优化解决的核心问题有三个 ...
- mysql 创建用户, 分配权限, 删除用户
通过create user 命令来创建用户, 有两种方式:(只介绍通过 create user 命令, 直接往user表中插入数据的方式,这里就不说了) 创建用户的同时, 指定用户可登录的主机和密码 ...
- LeetCode 234:回文链表 Palindrome Linked List
请判断一个链表是否为回文链表. Given a singly linked list, determine if it is a palindrome. 示例 1: 输入: 1->2 输出: ...
- 【shell脚本】批量修改扩展名===modifyExtension.sh
前提:需切换到需要批量修改扩展名的目录下,运行脚本 [root@VM_0_10_centos shellScript]# cat modifyExtension.sh #!/bin/bash # 编写 ...