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. error C2065: “CMainFrame”: 未声明的标识符

    xxxView.cp的开头包含 框架的头文件即可 : #include "MainFrm.h"

  2. RxJava简介

    RxJava简介 本文为前段时间学习RxJava时留下的历史遗留笔记,仅作纪念,科学的大神教学帖子在这里-> 给 Android 开发者的 RxJava 详解 通过链式调用序列实现基于事件流的异 ...

  3. 使用jQuery加载html页面到指定的div

    一.jQuery加载一个html页面到指定的div里 把a.html里面的某一部份的内容加载到b.html的一个div里.比如:加载a.html里面的<div id=“row"> ...

  4. 『随笔』WCF开发那些需要注意的坑

    执行如下 批处理:"C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\svcutil.exe" http://127.0.0.1: ...

  5. Linq 中查询一个表中指定的字段

    //Linq中查询一个表中指定的几个字段: ); // FindAllItems()为查询对应表的所有数据的方法: // Where 里面为查询条件 // Select 为查询的筛选条件 new{} ...

  6. ajax请求模拟登录

    前台 @if (Session["username"] != null) { <div class="login"> <span style= ...

  7. 【BZOJ1013】【JSOI2008】球形空间产生器sphere(高斯消元)

    1013: [JSOI2008]球形空间产生器sphere Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 1600  Solved: 860[Submi ...

  8. javascript继承(五)—prototype最优两种继承(空函数和循环拷贝)

    一.利用空函数实现继承 参考了文章javascript继承—prototype属性介绍(2) 中叶小钗的评论,对这篇文章中的方案二利用一个空函数进行修改,可以解决创建子类对象时,父类实例化的过程中特权 ...

  9. AngularJs——grunt神器的使用

    前面我们已经知道了如何安装grunt,本章节给各位道友介绍如何使用 grunt 的插件,grunt是重点在于如何配置使用 Gruntfile.js,官网上也有很多范例. 1,包装函数 module.e ...

  10. HTML5——播放器

    有了H5的Video,妈妈再也不用担心我没安Flash插件了 根据Video提供的方法和属性,简单练习了一下,不说废话,直接上图片和代码 <html><head><tit ...