http://www.cogs.pro/cogs/problem/problem.php?pid=7

★★   输入文件:mcst.in   输出文件:mcst.out   简单对比
时间限制:1.5 s   内存限制:128 MB

问题描述
假设要在n个城市之间建立通信联络网,则连通n个城市只需要n-1条线路。这时, 如何在最少经费的前提下建立这个通信网。在每两个城市之间都可以设置—条线路,相应地都要付出一定的经济代价。n个城市之间,最多可能设置n(n- 1)/2条线路,那么,如何在这些可能的线路中选择n-1条,以使总的耗费最少呢?
 
【输入格式】
输入文件有若干行
第一行,一个整数n,表示共有n个城市
第2--n+1行,每行n个数,分别表示该城市与其它城市之间路线的费用,如果城市间不能建立通信则用-1表示
 
【输出格式】
一行,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
 
输出文件
 
75
 
【数据规模】
 
对于40%的数据,保证有n<100: 
对于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. 通信线路的更多相关文章

  1. cogs——7. 通信线路

    7. 通信线路 ★★   输入文件:mcst.in   输出文件:mcst.out   简单对比时间限制:1.5 s   内存限制:128 MB 问题描述 假设要在n个城市之间建立通信联络网,则连通n ...

  2. cogs 7. 通信线路

    7. 通信线路 ★★   输入文件:mcst.in   输出文件:mcst.out   简单对比时间限制:1.5 s   内存限制:128 MB 问题描述 假设要在n个城市之间建立通信联络网,则连通n ...

  3. 【COGS 254】【POI 2001】交通网络图

    http://www.cogs.top/cogs/problem/problem.php?pid=254 dist[i]表示能最早到达i点的时间.这样就可以用最短路模型来转移了. #include&l ...

  4. 【COGS】894. 追查坏牛奶

    http://cojs.tk/cogs/problem/problem.php?pid=894 题意:n个点m条边的加权网络,求最少边数的按编号字典序最小的最小割.(n<=32, m<=1 ...

  5. 【COGS】147. [USACO Jan08] 架设电话线(二分+spfa)

    http://cojs.tk/cogs/problem/problem.php?pid=147 学到新姿势了orz 这题求的是一条1-n的路径的最大路径最小. 当然是在k以外的. 我们可以转换一下. ...

  6. 【COGS & USACO Training】710. 命名那个数字(hash+水题+dfs)

    http://cojs.tk/cogs/problem/problem.php?pid=710 近日开始刷水... 此题我为了练一下hash...但是hash跑得比暴力还慢.. 不言而喻... #in ...

  7. 【COGS & USACO】896. 圈奶牛(凸包)

    http://cojs.tk/cogs/problem/problem.php?pid=896 我的计算几何入门题... 看了看白书的计算几何部分,,恩好嘛.. 乃们都用向量!!!! 干嘛非要将2个点 ...

  8. 【COGS】714. USACO 1.3.2混合牛奶(贪心+水题)

    http://cojs.tk/cogs/problem/problem.php?pid=714 在hzwer的刷题记录上,默默地先跳过2题T_T...求凸包和期望的..T_T那是个啥..得好好学习 看 ...

  9. Cogs 97. [NOIP2007] 树网的核 Floyd

    题目: http://cojs.tk/cogs/problem/problem.php?pid=97 97. [NOIP2007] 树网的核 ★☆   输入文件:core.in   输出文件:core ...

随机推荐

  1. CodeForces 550B Preparing Olympiad(DFS回溯+暴力枚举)

    [题目链接]:click here~~ [题目大意] 一组题目的数目(n<=15),每一个题目有对应的难度,问你选择一定的题目(大于r个且小于l个)且选择后的题目里最小难度与最大难度差不小于x, ...

  2. Fragment使用的正确姿势

     网上关于Fragment的使用建议和分析非常多,可是依旧会有非常多人在使用Fragment的时候出现各种奇葩错误 这篇文章我分享一下各种项目中解决Fragment的各种注意事项(不做原理分析), ...

  3. ELK搭建(filebeat、elasticsearch、logstash、kibana)

    ELK部署(文章有点儿长,搭建时请到官网将tar包下载好,按步骤可以完成搭建使用) ELK指的是ElasticSearch.LogStash.Kibana三个开源工具 LogStash是负责数据的收集 ...

  4. [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 ...

  5. 安装meteor运行基本demo发生错误。

    bogon:~ paul$ curl https://install.meteor.com/ | sh % Total % Received % Xferd Average Speed Time Ti ...

  6. javascript 优秀写法

    http://www.csdn.net/article/2014-01-06/2818025-Useful-JavaScript-Tips-Best-Practices

  7. Mojo For Chromium Developers1

    Mojo For Chromium Developers Overview This document contains the minimum amount of information neede ...

  8. Mojo C++ Platform API

    Mojo C++ Platform API This document is a subset of the Mojo documentation. Contents Overview Platfor ...

  9. 列表的所有的input,将它的值以键值对的形式存放到一个数组里

    要求的格式 代码块 $('.btn-confirm').on('tap',function(){ var arr={}; var name = $("input[name='insuranc ...

  10. vue项目的一些最佳实践提炼和经验总结

    项目组织结构 ajax数据请求的封装和api接口的模块化管理 第三方库按需加载 利用less的深度选择器优雅覆盖当前页面UI库组件的样式 webpack实时打包进度 vue组件中选项的顺序 路由的懒加 ...