题目链接

 /*
Name:hdu-1102-Constructing Roads
Copyright:
Author:
Date: 2018/4/18 9:35:08
Description:
prime算法模板
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <utility>
#include <vector>
using namespace std;
const int MAXN = , INF = 0x3f3f3f3f;
bool v[MAXN];
int N;
int dis[MAXN];
vector<pair<int ,int>> g[MAXN];
int Prim() {
memset(v , , sizeof(v)) ;
for (int i=; i<=N; i++) dis[i] = INF;
dis[] = ;
int ans = ;
for (int i=; i<N; i++) {
int mark = -;
for (int j=; j<N; j++) {
if (!v[j]) {
if (mark == -) mark = j;
else if (dis[j]<dis[mark]) mark = j;
}
}
if (mark == -) break;
v[mark] = ;
ans += dis[mark] ;
for (int j=; j<g[mark].size(); ++j) {
if (!v[g[mark][j].first]) {
int x =g[mark][j].first;
dis[x] = min(dis[x], g[mark][j].second);
}
}
}
return ans;
} int main()
{
// freopen("in.txt", "r", stdin); while (~scanf("%d", &N)) {
for (int i=; i<; i++) {//clear
while (!g[i].empty()) g[i].pop_back();
}
for (int i=; i<N; i++) {
for (int j=; j<N; j++) {
int tmp;
scanf("%d", &tmp);
g[i].push_back(make_pair(j, tmp));
}
}
int q;
cin>>q;
for (int i=; i<=q; i++) {
int a, b;
scanf("%d %d", &a, &b);
a--,b--;
g[a][b].second = g[b][a].second = ;
}
cout<<Prim()<<endl;
}
return ;
}

hdu-1102-Constructing Roads(Prim算法模板)的更多相关文章

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

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

  2. hdu 1102 Constructing Roads (Prim算法)

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

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

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

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

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

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

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

  6. hdu 1102 Constructing Roads(kruskal || prim)

    求最小生成树.有一点点的变化,就是有的边已经给出来了.所以,最小生成树里面必须有这些边,kruskal和prim算法都能够,prim更简单一些.有一点须要注意,用克鲁斯卡尔算法的时候须要将已经存在的边 ...

  7. hdu 1102 Constructing Roads Kruscal

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

  8. HDU 1102 Constructing Roads

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

  9. HDU 1102 Constructing Roads(kruskal)

    Constructing Roads There are N villages, which are numbered from 1 to N, and you should build some r ...

随机推荐

  1. vloatile总结与synchronized对比

    原文地址:https://www.cnblogs.com/xiaoxian1369/p/5411877.html 1.要使volatile变量提供理想的线程安全,必须同时满足以下两个条件:1).对变量 ...

  2. 【算法题 14 LeetCode 147 链表的插入排序】

    算法题 14 LeetCode 147 链表的插入排序: 解题代码: # Definition for singly-linked list. # class ListNode(object): # ...

  3. Multi-thread & Multi-process

    关于多进程和多线程,教科书上最经典的一句话是“进程是资源分配的最小单位,线程是CPU调度的最小单位”. 对于到底是使用多进程还是多线程, 要根据实际情况来判断,选择更适合的. 具体情况,可以参考下面: ...

  4. QuickSort again

    I wrote a blog about Quick Sort before. Let's talk more about it. If you don't think that the implem ...

  5. android 对话框中的进度条 (ProgressDialog)

    from:http://byandby.iteye.com/blog/817214 显然要定义对话框进度条就要用ProgressDialog,首先我们需要创建ProgressDialog对象,当然这里 ...

  6. Mac 环境 下使用Charles 抓包Http/Https请求

    实现目标 在Mac 上 对 iOS  真机 和 模拟器 进行 Http/Https抓包 使用工具 Mac 上 Charles 4.2  安装 参考链接 1. 和 链接 2. 抓包 http 请求 (1 ...

  7. SpringBoot Redis工具类封装

    1.接口类 package com.sze.redis.util; import java.util.List; import java.util.Set; import java.util.conc ...

  8. jpa,jdbc,hibernate/mybatis,数据库驱动

    JPA是规范,hibernate/mybatis是对规范的实现,hibernate/mybatis是对jdbc的封装,也就是说hibernate/mybatis还是会调用jdbc.    我们平时使用 ...

  9. jQuery滑动杆打分插件

    在线演示 本地下载

  10. latin-1

    Latin1是ISO-8859-1的别名,有些环境下写作Latin-1.ISO-8859-1编码是单字节编码,向下兼容ASCII,其编码范围是0x00-0xFF,0x00-0x7F之间完全和ASCII ...