怎么说呢

这题就是个模板题

但是

hud你妹夫啊说好的只有一组数据呢???

嗯???

wa到家都不认识了好吗

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std; typedef pair<int, int> pii;
struct cmp{
bool operator() (const pii a, const pii b){
return a.first > b.first;
}
}; int n, q, val[][], dist[];
bool vis[]; int prim(int s)
{
priority_queue<pii, vector<pii>, cmp> q;
memset(vis, false, sizeof vis);
for(int i = ; i <= n; i++){
dist[i] = val[s][i];
q.push(make_pair(dist[i], i));
}
vis[s] = true;
int res = ;
while(!q.empty()){
pii u = q.top();
q.pop();
if(vis[u.second]) continue;
vis[u.second] = true;
res += u.first;
for(int i = ; i <= n; i++){
if(!vis[i] && i != u.second && val[u.second][i] < dist[i]){
dist[i] = val[u.second][i];
q.push(make_pair(dist[i], i));
}
}
}
return res;
} int main()
{
while(~scanf("%d", &n)){
for(int i = ; i <= n; i++){
for(int j = ; j <= n; j++){
scanf("%d", &val[i][j]);
}
}
scanf("%d", &q);
while(q--){
int u, v;
scanf("%d%d", &u, &v);
val[u][v] = val[v][u] = ;
}
printf("%d\n", prim());
}
return ;
}

(MST) HDOJ 1102 Constructing Roads的更多相关文章

  1. hdoj 1102 Constructing Roads

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 分析:看到这题给出的都是矩阵形式,就知道了可以用Prim算法求MST. #include <i ...

  2. Hdoj 1102.Constructing Roads 题解

    Problem Description There are N villages, which are numbered from 1 to N, and you should build some ...

  3. HDU 1102 Constructing Roads (最小生成树)

    最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...

  4. hdu 1102 Constructing Roads (Prim算法)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  5. HDU 1102 Constructing Roads, Prim+优先队列

    题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...

  6. HDU 1102(Constructing Roads)(最小生成树之prim算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...

  7. hdu 1102 Constructing Roads (最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  8. hdu 1102 Constructing Roads Kruscal

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...

  9. HDU 1102 Constructing Roads

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. TCP连接的状态与关闭方式及其对Server与Client的影响

    TCP连接的状态与关闭方式及其对Server与Client的影响 1. TCP连接的状态 首先介绍一下TCP连接建立与关闭过程中的状态.TCP连接过程是状态的转换,促使状态发生转换的因素包括用户调用. ...

  2. Evolutionary Computing: 4. Review

    Resource:<Introduction to Evolutionary Computing> 1. What is an evolutionary algorithm? There ...

  3. C#窗体 WinForm 对话框,流

    一.对话框 ColorDialog:颜色选择控件 private void button1_Click(object sender, EventArgs e) { //显示颜色选择器 colorDia ...

  4. input框限制只能输入正整数,逻辑与和或运算

    推荐下自己刚写的项目,请大家指正:童话之翼 有时需要限制文本框输入内容的类型,本节分享下正则表达式限制文本框只能输入数字.小数点.英文字母.汉字等代码. 例如,输入大于0的正整数 代码如下: < ...

  5. macbook上实现MacOS+Windows8+Ubuntu三系统

    至于为什么要装win和linux就不说了,本文只解释安装过程. GPT vs MBR MacOS下装另外两个系统的一个基本问题是:硬盘使用GPT分区,它和常见的MBR分区是两个硬盘分区方式. 它们的主 ...

  6. this kernel requires an x86-64 CPU, but only detected an i686 CPU. unable to boot - please ues a ker

    http://blog.csdn.net/xiao_cs/article/details/7728529 this kernel requires an x86-64 CPU, but only de ...

  7. SVN - 忽略已经提交的文件

    1.在本地删除要忽略的文件 2.与资源库同步,提交删除的文件 3.忽略文件

  8. MSSQL常用函数

    declare 定义变量 set 为变量赋值 SUBSTRING()函数 SUBSTRING ( expression, start, length ) expression 字符串.二进制字符串.文 ...

  9. AdaBoosting 3

    在学习AdaBoosting和online Boosting, 最好有bagging和boosting基础,这样看起来比较会比较顺.有空再补上. AdaBoost 算法的主要思想之一就是在训练集上维护 ...

  10. SAP web 开发 (第二篇 bsp 开发 mvc模式 Part2 )

    单击第一个图标,第一个图标突出显示,单击第二个图标,第一个变灰,第二个突出显示,反之一样.单击history读取历史记录. Controller ZCL_SUS_C_ORDER_CHANGE 1.   ...