zoj 2966 Build The Electric System
就是套了个prim算法就ac了
#include <stdio.h>
#include <string.h>
#define MaxInt 0x3f3f3f3f
#define N 510
/*创建map二维数组储存图表,low数组记录每2个点间最小权值,visited数组标记某点是否已访问*/
int map[N][N],low[N],visited[N];
int n;
int prim()
{
int i,j,pos,min,result=;
memset(visited,,sizeof(visited));
/*从某点开始,分别标记和记录该点*/
visited[]=;pos=;
/*第一次给low数组赋值*/
for(i=;i<=n;i++)
if(i!=pos) low[i]=map[pos][i];
/*再运行n-1次*/
for(i=;i<n;i++)
{
/*找出最小权值并记录位置*/
min=MaxInt;
for(j=;j<=n;j++)
if(visited[j]==&&min>low[j])
{
min=low[j];pos=j;
}
/*最小权值累加*/
result+=min;
/*标记该点*/
visited[pos]=;
/*更新权值*/
for(j=;j<=n;j++)
if(visited[j]==&&low[j]>map[pos][j])
low[j]=map[pos][j];
}
return result;
} int main()
{
int i,v,j,ans,s,e,t,m;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
/*所有权值初始化为最大*/
memset(map,MaxInt,sizeof(map));
for(i=;i<=m;i++)
{
scanf("%d%d%d",&s,&e,&v);
map[s+][e+]=map[e+][s+]=v;
}
ans=prim();
printf("%d\n",ans);
}
return ;
}
zoj 2966 Build The Electric System的更多相关文章
- zoj 2966 Build The Electric System(最小生成树)
Build The Electric System Time Limit: 2 Seconds Memory Limit: 65536 KB In last winter, there wa ...
- zoj 2966 Build The Electric System 最小生成树
Escape Time II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showP ...
- B - Build The Electric System 求强连通的最小和//lxm
有n个城市,有m条线路,每条线路a,b,len表示a到b的线路需要花费len的费用维修,要求能将所有城市联通的最小维修花费 按照排序排一下然后利用并查集解决 #include <iostream ...
- Sublime Text 3 Build 3065 All System CracKed By Hmily[LCG]
Sublime Text 3 Build 3065 All System CracKed By Hmily[LCG] <ignore_js_op> 程序员文本编辑器 Sublime Tex ...
- nenu contest3 The 5th Zhejiang Provincial Collegiate Programming Contest
ZOJ Problem Set - 2965 Accurately Say "CocaCola"! http://acm.zju.edu.cn/onlinejudge/showP ...
- 杭电ACM分类
杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...
- 转载:hdu 题目分类 (侵删)
转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...
- QDU_组队训练(ABEFGHKL)
A - Accurately Say "CocaCola"! In a party held by CocaCola company, several students stand ...
- 2019/12/22 TZOJ
4986 Team Formation http://www.tzcoder.cn/acmhome/problemdetail.do?&method=showdetail&id=498 ...
随机推荐
- onActivityResult不起作用?可能是和你的launchMode有关!
昨天在自己的项目中用到了onActivityResult()方法获得activity的返回值.我从Activity01通过 startActivityForResult启动了Activity02和Ac ...
- Open source and free log analysis and log management tools.
Open source and free log analysis and log management tools. Maintained by Dr. Anton Chuvakin Version ...
- APK反编译。
http://blog.csdn.net/ithomer/article/details/6727581?reload
- openssl编译(VC6.0)
官网:http://www.openssl.org/ 得到源码: git clone https://github.com/openssl/openssl 一.用vc编译器编译: 1.下载nasm: ...
- js完美继承代码示例
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- S3C2440 之SPI
概述: S3C2440有两个串行外设SPI接口,SPI具有全双工通信 SPI方框图 SPI操作: 通过使用SPI接口,S3C2440可以与外部器件同时发送.接收8位数据.当SPI接口为主机时,可以通过 ...
- WEBAPP组件化时代, Web Components
polymer ==> http://docs.polymerchina.org/ angular ==> http://www.ngnice.com/docs/guide scr ...
- 编写可维护的JS 04
4.变量.函数和运算符 变量 变量声明提前,单var 函数声明 先声明fn再执行 函数声明不应出现在语句块中 函数调用间隔 函数名与左括号间无间隔 立即调用函数 (fuction(){}) 严格模式 ...
- sql大数据量查询的优化技巧
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- android 文件操作类简易总结
android 文件操作类(参考链接) http://www.cnblogs.com/menlsh/archive/2013/04/02/2997084.html package com.androi ...