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
 

  1. #include<stdio.h>
  2. #include<string.h>
  3. using namespace std;
  4. #define N 505
  5. #define INF 0x7fffffff
  6. int n , m , k ;
  7. int g[N][N] , vis[N] , low[N] ;
  8.  
  9. void prim ()
  10. {
  11. int pos = , res = ;
  12. memset (vis , , sizeof(vis)) ;
  13. vis[pos] = ;
  14. for (int i = ; i <= n ; i++)
  15. if ( !vis[i] )
  16. low[i] = g[pos][i] ;
  17. for (int i = ; i < n ; i++) {
  18. int minn = INF ;
  19. pos = - ;
  20. for (int j = ; j <= n ; j++)
  21. if ( !vis[j] && low[j] < minn) {
  22. minn = low[j] ;
  23. pos = j ;
  24. }
  25. if (pos == -) {
  26. puts ("-1") ;
  27. return ;
  28. }
  29. res += minn ;
  30. vis[pos] = ;
  31. for (int j = ; j <= n ; j++)
  32. if (!vis[j] && low[j] > g[pos][j])
  33. low[j] = g[pos][j] ;
  34. }
  35. printf ("%d\n" , res) ;
  36. }
  37. int main()
  38. {
  39. // freopen ("a.txt" , "r" , stdin);
  40. int u , v , w ;
  41. int T , t;
  42. scanf ("%d" , &T) ;
  43. while (T--) {
  44. scanf ("%d%d%d" , &n , &m , &k) ;
  45. for (int i = ; i <= n ; i++)
  46. for (int j = ; j <= n ; j++)
  47. g[i][j] = INF ;
  48. for (int i = ; i < m ; i++) {
  49. scanf ("%d%d%d" , &u , &v , &w) ;
  50. if (g[u][v] > w)//notice
  51. g[u][v] = g[v][u] = w ;
  52. }
  53. for (int i = ; i < k ; i++) {
  54. scanf ("%d%d" , &t , &u) ;
  55. for (int j = ; j <= t - ; j++) {
  56. scanf ("%d" , &v) ;
  57. g[u][v] = g[v][u] = ;
  58. }
  59. }
  60. prim () ;
  61. }
  62. }

给偶上了一课,本以为用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. GDB调试汇编栈堆过程的学习

    前期调试 我的程序代码是: 首先,用gcc g gdb.c -o gdb -m32产生32位汇编. 输入gdb gdb进入gdb调试器 在main函数处设置一个断点:b main 用disassemb ...

  2. Jenkins进阶系列之——02email-ext邮件通知模板

    发现一个很好的邮件通知模板,根据我的需求定制了一些.分享一下. Default Subject: 构建通知:${BUILD_STATUS} - ${PROJECT_NAME} - Build # ${ ...

  3. 回顾一年的IT学习历程与大学生活

    今天是2015年8月27日,距离成为大三狗还有一个多星期,在这个不算繁忙的暑假的下午来总结一下这一年来,在IT方面的学习. 一.入门(2014.3) 我大一的专业是信息工程,信息工程听上去就是信息(I ...

  4. Bootstrap系列 -- 28. 下拉菜单状态

    下拉菜单项的默认的状态(不用设置)有悬浮状态(:hover)和焦点状态(:focus). 下拉菜单项除了上面两种状态,还有当前状态(.active)和禁用状态(.disabled).这两种状态使用方法 ...

  5. Nodejs学习笔记(七)--- Node.js + Express 构建网站简单示例

    目录 前言 新建项目.建立数据库以及其它准备工作 新建express + ejs 项目:sampleEjs 创建数据库 修改package.json文件,安装session和mysql模块 样式和JQ ...

  6. 八幅漫画理解使用JSON Web Token设计单点登录系统

    用jwt这种token的验证方式,是不是必须用https协议保证token不被其他人拦截? 是的.因为其实只是Base64编码而已,所以很容易就被解码了.如果你的JWT被嗅探到,那么别人就可以相应地解 ...

  7. ajax使用post提交中文

    Ajax使用POST提交中文乱码问题 前段时间写JSP,使用AJAX以POST方式提交数据,如果是中文字符提交就会乱码,后来写ASP时用到AJAX以POST方式提交数据,中文一样是乱码.搜索一下相关资 ...

  8. 【codevs 1296】营业额统计 水~~

    今天下午先写一个Splay水题来复习一下Splay模板.是不是有点太水了做这种水题我有点良心不安. 可笑的是一开始我竟然WA了一组,看来是我低估水题的数据范围了,我是空节点直接返回inf或-inf,明 ...

  9. druid(德鲁伊)数据源的使用和配置 阿里出品

    pom.xml <dependency>     <groupId>com.alibaba</groupId>     <artifactId>drui ...

  10. Android中实现自定义的拍照应用

    可以参考:http://www.android-doc.com/guide/topics/media/camera.html 一.添加相应的权限 <uses-permission android ...