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. PHP流式上传和表单上传(美图秀秀)

    最近需要开发一个头像上传的功能,找了很多都需要授权的,后来找到了美图秀秀,功能非常好用. <?php /** * Note:for octet-stream upload * 这个是流式上传PH ...

  2. 为C#自定义控件添加自定义事件

    这里的自定义控件是由普通控件组合而成的. 希望事件响应代码推迟到使用自定义控件的窗体里写. 步骤一:新建一个用户控件,放两个按钮,Tag分别是btn1,btn2. 这两个按钮的共用单击事件处理代码如下 ...

  3. webview滑动事件 与内部html左右滑动事件冲突问题的解决办法

    最近在做个混合app , 用html做页面,然后通过webview嵌套在activity中,效果是这样: 开始还是比较顺利,增加了菜单退出按钮,返回键页面回退功能,页面加载显示加载图标(在app端实现 ...

  4. 网址前面的icon

    shortcut icon和icon代码之间究竟有何区别呢.下面介绍一下   语句一:<link rel="shortcut icon" href="favicon ...

  5. linux下nat配置

    iptables要启用nat表,必须启动nat表的支持.默认情况下,linux下是没有开启nat表的支持的. #启动内核的路由功能 echo > /proc/sys/net/ipv4/ip_fo ...

  6. C#通过事件跨类调用WPF主窗口中的控件

    xaml.cs文件: using System; using System.Timers; using System.Windows; using System.Windows.Forms; name ...

  7. myeclipse如何设置字体?

    1.首先,将你的Myeclipse打开. 找到上面的菜单“windows”打开“Preferences” 2.然后,在弹出的设置窗口中找到“colors and fonts” 3.将右边的basic打 ...

  8. char 型二维数组

    char FutureFunc[][16] = {"XMA","ZIG","PEAK","PEAKBARS"," ...

  9. 【转】详解Java正则表达式语法

    (转自: http://www.jb51.net/article/76354.htm) 这篇文章主要介绍了Java正则表达式语法,包括常用正则表达式.匹配验证-验证Email是否正确以及字符串中查询字 ...

  10. C++ 基础知识复习(三)

    43. 继承的几种方式: 答:共有继承public,保护继承protected,私有继承private.其中后两种继承会改变原有的访问级别. 44. 深复制与浅复制: 答:简单理解,深复制自己申请了内 ...