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. Android WebView使用深入浅出

    目前很多android app都内置了可以显示web页面的界面,会发现这个界面一般都是由一个叫做WebView的组件渲染出来的,学习该组件可以为你的app开发提升扩展性. 先说下WebView的一些优 ...

  2. 傻傻分不清楚的php脚本路径

     闲话就不说了,还是直接提出今天的问题,准确的说,对多个相似的 有关当前脚本信息的全局变量常量的区分. 先贴上代码: <?php echo $_SERVER['PHP_SELF']; echo ...

  3. MVC采用HtmlHelper扩展和Filter封装验证码的功能

    最近因为有个项目除了登录还有其他很多地方需要用到验证码的功能,所以想到了采用HtmlHelper和ActionFilter封装一个验证码的功能,以便能够重复调用.封装好以后调用很方便,只需在View中 ...

  4. javascript中的闭包,超简单论述,保证小学生必懂

    js中的闭包已经有很多论断了,大家伙有没有听懂了,先引用一片比较高端 的 ”汤姆大叔“  深入理解JavaScript系列(16):闭包(Closures) 好了,为了引起大家的兴趣,先来小诗一首 v ...

  5. 在Ubuntu-14.04.3配置并成功编译Android6_r1源码

    折腾了一周,终于把Android6_r1的源码编译成功.先上图,这是在ubuntu中运行的Android模拟器: 由于我是在win8中安装虚拟机VMware,然后在虚拟机中安装Ubuntu进行编译,所 ...

  6. Boostrap(3)

    常用标签 1.文字 p标签(段落) small标签(让文字呈现灰色) em标签(文字斜体) blokcquote标签(文字内容为引用时用该标签) class=”pull-right"右浮动 ...

  7. IOS 计算两个经纬度之间的距离

    IOS 计算两个经纬度之间的距离 一 丶 -(double)distanceBetweenOrderBy:(double) lat1 :(double) lat2 :(double) lng1 :(d ...

  8. android常用的弹出提示框

    我们在平时做开发的时候,免不了会用到各种各样的对话框,相信有过其他平台开发经验的朋友都会知道,大部分的平台都只提供了几个最简单的实现,如果我们想实现自己特定需求的对话框,大家可能首先会想到,通过继承等 ...

  9. iOS开发小技巧--利用运行时得到隐藏的成员变量

    一.关于运行时,已经从网络上摘抄了一片文章,这里只有项目中自己的简单使用 -- 查找隐藏的成员变量 导入头文件 可以获得隐藏的成员变量,方法,属性等 代码: 打印效果图:

  10. hdu3746 KMP

    这题琢磨了挺长的时间.需要理解next[]表示了什么; next[i]代表了前缀和后缀的最大匹配的值,也就是个数. len-next[len]表示循环节的长度; 比如abcab   int fl=le ...