题目链接

 /*
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. django 操作前端数据

    django 利用json处理前端页面数据,FLASK当中也同样   def create_company(request):if request.user.is_superuser:custom_l ...

  2. STL之map、set灵活使用

    1.LA 5908/UVA1517 Tracking RFIDs 题意:给出s个传感器的位置,以及其感应范围.如果某个方向上有墙,则该方向上感应距离减1.现在有w个墙,给出p个物品的位置,问其能被几个 ...

  3. 40个你可能不知道的Python的特点和技巧

    1.拆箱 >>> a, b, c = 1, 2, 3 >>> a, b, c (1, 2, 3) >>> a, b, c = [1, 2, 3] ...

  4. java内置注解、元注解和自定义注解

    注解的作用: 1.生成文档 2.跟踪代码依赖性 3.编译时进行格式检查 ---------------------------------------------------------------- ...

  5. python异步库

    https://github.com/aio-libs  异步库

  6. INSPIRED启示录 读书笔记 - 第37章 大众网络服务产品

    十大要点 1.可用性:大众网络服务产品必须具备良好的用户体验 2.人物角色:按典型特征将用户分类,抽象出有代表性的用户类型(人物角色) 3.扩展性:应该不间断地考虑扩展性问题,永远留有余地,不到万不得 ...

  7. 什么是make config,make menuconfig,make oldconfig,make xconfig,make defconfig,make gconfig?【转】

    本文转载自;https://blog.csdn.net/baweiyaoji/article/details/52876701 在进行内核配置,或者是对一些软件的配置和编译中,常常会遇到: make ...

  8. C++字符串操作库函数

    #include <bits/stdc++.h> using namespace std; int main() { ]="; ]="abcdefg"; ]= ...

  9. json数据与Gson工具类的使用

    JS中使用JSON JSON对象 --> JSON字符串:JSON.stringify(对象) JSON字符串 --> JSON对象:JSON.parse(JSON字符串) <scr ...

  10. 格点多边形面积公式(Pick定理)的一个形象解释(转)

    Pick定理:如果一个简单多边形(以下称为“多边形”)的每个顶点都是直角坐标平面上的格点,则称该多边形为格点多边形.若一个面积为S的格点多边形,其边界上有a个格点,内部有b个格点,则S=a/2+b-1 ...