题目: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(最小生成树)的更多相关文章

  1. hdu 3371 Connect the Cities (最小生成树Prim)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...

  2. HDU 3371 Connect the Cities 最小生成树(和关于sort和qsort的一些小发现)

    解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了. 还是用并查集加克鲁斯卡尔.只是在输入已经连通的集合的时候,通过并查集将 ...

  3. hdu 3371 Connect the Cities

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...

  4. HDU 3371 Connect the Cities(prim算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...

  5. Hdu 3371 Connect the Cities(最小生成树)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...

  6. HDU 3371 Connect the Cities(并查集+Kruskal)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...

  7. POJ:3371 Connect the Cities(最小生成树)

    http://acm.hdu.edu.cn/showproblem.php?pid=3371 AC代码: /** /*@author Victor /* C++ */ #include <bit ...

  8. hdu oj 3371 Connect the Cities (最小生成树)

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  9. HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑

    这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...

随机推荐

  1. PCL中的OpenNI点云获取框架(OpenNI Grabber Framework in PCL)

    从PCL 1.0开始,PCL(三维点云处理库Point Cloud Library)提供了一个通用采集接口,这样可以方便地连接到不同的设备及其驱动.文件格式和其他数据源.PCL集成的第一个数据获取驱动 ...

  2. django之环境变量配置

    1.配置python变量环境,C:\Python27\;C:\Python27\Scripts\ 2.配置django变量环境,C:\Python34\Lib\site-packages\Django ...

  3. CentOS下递归遍历文件夹下所有文件,查找指定字符

    命令如下: find . | xargs grep -ri "XXX"

  4. msiexec command line arguments

    Documented command line arguments Type MSIEXEC /? and you'll get the following on-screen help: Windo ...

  5. Emacs的sr-speedbar中使能Go-mode

    sr-speedbar使用了speedbar的文件检索功能,但是Emacs24自带的speedbar不支持go文件预览,下面是在speedbar中使能go-mode的一种方法: 1,按F10启动菜单栏 ...

  6. Swift打印Debug日志,实现Release下不打印

    OC内,我们往往做log打印时,会考虑一个Debug环境下打印,Release下控制不打印,以节约性能消耗. OC我们可以这样做: 在pch文件内,定义如下: //打印日志 #ifdef DEBUG ...

  7. 使用IntelliJ IDEA 15和Maven创建Java Web项目

    转自:https://blog.csdn.net/myarrow/article/details/50824793博文链接!

  8. <转>赋值表达式解析的流程

    转自:http://www.cnblogs.com/nazhizq/p/6520072.html 上节说到表达式的解析问题,exprstate函数用于解析普通的赋值表达式.lua语言支持多变量赋值.本 ...

  9. [转]VUE优秀UI组件库合集

    原文链接 随着SPA.前后端分离的技术架构在业界越来越流行,前端的业务复杂度也越来越高,导致前端开发者需要管理的内容,承担的职责越来越多,这一切,使得业界对前端开发方案的思考多了很多,以react.v ...

  10. ios实例开发精品文章推荐(8.19)

    1.iOS源码:选择器类--简单的效果.<ignore_js_op> 下载地址:http://www.apkbus.com/android-109320-1-1.html 2.iOS源码: ...