Connect the Cities

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

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
 

 #include<stdio.h>
#include<string.h>
using namespace std;
#define N 505
#define INF 0x7fffffff
int n , m , k ;
int g[N][N] , vis[N] , low[N] ; void prim ()
{
int pos = , res = ;
memset (vis , , sizeof(vis)) ;
vis[pos] = ;
for (int i = ; i <= n ; i++)
if ( !vis[i] )
low[i] = g[pos][i] ;
for (int i = ; i < n ; i++) {
int minn = INF ;
pos = - ;
for (int j = ; j <= n ; j++)
if ( !vis[j] && low[j] < minn) {
minn = low[j] ;
pos = j ;
}
if (pos == -) {
puts ("-1") ;
return ;
}
res += minn ;
vis[pos] = ;
for (int j = ; j <= n ; j++)
if (!vis[j] && low[j] > g[pos][j])
low[j] = g[pos][j] ;
}
printf ("%d\n" , res) ;
}
int main()
{
// freopen ("a.txt" , "r" , stdin);
int u , v , w ;
int T , 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++)
g[i][j] = INF ;
for (int i = ; i < m ; i++) {
scanf ("%d%d%d" , &u , &v , &w) ;
if (g[u][v] > w)//notice
g[u][v] = g[v][u] = w ;
}
for (int i = ; i < k ; i++) {
scanf ("%d%d" , &t , &u) ;
for (int j = ; j <= t - ; j++) {
scanf ("%d" , &v) ;
g[u][v] = g[v][u] = ;
}
}
prim () ;
}
}

给偶上了一课,本以为用kruskal就能一条路走到黑:

但遗憾的TLE了,

分析时间复杂度:
   kruskal算法为O(e*lge),prim算法为O(v*v)(貌似可用优先队列缩减),此题数据为v=500,e=25000,用prim较优
转载:http://www.cnblogs.com/GO-NO-1/p/3710816.html
 

Connect the Cities(MST prim)的更多相关文章

  1. Connect the Cities(prim)用prim都可能超时,交了20几发卡时过的

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

  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. hdu3371 Connect the Cities (MST)

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

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

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...

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

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

  6. Connect the Cities[HDU3371]

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

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

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

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

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

  9. hdu 3371 Connect the Cities

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

随机推荐

  1. MVC5 + EF6 + Bootstrap3 (7) Bootstrap的栅格系统

    文章来源: Slark.NET-博客园http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-grid.html 上一节:ASP.NET MVC ...

  2. HoloLens开发手记 - Unity之摄像头篇

    当你穿戴好HoloLens后,你就会处在全息应用世界的中心.当你的项目开启了"Virtual Reality Support"选项并选中了"Windows Hologra ...

  3. 《TCP/IP详解卷1:协议》第11章 UDP:用户数据报协议-读书笔记

    章节回顾: <TCP/IP详解卷1:协议>第1章 概述-读书笔记 <TCP/IP详解卷1:协议>第2章 链路层-读书笔记 <TCP/IP详解卷1:协议>第3章 IP ...

  4. 【Moqui框架】Moqui连接各种类型的数据库

    Moqui连接mysql数据库 各种数据库的连接文本: -- Derby<datasource group-name="transactional" database-con ...

  5. 调研Android Studio开发环境的发展演变(附安装教程,多图)

    Android Studio(以下简称AS)第一次公开亮相是在2013年的谷歌I/O大会上,14年的大会上谷歌发布其试用测试版,如今AS已经历数次版本更新,功能十分强大.如(摘自百度百科Android ...

  6. 使用Web Deploy进行远程部署

    Web Deploy支持直接从本地Visual Studio的工程文件部署网站到远程服务器,部署的过程中可以对比哪些文件变化了需要拷贝,而不是一股脑的全部拷贝,效率和准确性会更好. 部署的过程主要要注 ...

  7. iOS开发小技巧--UIScrollView内部子控件添加约束的注意点

    注意:用UIScrollView时布局子控件的时候,不要相对于UIScrollView来添加约束,这样做不是设置子控件的位置,反而是设置了UIScrollView的contentSize 子控件的尺寸 ...

  8. poj3692 最大点权独立集/最大独立集

    题意:有男孩和女孩,男孩之间全部认识,女孩之间全部认识,一部分男孩和女孩认识,现在希望选出一些孩子,这些孩子都相互认识. 方法:正的做不好做,观察他的补图,补图之间无关系的边就是原图有关系的.补图中的 ...

  9. hdu2923 最短路floyd

    建图还是有点烦人的. #include<map> #include<string> #include<stdio.h> #include<iostream&g ...

  10. 【BZOJ-1406】密码箱 约数 + 乱搞 + set?

    1406: [AHOI2007]密码箱 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1143  Solved: 677[Submit][Status][ ...