题目: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. 10个超棒jQuery表单操作代码片段

    jQuery绝对是一个伟大的开源javascript类库,是帮助我们快速和高效开发前端应用的利器.可能大家在日常的开发过程中常常会处理表单相关的javascript,在今天这篇代码片段分享文章中,这里 ...

  2. .NET 中的 async/await 异步编程

    原文出处: Teroy 的博客 前言 最近在学习Web Api框架的时候接触到了async/await,这个特性是.NET 4.5引入的,由于之前对于异步编程不是很了解,所以花费了一些时间学习一下相关 ...

  3. 基于springboot的多数据源配置

    发布时间:2018-12-11   技术:springboot1.5.1 + maven3.0.1+ mybatis-plus-boot-starter2.3.1 + dynamic-datasour ...

  4. 使用 ODP.NET 访问 Oracle(.net如何访问Oracle)详解

    1,什么是ODF .NE,?就是Oracle 为 .NET (ODP.NET) 专门编写了 Oracle Data Provider,一个用于 Microsoft .NET 环境下的 Oracle 数 ...

  5. CSS特效(弧光效果)

    代码很简单, 根据需要修改标签class和弧光效果的宽高位置就可以了. <!DOCTYPE html> <html lang="en"> <head& ...

  6. linux下如何查看多核负载情况【转】

    最近服务器负载超荷,CPU的单个荷使用率到了百分之八九十,内存占用超大,让给检测性能,不给源码,只给一个+ -g编译生成的执行文件,Fuck!!! 1, 在linux下载了valgrind,使用val ...

  7. inotify-tools命令使用讲解

    inotify-tools 是为linux下inotify文件监控工具提供的一套c的开发接口库函数,同时还提供了一系列的命令行工具,这些工具可以用来监控文件系统的事件. inotify-tools是用 ...

  8. Mac 下查看网络端口占用情况

    1.Mac 下查看网络端口占用情况 有的时候关闭了服务器,但是端口还是占用,解决的方法是 kill 掉占用该端口的进程. # 查看 8009 端口的占用情况 $ lsof -i:8009 可以看到,该 ...

  9. Win 7 IE11不能下载文件,右键另存为也不行

    在IE11中不能下载文件,右键另存为也无效. 发现 在IE11中点击“INTERNET选项”后,IE临时文件夹的地址没有显示,大小为0,修改只能让设置在8-8MB,注销再登录后,一切设置无效. 问题就 ...

  10. 构建基于阿里云OSS文件上传服务

    转载请注明来源:http://blog.csdn.net/loongshawn/article/details/50710132 <构建基于阿里云OSS文件上传服务> <构建基于OS ...