hdu-1102-Constructing Roads(Prim算法模板)
/*
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算法模板)的更多相关文章
- 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 (Jav ...
- 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 (最小生成树)
最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...
- 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(kruskal || prim)
求最小生成树.有一点点的变化,就是有的边已经给出来了.所以,最小生成树里面必须有这些边,kruskal和prim算法都能够,prim更简单一些.有一点须要注意,用克鲁斯卡尔算法的时候须要将已经存在的边 ...
- 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 ...
- HDU 1102 Constructing Roads(kruskal)
Constructing Roads There are N villages, which are numbered from 1 to N, and you should build some r ...
随机推荐
- 关于 Content-Type:application/x-www-form-urlencoded 和 Content-Type:multipart/related(转)
转至:http://www.cnblogs.com/taoys/archive/2010/12/30/1922186.html application/x-www-form-urlencoded: 窗 ...
- RHEVM 相关介绍
基础概念: RHEV-H RHEVH(Redhat Enterprise Virtuallization Hypervisor),它是运行虚拟机所需的最低操作系统.RHEVH由作为RHEL(Redha ...
- python read文件的r和rb的区别
r,rb,w,wb 那么在读写文件时,有无b标识的的主要区别在哪里呢? 1.文件使用方式标识 'r':默认值,表示从文件读取数据. 'w':表示要向文件写入数据,并截断以前的内容 'a':表示要向文件 ...
- ORA-00001:unique constraint violated 以及 Incorrect result size: expected 1, actual 0
往数据库中插入数据时报错: www.2cto.com ORA-00001: unique constraint (IDX_CARTON_HEADER)violated. 即往CARTON_ ...
- yii2弹出层
bootstrap http://getbootstrap.com/javascript/#modals https://github.com/lichunqiang/yii2-sweet-submi ...
- Service Fusing
服务熔断也称服务隔离,来自于Michael Nygard 的<Release It>中的CircuitBreaker应用模式,Martin Fowler在博文CircuitBreaker中 ...
- win32调试——OutputDebugString
win32下开发console程序可以直接用printf打印到控制台. 开发图形界面程序时,可以调用OutputDebugString将字符串输出到Debug窗口, 注意是要调试运行才能看到Debug ...
- 写python中的装饰器
python中的装饰器主要用于在已有函数实现功能前附加需要输出的信息,下面将用实例展示我如何写装饰器. 首先分别尝试写装饰器装饰一个无参函数和一个有参函数(被装饰函数仅输出,无返回值情况下) def ...
- FAT和EXFAT文件系统
文件系统 文件系统是操作系统用于明确磁盘或分区上的文件的方法和数据结构:即在磁盘上组织文件的方法.在移动存储设备上比较常用的有FAT文件系统和ExFAT文件系统. FAT分区依据FAT表中每个簇链的所 ...
- jQuery计算器插件
在线演示 本地下载