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 ...
随机推荐
- Python Tkinter 基础控件学习
# -*- coding: utf-8 -*- from Tkinter import * def btn_click(): b2['text'] = 'clicked' evalue = e.get ...
- HDU5053the Sum of Cube(水题)
HDU5053the Sum of Cube(水题) 题目链接 题目大意:给你L到N的范围,要求你求这个范围内的全部整数的立方和. 解题思路:注意不要用int的数相乘赋值给longlong的数,会溢出 ...
- iOS 报错:(子线程中更新UI)This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.
今天在写程序的时候,使用Xcode 运行工程时报出下面的错误错信息,我还以为是什么呢,好久没遇到过这样的错误了. **ProjectName[1512:778965] This application ...
- thinkphp5项目--个人博客(六)
thinkphp5项目--个人博客(六) 项目地址 fry404006308/personalBlog: personalBloghttps://github.com/fry404006308/per ...
- ASP.NET Identity 角色管理(Roles)
当我们使用ASP.NET 4.5创建模板项目时,会发现模板只提供了ApplicationUserManager用于用户的登录注册.修改.设置等,而没有提供与用户角色相关的代码,对此就需要我们自己手动的 ...
- 解决夸dll返回dynamic无法访问
public static class DynamicFactory { //创建线程安全(对象不会再同一时刻被多个线程访问)的字典对象 private static ConcurrentDictio ...
- 洛谷1525 关押罪犯NOIP2010 并查集
问题描述 S城现有两座监狱,一共关押着N名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨气值”(一个正整数值)来表示某两 ...
- 学习参考《高性能MySQL(第3版)》中文PDF+英文PDF
学习mysql数据库时推荐看看mysql 领域的经典之作<高性能mysql(第3版)>,共分为16 章和6 个附录,内容涵盖mysql 架构和历史,基准测试和性能剖析,数据库软硬件性能优化 ...
- Unity 编辑器学习(三)之 Light & Baked
上一篇博客已经详细的介绍GI了,接下来我们讲点实际的,怎么烘焙场景及注意事项. 一.Light Property: Function: Type 当前灯光的类型.有四种类型:Directional, ...
- minixml的安装教程
最近想要实现对xml的解析,上网看到有很多库可以直接调用,例如minixml,tinyxml等,我选择了minixml(没有原因,纯属是因为这个找到了中文文档- -) 附上中文文档链接:https:/ ...