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. 技术分析:Femtocell家庭基站通信截获、伪造任意短信

    阿里移动安全团队与中国泰尔实验室无线技术部的通信专家们一起,联合对国内运营商某型Femtocell基站进行了安全分析,发现多枚重大漏洞,可导致用户的短信.通话.数据流量被窃听.恶意攻击者可以在免费申领 ...

  2. 慢牛股票-基于Sencha+Cordova的股票类APP

    13,14这两年,我的业余时间都花在了移动互联网技术和股票技术分析上,14年底,终于开发完成慢牛,上线小米应用商店.应用宝.百度应用商店.   慢牛是一款数据分析类的股票APP,提供数据订阅和数据分析 ...

  3. 风清杨之Oracle的安装与说明

    1.Oracle官网与下载地址 Oracle中文官网:http://www.oracle.com/cn/index.html Oracle中文官网下载:http://www.oracle.com/te ...

  4. [C/C++基础] C语言常用函数sprintf和snprintf的使用方法

    Sprintf 函数声明:int sprintf(char *buffer, const char *format [, argument1, argument2, …]) 用途:将一段数据写入以地址 ...

  5. 编译到底做了什么(***.c -> ***.o的过程)

     (第一次写博客,好激动的说.......)   我们知道,一个程序由源代码到可执行文件往往由这几步构成: 预处理(Prepressing)-> 编译(Compilation)-> 汇编( ...

  6. Object C学习笔记14-分类(category)

    在.NET中有一个非常带劲的特性,那就是扩展方法. 扩展方法使你能够向现有类型“添加”方法(包括你自定义的类型和对象噢),而无需创建新的派生类型.重新编译或以其他方式修改原始类型.扩展方法是一种特殊的 ...

  7. Windows Office key 持续更新地址

    微软 Windows  Office 系列序列号,每日都有更新,上面显示的基本都可用,包括MAK及Retail Key. Windows 10   http://textuploader.com/52 ...

  8. hdu1711 KMP

    #include<stdio.h> #include<string.h> #define maxn 1000010 int next[maxn],s[maxn],p[maxn] ...

  9. Java原来如此-遍历Map的三种方法

    import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; pub ...

  10. 6.Android之switch和togglebutton按钮学习

    Switch和ToggleButton按钮在手机上也经常看到,比如手机设置里面wlan,蓝牙,gps开关等. 首先在工具上拖进一个switch和togglebutton开关按钮,如图 生成xml代码如 ...