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开发一个app(2,main.js)

    昨天跟着vue的官网搭建了vue的一个脚手架,我也是第一次用VUE一切都在摸索阶段. 今天试着看下里面脚手架里面有点什么东西 先看看main.js 导入了3个模块 一个vue,一个app,还有rout ...

  2. popOver 弹出框简单使用

    1.仿QQ弹出框 1.1用到的知识点 1.1.1如何调整弹出框的大小(这里弹出的也是控制器) 这里已经有讲解过http://blog.csdn.net/iostiannan/article/detai ...

  3. Java并发之线程中断

    前面的几篇文章主要介绍了线程的一些最基本的概念,包括线程的间的冲突及其解决办法,以及线程间的协作机制.本篇主要来学习下Java中对线程中断机制的实现.在我们的程序中经常会有一些不达到目的不会退出的线程 ...

  4. FastDFS的安装步骤

    1.安装相关环境 yum install -y gcc-c++ yum -y install libevent yum install -y pcre pcre-devel yum install - ...

  5. 【机器学习实战】第6章 支持向量机(Support Vector Machine / SVM)

    第6章 支持向量机 <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/lates ...

  6. C#之基础

    引子:C#是.NET平台所支持的多种语言中的一门编程语言,它是一门面向对象编程语言.面向对象语言的三大基本特性是:封装.继承.多态.学过C#的人肯定都知道,C#和Java极其相似.我已经学过C语言,现 ...

  7. 【转】python os.popen 超时问题

    python 版本 2.5.4 (在高版本python中提倡使用 subprocess.Popen 取代 os.popen) os.popen 会出现过长时间等待导致阻塞问题, 解决方法如下: [py ...

  8. 执行manage.py syncdb提示Unknown command: 'syncdb'

    1. 实验环境 ubuntu14.4 + django1.9.7 2. 问题描述 在配置完数据库mysite/settings.py后,通常需要运行 python manage.py syncdb 为 ...

  9. ZOJ1171

    错误代码先放这 #include<cstdio> #include<cstdlib> #include<iostream> #include<cstring& ...

  10. 浅谈script标签中的async和defer

    script标签用于加载脚本与执行脚本,在前端开发中可以说是非常重要的标签了.直接使用script脚本的话,html会按照顺序来加载并执行脚本,在脚本加载&执行的过程中,会阻塞后续的DOM渲染 ...