【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 ...
随机推荐
- [转载]date命令时间转换
Linux时间戳和标准时间的互转 在LINUX系统中,有许多场合都使用时间戳的方式表示时间,即从1970年1月1日起至当前的天数或秒数.如/etc/shadow里的密码更改日期和失效日期,还有代理服务 ...
- html--鼠标控制DIV移动
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- ASIHttpRequest:创建队列、下载请求、断点续传、解压缩
ps:本文转载自网络:http://ryan.easymorse.com/?p=12 感谢作者 工程完整代码下载地址:RequestTestDownload1 可完成: 下载指定链接的zip压缩文件 ...
- tomcat下配置https环境
在网上搜了一下,内容不是非常完好. 现进行整理,做个学习笔记,以备以后使用. (1)进入到jdk下的bin文件夹 (2)输入例如以下指令"keytool -v -genkey -alias ...
- .NET基础拾遗(4)委托为何而生?
生活中的例子: 你早上要吃包子作为早饭,那么你可能让你爸爸或者妈妈帮你做,那你就会调用 爸爸.要包子() 或妈妈.要包子() 返回包子对象. 但是如果你爸妈不在家的时候,你只能去街上买,问题是你根本不 ...
- Css控制div水平垂直居中显示
<style>#info{height:0px; width:0px;top:50%; left:50%;position:absolute;}#center{background:#FF ...
- 华为Oj 找出字符串第一个出现一次的字符
#include <stdio.h> #include <string.h> char firstSingle(char *str) { int hash[255]={0}; ...
- OpenGL ES 2.0 曲面物体的构建
球体构建的基本原理构建曲面物体最重要的就是找到将曲面恰当拆分成三角形的策略. 最基本的策略是首先按照一定的规则将物体按行和列两个方向进行拆分,这时就可以得到很多的小四边形.然后再将每个小四边形拆分成两 ...
- (原)torch中显示nn.Sequential()网络的详细情况
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6065526.html 本部分多试几次就可以弄得清每一层具体怎么访问了. step1. 网络定义如下: ...
- CSS3 中的按钮效果与进度条
效果如图