【HDU1301】Jungle Roads(MST基础题)
爽爆。史上个人最快MST的记录7分40s。。一次A。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <numeric> #define typec int
using namespace std; const typec inf = 0xffff;
const int V = ;
int vis[V]; typec lowc[V];
int Map[V][V]; typec prim (typec cost[][V], int n) {
int i, j, p;
typec minc, res = ;
memset(vis, , sizeof (vis));
vis[] = ;
for (i = ; i < n; ++ i) lowc[i] = cost[][i];
for (i = ; i < n; ++ i) {
minc = inf; p = -;
for (j = ; j < n; ++ j) {
if ( == vis[j] && minc > lowc[j]) {
minc = lowc[j]; p = j;
}
}
if (inf == minc) return -;
res += minc; vis[p] = ;
for (j = ; j < n; ++ j) {
if ( == vis[j] && lowc[j] > cost[p][j]) {
lowc[j] = cost[p][j];
}
}
}
return res;
} int main () {
int n;
while (~scanf("%d", &n)) {
if (n == ) break;
for (int i = ; i < n; ++ i) {
for (int j = ;j < n; ++ j) {
if (i == j) Map[i][j] = ;
else Map[i][j] = inf;
}
} for (int i = ; i < n - ; ++ i) {
char s_c; int e_n;
cin >> s_c >> e_n;
for (int j = ; j < e_n; ++ j) {
char e_c; int w;
cin >> e_c >> w;
Map[s_c - 'A'][e_c - 'A'] = Map[e_c - 'A'][s_c - 'A'] = min(Map[s_c - 'A'][e_c - 'A'], w);
}
} cout << prim(Map, n) << endl;
}
return ;
}
【HDU1301】Jungle Roads(MST基础题)的更多相关文章
- HDU1301 Jungle Roads
Jungle Roads The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ai ...
- HDU-1301 Jungle Roads(最小生成树[Prim])
Jungle Roads Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- 【HDU1102】Constructing Roads(MST基础题)
最小生成树水题.prim一次AC #include <iostream> #include <cstring> #include <cstdlib> #includ ...
- hdu1301 Jungle Roads 基础最小生成树
#include<iostream> #include<algorithm> using namespace std; ; int n, m; ]; struct node { ...
- hdu1301 Jungle Roads 最小生成树
The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was s ...
- 【HDU1162】Eddy's picture(MST基础题)
很基础的点坐标MST,一不留神就AC了, - - !! #include <iostream> #include <cstring> #include <cstdlib& ...
- HDU1301 Jungle Roads(Kruskal)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- 【HDU1879】继续畅通工程(MST基础题)
真心大水题...不多说. #include <iostream> #include <cstring> #include <cstdlib> #include &l ...
- 【HDU1233】还是畅通工程(MST基础题)
无坑,裸题.直接敲就恩那个AC. #include <iostream> #include <cstring> #include <cstdio> #include ...
随机推荐
- Java之Map
Map 是一种把键对象和值对象映射的集合,它的每一个元素都包含一对键对象和值对象. Map没有继承于Collection接口 从Map集合中检索元素时,只要给出键对象,就会返回对应的值对象. code ...
- Css中的变形及过渡动画
在css3的标准中新增加了变形样式,这些样式使得网页中各元素的位置形状的变换变得更加容易.其语法如下: transform:none | <transform-function>+ 其中对 ...
- 企业生产环境中linux系统分区的几种方案
方案1:针对网站集群架构中的某个节点服务器分区 该服务器上的数据有多份(其他节点也有)且数据不太重要,建议分区方案如下: /boot: 200MB swap: 物理内存的1.5倍,当内存大于或等于8G ...
- web前端之 DOM
文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是,DOM把 ...
- Android静态变量使用陷阱
静态变量大家再熟悉不过了,本来没什么好重复的.事情起因是这样的,最近测试那边反应正在做的一个产品总是莫名其妙的显示不出某些数据,甚至闪退崩溃,仔细查了几遍发现没什么问题,最后百般周折发现在那部测试机上 ...
- CFGYM 2013-2014 CT S01E03 D题 费用流模版题
题意: n行, a房间的气球,b房间的气球 i行需要的气球,与a房的距离,b房的距离 求最小距离 #include <stdio.h> #include <string.h> ...
- Gson源码分析之Json结构抽象和注解使用
github上的博客地址: http://chuyun923.github.io/blog/2015/01/06/gsonyuan-ma-fen-xi/ XML和Json作为最常用的两种网络传输格式而 ...
- Oracle SQL ANY和ALL语句
Oracle的嵌套子查询可以使用Some,Any和All对子查询中返回的多行结果进行处理. Some表示满足其中一个的含义,是用or串起来的比较从句. 例如:SELECT * FROM emp WHE ...
- 《第一行代码》学习笔记13-UI(2)
1.EditText:程序和用户进行交互的重要控件,允许用户在控件里输入和编辑内容,并可以在程序中对这些内容进行处理. 2.Android控件使用的一般规律:给控件定义一个id->指定下控件的宽 ...
- Oracle复杂查询
1:列出所有员工的姓名,部门名称,和工资 select a1.ename,a1.sal,a2.dname from emp a1,dept a2 where a1.deptno = a2.deptno ...