Connect the Cities

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18067    Accepted Submission(s): 4460

Problem Description
In 2100, since the sea level rise, most of the cities disappear.
Though some survived cities are still connected with others, but most of
them become disconnected. The government wants to build some roads to
connect all of these cities again, but they don’t want to take too much
money.  
 
Input
The first line contains the number of test cases.
Each
test case starts with three integers: n, m and k. n (3 <= n
<=500) stands for the number of survived cities, m (0 <= m <=
25000) stands for the number of roads you can choose to connect the
cities and k (0 <= k <= 100) stands for the number of still
connected cities.
To make it easy, the cities are signed from 1 to n.
Then follow m lines, each contains three integers p, q and c (0 <= c <= 1000), means it takes c to connect p and q.
Then
follow k lines, each line starts with an integer t (2 <= t <= n)
stands for the number of this connected cities. Then t integers follow
stands for the id of these cities.
 
Output
For each case, output the least money you need to take, if it’s impossible, just output -1.
 
Sample Input
1
6 4 3
1 4 2
2 6 1
2 3 5
3 4 33
2 1 2
2 1 3
3 4 5 6
 
Sample Output
1
 
Author
dandelion
 
Source
 
题意:
n个点,m条已知长度的边,k组已经连接的边,问最小要建多少条边;
代码:
 //prim 模板。这题数据太大用cruscal会超时。
#include<iostream>
#include<cstdio>
#include<cstring>
int dis[],map[][],mark[],ha[];
const int MAX=;
int prim(int n)
{
for(int i=;i<=n;i++) //初始化每个点到生成树中点的距离
{
dis[i]=map[][i];
mark[i]=;
}
mark[]=; //1这个点加入生成树中。
int sum=;
for(int i=;i<n;i++) //枚举n-1条边
{
int sta=-,Min=MAX;
for(int j=;j<=n;j++) //找不在生成树中的点中距离生成树中的点长度最小的
{
if(!mark[j]&&dis[j]<Min)
{
Min=dis[j];
sta=j;
}
}
if(sta==-) return -; //没找到可以可以联通的路
mark[sta]=; //新找到的点加入生成树
sum+=Min;
for(int j=;j<=n;j++) //更新树外的点到树中的点的距离
{
if(!mark[j]&&dis[j]>map[sta][j])
dis[j]=map[sta][j];
}
}
return sum;
}
int main()
{
int n,m,k,p,q,c,h,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
map[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",&h);
for(int i=;i<=h;i++)
{
scanf("%d",&ha[i]);
for(int j=;j<i;j++)
map[ha[i]][ha[j]]=map[ha[j]][ha[i]]=;
}
}
int ans=prim(n);
printf("%d\n",ans);
}
return ;
}

HDU3371 最小生成树的更多相关文章

  1. 最小生成树(Kruskal算法-边集数组)

    以此图为例: package com.datastruct; import java.util.Scanner; public class TestKruskal { private static c ...

  2. 最小生成树计数 bzoj 1016

    最小生成树计数 (1s 128M) award [问题描述] 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一 ...

  3. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  4. 【BZOJ 1016】【JSOI 2008】最小生成树计数

    http://www.lydsy.com/JudgeOnline/problem.php?id=1016 统计每一个边权在最小生成树中使用的次数,这个次数在任何一个最小生成树中都是固定的(归纳证明). ...

  5. 最小生成树---Prim算法和Kruskal算法

    Prim算法 1.概览 普里姆算法(Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点(英语:Vertex (gra ...

  6. Delaunay剖分与平面欧几里得距离最小生成树

    这个东西代码我是对着Trinkle的写的,所以就不放代码了.. Delaunay剖分的定义: 一个三角剖分是Delaunay的当且仅当其中的每个三角形的外接圆内部(不包括边界)都没有点. 它的存在性是 ...

  7. 最小生成树(prim&kruskal)

    最近都是图,为了防止几次记不住,先把自己理解的写下来,有问题继续改.先把算法过程记下来: prime算法:                  原始的加权连通图——————D被选作起点,选与之相连的权值 ...

  8. 最小生成树 prime poj1258

    题意:给你一个矩阵M[i][j]表示i到j的距离 求最小生成树 思路:裸最小生成树 prime就可以了 最小生成树专题 AC代码: #include "iostream" #inc ...

  9. 最小生成树 prime + 队列优化

    存图方式 最小生成树prime+队列优化 优化后时间复杂度是O(m*lgm) m为边数 优化后简直神速,应该说对于绝大多数的题目来说都够用了 具体有多快呢 请参照这篇博客:堆排序 Heapsort / ...

随机推荐

  1. [Scala] 快学Scala A2L2

    集合 13.1 集合的三大类 所有的集合都扩展Iterable特质.集合的三大集合为Seq, Set, Map Seq是一个有先后次序的值的序列,比如数组或列表.IndexSeq允许我们通过整型下表快 ...

  2. spring mvc+mybatis+sql server简单配置

    context.xml配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&qu ...

  3. 学习JVM GarbageCollection

    前言 Java和C++之间显著的一个区别就是对内存的管理.Java和C++把内存管理的权利赋予给开发人员的方式不同,Java拥有一套自动的内存回收系统(Garbage Collection,GC)简称 ...

  4. JQuery Mobile 页面参数传递

    在单页模版中使用基于HTTP的方式通过POST和GET请求传递参数,而在多页模版中不需要与服务器进行通信,通常在多页模版中有以下三种方法来实现页面间的参数传递. 1.GET方式:在前一个页面生成参数并 ...

  5. libeventReferenceManual阅读笔记

    一.01_intro.html Example:A low-level ROT13 server with Libevent 这是一个利用event实现的server实例 Example:A simp ...

  6. 4.EasyUI学习总结(四)——EasyUI组件使用 (通过用户登录来演示dialog、ajax的使用,serialize方法的使用,前后台怎样交互等)

    一.EasyUI组件的简单介绍 详细可看api: http://www.jeasyuicn.com/api/docTtml/index.htm easyUI提供了很多组件让我们使用,如下图所示: 很多 ...

  7. 1.UI初认识

    前节:app是什么? app英文全称:application 应用程序,简称应用.也就是手机应用的简写 出处:http://www.cnblogs.com/mcj-coding/p/5098254.h ...

  8. 八皇后问题_Qt_界面程序实现

    //核心代码如下 //Queen--放置皇后 #include "queue.h" queue::queue() { *; ; this->board = new bool[ ...

  9. 使用tomact监控应用服务器的性能

    第一步:在D:\apache-tomcat-7.0.63\conf 配置目录找到tomcat-users.xml 打开添加用户角色及权限 第二步:重启tomact 第三步:浏览器上打开http://1 ...

  10. 3.使用OGG进程进行初始化数据

    开始初始化数据的时候要满足下面的条件: 1.disable掉目标段表的外键约束 2.disable掉目标端表的触发器 3.删除目标段表的索引,加快初始化速度 4.目标端表结构创建完成 源端配置初始化抽 ...