注意输入的数据分别是做什么的就好。还有,以下代码用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 ;
}

最新文章

  1. [LeetCode] Word Ladder II 词语阶梯之二
  2. [Python]实现简单决策树
  3. 使用lsof查看进程句柄使用情况
  4. linux下 C++ 读取mat文件 MATLAB extern cyphon scipy 未完待续
  5. [办公自动化]skydrive onedrive
  6. 谷歌技术&amp;quot;三宝&amp;quot;之MapReduce
  7. Java_spring_定时执行任务
  8. Asp.Net Design Pattern Studynotes -- Part1
  9. 学习笔记 之--AJAX核心对象 XMLHttpRequest
  10. IE6 png图片实现半透明的方法
  11. 从零开始学 Web 之 CSS(三)链接伪类、背景、行高、盒子模型、浮动
  12. DataGridView导出数据到Excel
  13. 通过 PHP,可以把文件上传到服务器。
  14. 05 Hadoop 设置块的大小
  15. 《InsideC#》笔记(十) 异常处理
  16. redis常见应用场景
  17. 设置Eclipse具有字母自动联想
  18. vue常见开发问题整理
  19. 变量,if.elif .else判断
  20. HQL语句的3个小技巧

热门文章

  1. 构建一个基于 Spring 的 RESTful Web Service
  2. 设计模式之(二)Adapter模式
  3. linux系统批量无人值守安装
  4. VC++ 坐标问题总结,控件大小随窗口变化
  5. Linux是如何启动的
  6. jquery之radio
  7. 豆瓣移动端风格的css命名方法与学习
  8. 附加到IIS调试出现不会命中断点
  9. 【Solr初探】Solr安装,启动,查询,索引
  10. C#创建Windows服务的几个注意事项