HDU 1102 Constructing Roads (最小生成树)
最小生成树模板(嗯……在kuangbin模板里面抄的……)
最小生成树(prim)
/** Prim求MST
* 耗费矩阵cost[][],标号从0开始,0~n-1
* 返回最小生成树的权值,返回-1表示原图不连通
*/
const int INF = 0x3f3f3f3f;
const int MAXN = 110; bool vis[MAXN];
int lowc[MAXN];
int map[MAXN][MAXN]; int Prim(int cost[][MAXN], int n)
{
int ans = 0;
memset(vis, false, sizeof(vis));
vis[0] = true;
for (int i = 1; i < n; ++i) lowc[i] = cost[0][i];
for (int i = 1; i < n; ++i) {
int minc = INF;
int p = 1;
for (int j = 0; j < n; ++j)
if (!vis[j] && minc > lowc[j]) {
minc = lowc[j];
p = j;
}
if (minc == INF) return -1;
ans += minc;
vis[p] = true;
for (int j = 0; j < n; ++j)
if (!vis[j] && lowc[j] > cost[p][j])
lowc[j] = cost[p][j];
}
return ans;
}
感觉Prim和Dijkstra有点像。写起来挺简单的。
我一开始的想法是把每个q中的a,b设为已访问节点,后来发现不对,例如ab连,dc连,但它们并不是全部相连的。
好吧其实此题就是把已建好两点之间距离设为0.
//Problem : 1102 ( Constructing Roads ) Judge Status : Accepted
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int N = 105;
const int INF = 3000; int map[N][N];
int vis[N];
int dis[N]; int Prim(int n)
{
int ans = 0;
memset(vis, 0, sizeof(vis));
for (int j = 1; j <= n; ++j) {
dis[j] = map[1][j];
}
for (int k = 1; k <= n; ++k) {
int minc = INF;
int p = 1;
for (int i = 1; i <= n; ++i) {
if (!vis[i] && dis[i] < minc) {
minc = dis[i];
p = i;
}
}
if (minc == INF) return -1;
ans += minc;
vis[p] = 1;
for (int j = 1; j <= n; ++j) {
if (map[p][j] < dis[j] && !vis[j])
dis[j] = map[p][j];
}
}
return ans;
} int main()
{
int n, q;
while (cin >> n) {
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
cin >> map[i][j];
cin >> q;
int a, b;
for (int i = 0; i < q; ++i) {
cin >> a >> b;
map[a][b] = map[b][a] = 0;
}
cout << Prim(n) << endl;
}
return 0;
}
HDU 1102 Constructing Roads (最小生成树)的更多相关文章
- hdu 1102 Constructing Roads(最小生成树 Prim)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...
- (step6.1.4)hdu 1102(Constructing Roads——最小生成树)
题目大意:输入一个整数n,表示村庄的数目.在接下来的n行中,每行有n列,表示村庄i到村庄 j 的距离.(下面会结合样例说明).接着,输入一个整数q,表示已经有q条路修好. 在接下来的q行中,会给出修好 ...
- HDU 1102 Constructing Roads(最小生成树,基础题)
注意标号要减一才为下标,还有已建设的路长可置为0 题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<str ...
- HDU 1102 Constructing Roads, Prim+优先队列
题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...
- HDU 1102(Constructing Roads)(最小生成树之prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...
- hdu 1102 Constructing Roads (最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- hdu 1102 Constructing Roads (Prim算法)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- hdu 1102 Constructing Roads Kruscal
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...
- HDU 1102 Constructing Roads
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- NSAssert使用摘抄
#define NSAssert(condition, desc, ...) 只有条件condition满足,才会执行下一个语句,否则输出断言错误. 例如: NSAssert(1 != 2, @&qu ...
- 【web安全】第二弹:XSS攻防中的复合编码问题
最近一直在研究XSS的攻防,特别是dom xss,问题慢慢的迁移到浏览器编码解码顺序上去. 今儿被人放鸽子,无奈在KFC看了两个小时的资料,突然有种豁然开朗的感觉. 参考资料先贴出来: 1. http ...
- 百度:在O(1)空间复杂度范围内对一个数组中前后连段有序数组进行归并排序
一.题目理解 题目:数组al[0,mid-1]和al[mid,num-1]是各自有序的,对数组al[0,num-1]的两个子有序段进行merge,得到al[0,num-1]整体有序.要求空间复杂度为O ...
- U3D版本《暗黑世界V1.0》编译——图文教程!
原地址:http://blog.csdn.net/uxqclm/article/details/11970773 欢迎来到9秒:www.9miao.com 说明: A. 工具准备: ...
- Es索引优化
https://www.elastic.co/guide/en/elasticsearch/guide/current/hardware.html https://www.elastic.co/gui ...
- HDU 5044 TREE
题意:一棵树上两种操作,操作1,改变u到v的每一点的值增加k,操作2,改变u到v每一条边值增加k.最后结束时问,每一点和每一条边的值. 初始时,点和边的值都为0. 分析: 很显然要用树链剖分,将点和边 ...
- TC SRM 607 DIV2
求拼接完成后的字符串包含的子回文串的数目,一开始还用暴力去做,想都不用想 肯定超时了. 复习了一下求最长子回文串的算法,发现可以类似解决. 给相邻字符之间添加一个'@'字符,这样所有的回文串都是奇数长 ...
- 转载爱哥自定义View系列--文字详解
FontMetrics FontMetrics意为字体测量,这么一说大家是不是瞬间感受到了这玩意的重要性?那这东西有什么用呢?我们通过源码追踪进去可以看到FontMetrics其实是Paint的一个内 ...
- Oracle EBS R12 WIP Component Issue&Return Process
oracleassemblytransactionscomponentsjobsreference 目录(?)[-] 定义BOM 定义Routing 定义WIP Discrete Job 发料 Mat ...
- 先贴出代码C++ 中的单例模式
先贴出代码,代码后面是讲解 自己编写的单例模式: #include <iostream> #include <stdio.h> #include <string> ...