注意输入的数据分别是做什么的就好。还有,以下代码用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基础题)的更多相关文章

  1. hdu3371 Connect the Cities (MST)

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. Connect the Cities(MST prim)

    Connect the Cities Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  3. 【HDU1162】Eddy's picture(MST基础题)

    很基础的点坐标MST,一不留神就AC了, - - !! #include <iostream> #include <cstring> #include <cstdlib& ...

  4. 【HDU1301】Jungle Roads(MST基础题)

    爽爆.史上个人最快MST的记录7分40s..一次A. #include <iostream> #include <cstring> #include <cstdlib&g ...

  5. 【HDU1879】继续畅通工程(MST基础题)

    真心大水题...不多说. #include <iostream> #include <cstring> #include <cstdlib> #include &l ...

  6. 【HDU1233】还是畅通工程(MST基础题)

    无坑,裸题.直接敲就恩那个AC. #include <iostream> #include <cstring> #include <cstdio> #include ...

  7. 【HDU1102】Constructing Roads(MST基础题)

    最小生成树水题.prim一次AC #include <iostream> #include <cstring> #include <cstdlib> #includ ...

  8. 【HDU1875】畅通工程再续(MST基础题)

    更改成实形数即可.第一次敲完直接交,CE了一次.晕. #include <iostream> #include <cstring> #include <cstdio> ...

  9. 【HDU2122】Ice_cream’s world III(MST基础题)

    2坑,3次WA. 1.判断重边取小.2.自边舍去. (个人因为vis数组忘记初始化,WA了3次,晕死!!) #include <iostream> #include <cstring ...

随机推荐

  1. python核心编程第二版笔记

    python核心编程第二版笔记由网友提供:open168 python核心编程--笔记(很详细,建议收藏) 解释器options:1.1 –d   提供调试输出1.2 –O   生成优化的字节码(生成 ...

  2. 万恶DevExpress

    公司需要,开始了DevExpress的学习之旅,说它万恶也只是在不了解它的情况下,熟悉之后能很方便的实现很多想要的功能 这里简单写一下要整理的内容,也就是大纲,以后再慢慢添加 一.控件和组件 date ...

  3. [RxJS] Handling Multiple Streams with Merge

    You often need to handle multiple user interactions set to different streams. This lesson shows hows ...

  4. Android实现左右滑动效果

    本示例演示在Android中实现图片左右滑动效果.   关于滑动效果,在Android中用得比较多,本示例实现的滑动效果是使用ViewFlipper来实现的,当然也可以使用其它的View来实现.接下来 ...

  5. 浅谈对JIT编译器的理解。

    1. 什么是Just In Time编译器? Hot Spot 编译 当 JVM 执行代码时,它并不立即开始编译代码.这主要有两个原因: 首先,如果这段代码本身在将来只会被执行一次,那么从本质上看,编 ...

  6. samba常用命令

    1.# smbstatusSamba version 3.6.23PID Username Group Machine ---------------------------------------- ...

  7. Code First 数据注释--DatabaseGenerated

    EF中可以使用DatabaseGenerated设置数据库字段的属性,它有三个枚举值:Computed.None.Identity Computed 表示这一列是计算所得 None 不做处理 Iden ...

  8. gridView 布局间距合理化的有效办法

    网上有很多人闻到过这个问题,给出好多办法,多数人并不是意见提的不对,而是没有抓住问题的本质 . 因为我要做一个girdView的demo实例,遇到了这个问题 与大家一起分享,如果有想要demo的请给我 ...

  9. Xcode快捷键 ---- 提高效率

    Mac中主要有四个修饰键,分别是Command,Control,Option和Shift.     1. ⌘ + L 搜索行数,输入行数,调到指定行数   2.⌘ + shift + O 查询flie ...

  10. Lightoj1009 Back to Underworld(带权并查集)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Back to Underworld Time Limit:4000MS      ...