【HDU3371】Connect the Cities(MST基础题)
注意输入的数据分别是做什么的就好。还有,以下代码用C++交可以过,而且是500+ms,但是用g++就会TLE,很奇怪。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <numeric>
#include <limits.h> #define typec int
using namespace std; const typec inf = 0xffff;
const int V = ;
int vis[V]; typec lowc[V];
int Map[V][V]; typec prim (typec cost[][V], int n) {
int i, j, p;
typec minc, res = ;
memset(vis, , sizeof(vis));
vis[] = ;
for (i = ; i < n; ++ i) lowc[i] = cost[][i];
for (i = ; i < n; ++ i) {
minc = inf;
p = -;
for (j = ; j < n; ++ j) {
if ( == vis[j] && minc > lowc[j]) {
minc = lowc[j];
p = j;
}
}
if (inf == minc) return -;
res += minc;
vis[p] = ;
for (j = ; j < n; ++ j) {
if ( == vis[j] && lowc[j] > cost[p][j]) {
lowc[j] = cost[p][j];
}
}
}
return res;
} int main () {
int n, road_n, union_n, T;
scanf("%d", &T);
while (T--) {
scanf("%d%d%d", &n, &road_n, &union_n);
//cout << "road_n : " << road_n << endl;
for (int i = ; i < V; ++ i) {
for (int j = ; j < V; ++ j) {
if (i == j) Map[i][j] = ;
else Map[i][j] = inf;
}
} for (int i = ; i < road_n; ++ i) {
int x, y, w;
scanf("%d%d%d", &x, &y, &w);
Map[x - ][y - ] = Map[y - ][x - ] = min(Map[x - ][y - ], w); }
/*
for (int i = 0 ; i < n; ++ i) {
for (int j = 0; j < n; ++ j) {
cout << Map[i][j] << "\t";
}
cout << endl;
}
*/
for (int i = ; i < union_n; ++ i) {
int xx, xx_n, last;
scanf("%d", &xx_n);
for (int j = ; j < xx_n; ++ j) {
scanf("%d", &xx);
if (!j) {
last = xx;
continue;
} else {
Map[xx - ][last - ] = Map[last - ][xx - ] = ;
last = xx;
}
}
} printf("%d\n", prim(Map, n));
}
return ;
}
【HDU3371】Connect the Cities(MST基础题)的更多相关文章
- hdu3371 Connect the Cities (MST)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Connect the Cities(MST prim)
Connect the Cities Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- 【HDU1162】Eddy's picture(MST基础题)
很基础的点坐标MST,一不留神就AC了, - - !! #include <iostream> #include <cstring> #include <cstdlib& ...
- 【HDU1301】Jungle Roads(MST基础题)
爽爆.史上个人最快MST的记录7分40s..一次A. #include <iostream> #include <cstring> #include <cstdlib&g ...
- 【HDU1879】继续畅通工程(MST基础题)
真心大水题...不多说. #include <iostream> #include <cstring> #include <cstdlib> #include &l ...
- 【HDU1233】还是畅通工程(MST基础题)
无坑,裸题.直接敲就恩那个AC. #include <iostream> #include <cstring> #include <cstdio> #include ...
- 【HDU1102】Constructing Roads(MST基础题)
最小生成树水题.prim一次AC #include <iostream> #include <cstring> #include <cstdlib> #includ ...
- 【HDU1875】畅通工程再续(MST基础题)
更改成实形数即可.第一次敲完直接交,CE了一次.晕. #include <iostream> #include <cstring> #include <cstdio> ...
- 【HDU2122】Ice_cream’s world III(MST基础题)
2坑,3次WA. 1.判断重边取小.2.自边舍去. (个人因为vis数组忘记初始化,WA了3次,晕死!!) #include <iostream> #include <cstring ...
随机推荐
- 最快速的“高斯”模糊算法(附Android源码)
这是一个外国人的算法,本人是搬运工.参考:http://blog.ivank.net/fastest-gaussian-blur.html 1:高斯模糊算法(参考:http://www.rua ...
- C语言伪随机数的生成
在stdlib.h中,有两个函数与伪随机数的生成有关:srand和rand.C语言中,随机数表有很多列,srand函数是根据其参数(unsigned类型)来获得一个种子(seed),根据种子来设置从哪 ...
- poj 2407 Relatives(简单欧拉函数)
Description Given n, a positive integer, how many positive integers less than n are relatively prime ...
- C#生成带项目编号的Word段落
using System; using Microsoft.Office.Interop.Word; using Word = Microsoft.Office.Interop.Word; names ...
- 小米路由器mini建FTP
输入命令 df -h 复制代码 查看自己的分区路径,我自己用了一个闲置笔记本电脑硬盘转USB,插到mini,有四个区 为了方便,我选择把ftp安装在第一个分区 /extdisks/sda1/ 创建f ...
- Android Fragment详解(三): 实现Fragment的界面
为fragment添加用户界面: Fragment一般作为activity的用户界面的一部分,把它自己的layout嵌入到activity的layout中. 一个 要为fragment提供layout ...
- Dynamics CRM 常用 C# 方法集合
Plugin(C#) 分派 AssignRequest assign = new AssignRequest(); assign.Assignee = prEntity["ownerid&q ...
- ORA-24324、ORA-12560、ORA-12514
SQL> startup ERROR: ORA-24324: 未初始化服务句柄 ORA-01041: 内部错误, hostdef 扩展名不存在. SQL> conn sys /nolog; ...
- HTML与CSS入门——第八章 使用外部和内部链接
知识点: 1.链接锚的使用方法 2.在自己的网站上的页面之间链接的方法 3.链接到外部内容的方法 4.链接到一个E-mail地址的方法 5.在新浏览器窗口中查看链接的方法 6.用CSS为链接添加样式的 ...
- RadGrid SelectedIndexChanged 事件没反应的解决方法
Hello Hrushikesh, You can set ClientSettings.EnablePostBackOnRowClick to true along with ClientSetti ...