Connect the Cities

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 701 Accepted Submission(s): 212
 
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
HDOJ Monthly Contest – 2010.04.04
 
Recommend
lcy
/*
点多变少的时候用克利斯卡尔算法,点少边的时候用prim算法 真心的心累,交了20几发卡时过的
*/
#include<bits/stdc++.h>
#define N 550
#define INF 0x3f3f3f3f
using namespace std;
/*********输入输出外挂************/
template <class T>
inline bool scan_d(T &ret)
{
char c;
int sgn;
if(c=getchar(),c==EOF)
return ; //EOF
while(c!='-'&&(c<''||c>''))
c=getchar();
sgn=(c=='-')?-:;
ret=(c=='-')?:(c-'');
while(c=getchar(),c>=''&&c<='')
ret=ret*+(c-'');
ret*=sgn;
return ;
}
inline void out(int x)
{
if(x>)
out(x/);
putchar(x%+'');
}
/*********输入输出外挂************/
int t;
int n,m,k;
int mapn[][];//用来构建图
bool vis[];//用来记录那个点访问过也就是判环用的
int d[];//表示选中的结点到当前结点的最小距离
int x,y,val,q,rt[];
void init()
{
memset(mapn,INF,sizeof mapn);
memset(vis,false,sizeof vis);
memset(d,INF,sizeof d);
}
int prim(int m)
{
int root;//表示当前选中的结点
int minn=INF;
root=m;
vis[m]=true;
long long cur=;
int len=;
for(int i=;i<=n;i++)//优化的prim算法,总共要找n个点嘛,先找了m了,然后再找n-1个点就够了
{
minn=INF;
for(int j=;j<=n;j++)//然后开始找点
if(!vis[j])//这个点没有遍历过
{
if(d[j]>mapn[root][j])
d[j]=mapn[root][j];
if(minn>d[j])
{
minn=d[j]; m=j;
}
}
if(!vis[m])//这个点没有遍历过
{
cur+=minn;
vis[m]=true;
root=m;
len++; }
}
if(len==n-)
return cur;
return -;
}
int main()
{
//freopen("C:\\Users\\acer\\Desktop\\in.txt","r",stdin);
scan_d(t);
while(t--)
{
scan_d(n);scan_d(m);scan_d(k);
init();//初始化
for(int i=;i<m;i++)
{
scan_d(x);scan_d(y);scan_d(val);
if(mapn[x][y]>val)//判重边
{
mapn[x][y]=mapn[y][x]=val;
}
}
for(int i=;i<k;i++)
{
scan_d(q);
for(int j=;j<q;j++)
scan_d(rt[j]);
/*既然已经联通了那么相互之间就不需要再花费钱去修路了*/
for(int j=;j+<q;j++)
mapn[rt[j]][rt[j+]]=mapn[rt[j+]][rt[j]]=;
}
printf("%d\n",prim());
}
return ;
}

Connect the Cities(prim)用prim都可能超时,交了20几发卡时过的的更多相关文章

  1. Connect the Cities(MST prim)

    Connect the Cities Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  2. HDU 3371 Connect the Cities(prim算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...

  3. HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑

    这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...

  4. Connect the Cities[HDU3371]

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...

  5. hdu 3371 Connect the Cities(最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...

  6. hdu oj 3371 Connect the Cities (最小生成树)

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  7. 求最小生成树(暴力法,prim,prim的堆优化,kruskal)

    求最小生成树(暴力法,prim,prim的堆优化,kruskal) 5 71 2 22 5 21 3 41 4 73 4 12 3 13 5 6 我们采用的是dfs的回溯暴力,所以对于如下图,只能搜索 ...

  8. hdu 3371 Connect the Cities

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...

  9. hdoj 3371 Connect the Cities

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

随机推荐

  1. vue 2 使用Bus.js进行兄弟(非父子)组件通信 简单案例

    vue2中废弃了$dispatch和$broadcast广播和分发事件的方法.父子组件中可以用props和$emit().如何实现非父子组件间的通信,可以通过实例一个vue实例Bus作为媒介,要相互通 ...

  2. 线段树初步__ZERO__.

    线段树,顾名思义,就是指一个个线段组成的树. 线段树的定义就是: 线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间对应线段树中的一个叶结点.使用线段树可以快速的查找某 ...

  3. XCode消除警告、错误

    1.集成支付宝SDK后,报一堆warning: (arm64) /Users/scmbuild/workspace/standard-pay/.....警告 解决方法: 1)  Go to Build ...

  4. 《MATLAB从入门到放弃》二维曲线和图形绘制基础(二):使用Help文档学习line、plot、plotyy、subplot、hold绘图函数

    目录: »  plot 最常用的二维曲线绘图函数 >  帮助文档 >  基本使用语法 >  线条的样式.符号和颜色调整 >  图形属性调整 >  使用图形句柄进行设置 » ...

  5. JS设计模式(二) 惰性模式

    惰性模式:减少代码每次执行时的重复性判断,通过重新定义对象来避免原对象中的分支判断,提高网站性能. 例如针对不同浏览器的事件注册方法: var AddEvent = function(dom, typ ...

  6. 【转】Mapreduce部署与第三方依赖包管理

    Mapreduce部署是总会涉及到第三方包依赖问题,这些第三方包配置的方式不同,会对mapreduce的部署便捷性有一些影响,有时候还会导致脚本出错.本文介绍几种常用的配置方式: 1. HADOOP_ ...

  7. .Neter玩转Linux系列之二:Linux下的文件目录及文件目录的权限

    一.Linux下的文件目录 简介:linux的文件系统是采用级层式的树状目录结构,在此 结构中的最上层是根目录“/”,然后在此目录下再创建 其他的目录.深刻理解linux文件目录是非常重要的,如下图所 ...

  8. Thirft框架快速入门

    Thrift介绍1.什么是thrift?thrift早期由facebook内部团队开发,主要用于实现跨语言间的方法调用,属于远程方法调用的一种,后开源纳入apache中,成为了apache thrif ...

  9. Oracle 定时查询数据插入新表中(job+存储过程)

    create table EGMAS_COUNT_DATA(TIMES       date not null, COUNT NUMBER(30) not null, SYSTEM_NAME VARC ...

  10. MySQL的备份与还原以及常用数据库查看命令

    MySQL命令行导出数据库: 1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录 如我输入的命令行:cd C:\Program Files\MySQL\MySQL Serv ...