hdu 3371 Connect the Cities(最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371

984ms风险飘过~~~
/************************************************************************/
/*
hdu Connect the Cities
最小生成树
题目大意:最小生成树,题目很长,题意很简单就是最小生成树。关键是构建图
*/
/************************************************************************/ #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h> #define MAX 0xfffffff const int N = ;
int map[N][N];
int vis[N];
int mark[N];
int num,n; void build_map()
{
int m,k;
int q,p,c;
int t; scanf("%d%d%d",&n,&m,&k);
for (int i = ; i <= n; i++)
for (int j = i; j <= n; j++)
map[i][j] = map[j][i] = ((i==j)?:MAX); for (int i = ; i < m; i++)
{
scanf("%d%d%d",&p,&q,&c);
if (map[p][q] > c)
{
map[p][q] = map[q][p] = c;
} }
while(k--)
{
scanf("%d",&t);
for (int i = ; i < t; i++)
{
scanf("%d",&mark[i]);
for (int j = ; j < i; j++)
{
map[mark[j]][mark[i]] = map[mark[i]][mark[j]] = ;
}
}
}
} int prim()
{
int t = n;
int sum = ;
int min,k;
vis[] = ;
while(--t)
{
min = MAX;
for(int i = ; i <= n; i++)
{
if (vis[i] != && map[][i] < min)
{
min = map[][i];
k = i;
}
}
if(min == MAX)break;
vis[k] = ;
sum += min;
for (int i = ; i <= n; i++)
{
if (vis[i] != && map[k][i] < map[][i])
map[][i] = map[k][i];
}
}
return t==?sum:-;
} int main()
{
scanf("%d",&num);
while(num--)
{
build_map();
memset(vis,,sizeof(vis));
printf("%d\n",prim());
}
return ;
}
hdu 3371 Connect the Cities(最小生成树)的更多相关文章
- hdu 3371 Connect the Cities (最小生成树Prim)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...
- HDU 3371 Connect the Cities 最小生成树(和关于sort和qsort的一些小发现)
解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了. 还是用并查集加克鲁斯卡尔.只是在输入已经连通的集合的时候,通过并查集将 ...
- hdu 3371 Connect the Cities
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...
- HDU 3371 Connect the Cities(prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...
- Hdu 3371 Connect the Cities(最小生成树)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...
- HDU 3371 Connect the Cities(并查集+Kruskal)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...
- POJ:3371 Connect the Cities(最小生成树)
http://acm.hdu.edu.cn/showproblem.php?pid=3371 AC代码: /** /*@author Victor /* C++ */ #include <bit ...
- hdu oj 3371 Connect the Cities (最小生成树)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑
这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...
随机推荐
- 编程填空:第i位取反
总时间限制: 1000ms 内存限制: 1024kB描述写出函数中缺失的部分,使得函数返回值为一个整数,该整数的第i位是n的第i位取反,其余位和n相同 请使用[一行代码]补全bitManipulati ...
- [解决思路]ORA-01078: failure in processing system parameters LRM-00109: could not open parameter file
oracle数据库,服务器异常断电,导致数据库不能启动.... 错误提示: SQL> startup ORA-01078: failure in processing system parame ...
- 文本diff算法Patience Diff
一般在使用 Myers diff算法及其变体时, 对于下面这种例子工作不是很好, 让变化不易阅读, 并且容易导致合并冲突 void Chunk_copy(Chunk *src, size_t src_ ...
- SpannableStringUtil实现丰富文字效果
代码地址如下:http://www.demodashi.com/demo/15007.html 前言 在android开发中,我们不免会用到 TextView 的各种显示效果,如字体样式,颜色,大小, ...
- 俄罗斯方块-C语言-详注版
代码地址如下:http://www.demodashi.com/demo/14818.html 俄罗斯方块-C语言-详注版 概述 本文详述了C语言版俄罗斯方块游戏的原理以及实现方法,对游戏代码进行了详 ...
- XML制作RSS源
什么是RSS源?看到这片文章的人相信都知道.自己博客首页不就是一个吗? 好吧,先来一个简单点的.直接就是死代码:详细如何使用就看RSS使用标准吧! <?xml version = "1 ...
- 没有找到AdbWinApi.dll
今天调试adb命令时遇到一些问题 1. 没有找到AdbWinApi.dll 2. adb server is out of date. killing... ADB server didn't AC ...
- Android开发环境——模拟器AVD相关内容汇总
Android开发环境将分为SDK相关内容.Eclipse ADT相关内容.模拟器AVD相关内容.调试器DDMS相关内容.日志LogCat相关内容.连接驱动ADB相关内容.内存泄露检测工具MAT相关 ...
- maven常见问题问答(转)
转自:http://www.oschina.net/question/158170_29368 1.前言 Maven,发音是[`meivin],"专家"的 意思.它是一个很好的项目 ...
- JavaScript Math Object 数字
JavaScript Math Object Math Object The Math object allows you to perform mathematical tasks. Math is ...