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 ...
随机推荐
- C++primer读书笔记11-多态
多态也是C++中的一个重要的方面.多态和动态类型,虚函数本质上是指同样的事情. 1 虚函数 类中的成员函数原型前面加上virtual 表面这个函数是个虚函数.虚函数的目的是为了在继承它的派生类中又一次 ...
- light oj 1094 Farthest Nodes in a Tree(树的直径模板)
1094 - Farthest Nodes in a Tree problem=1094" style="color:rgb(79,107,114)"> probl ...
- light oj 1317
Description You probably have played the game "Throwing Balls into the Basket". It is a si ...
- 2015-8-29阿里校园招聘研发project师笔试题
前言:原题来自于网络:http://www.cnblogs.com/nausicaa/p/3946694.html.本人依据自己理解对题目进行解答.因为水平有限.题目有不会做.做错的地方.欢迎大家留言 ...
- litepal更好的操作sqlite3,配置与基本操作
litepal更好的操作sqlite3 配置 在app下的build.gradle中加入 创建litepal.xml配置 在app/src/main下面创建assets,新建litepal.xml & ...
- BZOJ 1927 最小费用流问题
From lydrainbowcat //By SiriusRen #include <queue> #include <cstdio> #include <cstrin ...
- POJ 4007 Flood-it!
题目:http://poj.org/problem?id=4007 思路: (lyd学长的思路) IDA*算法,首先迭代加深限制搜索深度. 可以发现如果当前矩阵中除了左上角的连通块之外,共有M种颜色, ...
- settings.xml配置的镜像
<localRepository>D:/apache-maven-3.5.4/maven-jar/repository</localRepository> <mirror ...
- Spring的注解@SuppressWarnings用法记录
@SuppressWarnings注解用法 @SuppressWarnings注解主要用在取消一些编译器产生的警告对代码左侧行列的遮挡,有时候这会挡住我们断点调试时打的断点. 如图所示: 这时候我们在 ...
- Linux Putty 复制粘贴
从putty复制: 用左键选中文字,再其他地方点中键就可以粘贴了,不需要右键粘贴. 在putty中粘贴: 在其他地方用左键选中文字,不用右键复制,保持选中文字高亮,然后在putty中点中键 ...