COGS——T 7. 通信线路
http://www.cogs.pro/cogs/problem/problem.php?pid=7
★★ 输入文件:mcst.in 输出文件:mcst.out 简单对比
时间限制:1.5 s 内存限制:128 MB
第一行,一个整数n,表示共有n个城市
第2--n+1行,每行n个数,分别表示该城市与其它城市之间路线的费用,如果城市间不能建立通信则用-1表示
-1 5 -1 -1 -1 -1
5 -1 50 -1 -1 10
-1 50 -1 20 10 -1
-1 -1 20 -1 60 30
-1 -1 10 60 -1 100
-1 10 -1 30 100 -1
对于60%的数据,保证有n<256;
对于全部的数据,保证有n<=1501。
#include<algorithm>
#include<cstdio> using namespace std; const int N();
int n,tot,fa[N];
struct Edge
{
int u,v,w;
} edge[N*N>>];
bool cmp(Edge a,Edge b)
{
return a.w<b.w;
} int find(int x)
{
return fa[x]==x?x:fa[x]=find(fa[x]);
} inline void read(int &x)
{
x=; int if_=;char ch=getchar();
for(; ch<''||ch>''; ch=getchar()) if(ch=='-') if_=;
for(; ch>=''&&ch<=''; ch=getchar()) x=x*+ch-'';
x=!if_?x:((~x)+);
} int main()
{
freopen("mcst.in","r",stdin);
freopen("mcst.out","w",stdout);
read(n);
for(int x,i=; i<=n; i++)
for(int j=; j<=n; j++)
{
read(x);
if(x!=-)
{
edge[++tot].u=i,
edge[tot].v=j;
edge[tot].w=x;
}
}
for(int i=; i<=n; i++) fa[i]=i;
sort(edge+,edge++tot,cmp);
int cnt=,ans=;
for(int i=; i<=tot; i++)
{
int x=edge[i].u,y=edge[i].v;
int fx=find(x),fy=find(y);
if(fx==fy) continue;
fa[fx]=fy;
ans+=edge[i].w;
if(++cnt==n-) break;
}
printf("%d",ans);
return ;
}
COGS——T 7. 通信线路的更多相关文章
- cogs——7. 通信线路
7. 通信线路 ★★ 输入文件:mcst.in 输出文件:mcst.out 简单对比时间限制:1.5 s 内存限制:128 MB 问题描述 假设要在n个城市之间建立通信联络网,则连通n ...
- cogs 7. 通信线路
7. 通信线路 ★★ 输入文件:mcst.in 输出文件:mcst.out 简单对比时间限制:1.5 s 内存限制:128 MB 问题描述 假设要在n个城市之间建立通信联络网,则连通n ...
- 【COGS 254】【POI 2001】交通网络图
http://www.cogs.top/cogs/problem/problem.php?pid=254 dist[i]表示能最早到达i点的时间.这样就可以用最短路模型来转移了. #include&l ...
- 【COGS】894. 追查坏牛奶
http://cojs.tk/cogs/problem/problem.php?pid=894 题意:n个点m条边的加权网络,求最少边数的按编号字典序最小的最小割.(n<=32, m<=1 ...
- 【COGS】147. [USACO Jan08] 架设电话线(二分+spfa)
http://cojs.tk/cogs/problem/problem.php?pid=147 学到新姿势了orz 这题求的是一条1-n的路径的最大路径最小. 当然是在k以外的. 我们可以转换一下. ...
- 【COGS & USACO Training】710. 命名那个数字(hash+水题+dfs)
http://cojs.tk/cogs/problem/problem.php?pid=710 近日开始刷水... 此题我为了练一下hash...但是hash跑得比暴力还慢.. 不言而喻... #in ...
- 【COGS & USACO】896. 圈奶牛(凸包)
http://cojs.tk/cogs/problem/problem.php?pid=896 我的计算几何入门题... 看了看白书的计算几何部分,,恩好嘛.. 乃们都用向量!!!! 干嘛非要将2个点 ...
- 【COGS】714. USACO 1.3.2混合牛奶(贪心+水题)
http://cojs.tk/cogs/problem/problem.php?pid=714 在hzwer的刷题记录上,默默地先跳过2题T_T...求凸包和期望的..T_T那是个啥..得好好学习 看 ...
- Cogs 97. [NOIP2007] 树网的核 Floyd
题目: http://cojs.tk/cogs/problem/problem.php?pid=97 97. [NOIP2007] 树网的核 ★☆ 输入文件:core.in 输出文件:core ...
随机推荐
- CodeForces 550B Preparing Olympiad(DFS回溯+暴力枚举)
[题目链接]:click here~~ [题目大意] 一组题目的数目(n<=15),每一个题目有对应的难度,问你选择一定的题目(大于r个且小于l个)且选择后的题目里最小难度与最大难度差不小于x, ...
- Fragment使用的正确姿势
网上关于Fragment的使用建议和分析非常多,可是依旧会有非常多人在使用Fragment的时候出现各种奇葩错误 这篇文章我分享一下各种项目中解决Fragment的各种注意事项(不做原理分析), ...
- ELK搭建(filebeat、elasticsearch、logstash、kibana)
ELK部署(文章有点儿长,搭建时请到官网将tar包下载好,按步骤可以完成搭建使用) ELK指的是ElasticSearch.LogStash.Kibana三个开源工具 LogStash是负责数据的收集 ...
- [NOIP2015模拟10.22] 最大子矩阵 解题报告(单调栈)
Description 我们将矩阵A中位于第i行第j列的元素记作A[i,j].一个矩阵A是酷的仅当它满足下面的条件: A[1,1]+A[r,s]<=A[1,s]+A[r,1](r,s ...
- 安装meteor运行基本demo发生错误。
bogon:~ paul$ curl https://install.meteor.com/ | sh % Total % Received % Xferd Average Speed Time Ti ...
- javascript 优秀写法
http://www.csdn.net/article/2014-01-06/2818025-Useful-JavaScript-Tips-Best-Practices
- Mojo For Chromium Developers1
Mojo For Chromium Developers Overview This document contains the minimum amount of information neede ...
- Mojo C++ Platform API
Mojo C++ Platform API This document is a subset of the Mojo documentation. Contents Overview Platfor ...
- 列表的所有的input,将它的值以键值对的形式存放到一个数组里
要求的格式 代码块 $('.btn-confirm').on('tap',function(){ var arr={}; var name = $("input[name='insuranc ...
- vue项目的一些最佳实践提炼和经验总结
项目组织结构 ajax数据请求的封装和api接口的模块化管理 第三方库按需加载 利用less的深度选择器优雅覆盖当前页面UI库组件的样式 webpack实时打包进度 vue组件中选项的顺序 路由的懒加 ...