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. python 学习 : 一个简单的秒表

      游戏说明:绿色数字(左边表示成功停止在整秒的次数,右边表示停止的总次数) 点击stop,如果小数点后为0,即你停止的时间是整秒数,右上方斜杠左边数字加一 把代码复制到这个网页code run he ...

  2. tyvj1144 股票

    描述 2130年,股神巴菲特投胎了!他投胎到你身上!你作为股神转世,能力比原股神还要强,你可以预测到今后n天的股价.假设刚开始你的手上有1元钱,你想知道n天后你最多可以赚到多少钱.作为股神转世,你准备 ...

  3. LYDSY热身赛 escape

    Description 给出数字N(1<=N<=10000),X(1<=x<=1000),Y(1<=Y<=1000),代表有N个敌人分布一个X行Y列的矩阵上矩形的行 ...

  4. 记 Mac Pro 系统升级后,编译安装 PHP-5.6.28 / PHP-7.0 报错修复过程

    买 Mac Pro 的时候,系统为 OS X 10.11.5,编译 PHP-5.6.21 的时候,也遇到一些坑,安装过程记录如下: Mac Pro 编译安装 PHP 5.6.21 及 问题汇总 后来, ...

  5. mysqldump

    $ mysqldump -uroot -p ott_cibn_cms2_staging > /opt/app/ruby/ott-cibn-cms-2.0/current/public/db.sq ...

  6. 通过goto语句学习if...else、switch语句并简单优化

    goto语句在C语言中实现的就是无条件跳转,第二章一上来就介绍goto语句就是要通过goto语句来更加清楚直观的了解控制结构. 我理解的goto语句其实跟switch语句有相似之处,都是进行跳转.不同 ...

  7. phpstorm的使用教程

    1.设置行号:file->settings->Editor->Appearance->Show line numbers 2.设置字体和背景 :file->setting ...

  8. jstl 简单实践

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  9. upload&&download

    package am.demo;  import java.io.File;  import java.io.IOException;  import java.util.Iterator;  imp ...

  10. Python之路

    Python学习之路 第一天   Python之路,Day1 - Python基础1介绍.基本语法.流程控制              第一天作业第二天   Python之路,Day2 - Pytho ...