Connect the Cities(MST prim)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
Input
Output
Sample Input
Sample Output
#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了,
Connect the Cities(MST prim)的更多相关文章
- Connect the Cities(prim)用prim都可能超时,交了20几发卡时过的
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3371 Connect the Cities(prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...
- hdu3371 Connect the Cities (MST)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 3371 Connect the Cities (最小生成树Prim)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...
- HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑
这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...
- Connect the Cities[HDU3371]
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu oj 3371 Connect the Cities (最小生成树)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 3371 Connect the Cities(最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...
- hdu 3371 Connect the Cities
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...
随机推荐
- MVC5 + EF6 + Bootstrap3 (7) Bootstrap的栅格系统
文章来源: Slark.NET-博客园http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-grid.html 上一节:ASP.NET MVC ...
- HoloLens开发手记 - Unity之摄像头篇
当你穿戴好HoloLens后,你就会处在全息应用世界的中心.当你的项目开启了"Virtual Reality Support"选项并选中了"Windows Hologra ...
- 《TCP/IP详解卷1:协议》第11章 UDP:用户数据报协议-读书笔记
章节回顾: <TCP/IP详解卷1:协议>第1章 概述-读书笔记 <TCP/IP详解卷1:协议>第2章 链路层-读书笔记 <TCP/IP详解卷1:协议>第3章 IP ...
- 【Moqui框架】Moqui连接各种类型的数据库
Moqui连接mysql数据库 各种数据库的连接文本: -- Derby<datasource group-name="transactional" database-con ...
- 调研Android Studio开发环境的发展演变(附安装教程,多图)
Android Studio(以下简称AS)第一次公开亮相是在2013年的谷歌I/O大会上,14年的大会上谷歌发布其试用测试版,如今AS已经历数次版本更新,功能十分强大.如(摘自百度百科Android ...
- 使用Web Deploy进行远程部署
Web Deploy支持直接从本地Visual Studio的工程文件部署网站到远程服务器,部署的过程中可以对比哪些文件变化了需要拷贝,而不是一股脑的全部拷贝,效率和准确性会更好. 部署的过程主要要注 ...
- iOS开发小技巧--UIScrollView内部子控件添加约束的注意点
注意:用UIScrollView时布局子控件的时候,不要相对于UIScrollView来添加约束,这样做不是设置子控件的位置,反而是设置了UIScrollView的contentSize 子控件的尺寸 ...
- poj3692 最大点权独立集/最大独立集
题意:有男孩和女孩,男孩之间全部认识,女孩之间全部认识,一部分男孩和女孩认识,现在希望选出一些孩子,这些孩子都相互认识. 方法:正的做不好做,观察他的补图,补图之间无关系的边就是原图有关系的.补图中的 ...
- hdu2923 最短路floyd
建图还是有点烦人的. #include<map> #include<string> #include<stdio.h> #include<iostream&g ...
- 【BZOJ-1406】密码箱 约数 + 乱搞 + set?
1406: [AHOI2007]密码箱 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1143 Solved: 677[Submit][Status][ ...