【HDU3371】Connect the Cities(MST基础题)
2024-08-25 21:49:03
注意输入的数据分别是做什么的就好。还有,以下代码用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 ;
}
最新文章
- [LeetCode] Word Ladder II 词语阶梯之二
- [Python]实现简单决策树
- 使用lsof查看进程句柄使用情况
- linux下 C++ 读取mat文件 MATLAB extern cyphon scipy 未完待续
- [办公自动化]skydrive onedrive
- 谷歌技术&;quot;三宝&;quot;之MapReduce
- Java_spring_定时执行任务
- Asp.Net Design Pattern Studynotes -- Part1
- 学习笔记 之--AJAX核心对象 XMLHttpRequest
- IE6 png图片实现半透明的方法
- 从零开始学 Web 之 CSS(三)链接伪类、背景、行高、盒子模型、浮动
- DataGridView导出数据到Excel
- 通过 PHP,可以把文件上传到服务器。
- 05 Hadoop 设置块的大小
- 《InsideC#》笔记(十) 异常处理
- redis常见应用场景
- 设置Eclipse具有字母自动联想
- vue常见开发问题整理
- 变量,if.elif .else判断
- HQL语句的3个小技巧