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. hashicorp/consul

    https://github.com/hashicorp/consul/tree/master/vendor/github.com/boltdb/bolt

  2. DTD总结

    DTD 可以检测 XNM 文档的结构是否正确,就好像文章中用来保证结构正确的语法规则一样. 引入 DTD 1.引入私有的 DTD 文件,URI 可以使相对地址或绝对地址 <!DOCTYPE 根元 ...

  3. jquery.print.js 打印插件

    <script language="javascript" src="jquery-1.4.4.min.js"></script> &l ...

  4. 启动apache和tomcat端口被占用解决办法

    1,打开控制台,使用命令 netstat -aon|findstr 8090  找出端口被占用的进程, 2,使用 taskkill -f -pid 4116(进程id)杀掉当前占用端口的进程

  5. FileZillaFTP使用教程

    FileZillaServer.exe服务启动和关闭程序 FileZilla Server Interface.exe 服务 管理程序 配置ftp服务器的用户名,密码,目录,目录读写权限 启动File ...

  6. SQL实现表名更改,列名更改,约束更改

    修改表名 格式:sp_rename tablename,newtablename sp_rename tablename,newtablename 修改字段名 格式:sp_rename 'tablen ...

  7. Gym - 100917H

    题目链接 #include<cstdio> #include<cstring> #include<algorithm> #include<vector> ...

  8. 数据库DBA(3年以内需求)

    1.DBA工作:日常维护,备份及恢复,系统安装,补丁应用,健康检查及优化,故障处理; 2.精通sql语句.视图存储过程.函数的编写.触发器; 3.精通data guard/RAC,能顺利配置和管理da ...

  9. HTML中图像代替提交按钮

    1. 用图像代替提交按钮 当只有一个提交按钮的时候 ,可以简单的实现,不用添加事件函数,代码是: <input type = "image"' name = ".. ...

  10. Centos7 安装配置Mysql

    Centos7默认自带的mariadb数据库,它是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL ...