cogs 7. 通信线路
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<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 2325750
#define M 2510
using namespace std;
int n,tot,fa[M],num,ans;
struct nond{
int x,y,z;
}edge[MAXN];
int cmp(nond a,nond b){
return a.z<b.z;
}
int find(int x){
if(fa[x]==x) return fa[x];
else return fa[x]=find(fa[x]);
}
int main(){
freopen("mcst.in","r",stdin);
freopen("mcst.out","w",stdout);
scanf("%d",&n);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
int x;
scanf("%d",&x);
if(x!=-){
edge[++tot].x=i;
edge[tot].y=j;
edge[tot].z=x;
}
}
sort(edge+,edge++tot,cmp);
for(int i=;i<=n;i++) fa[i]=i;
for(int i=;i<=tot;i++){
int dx=find(edge[i].x);
int dy=find(edge[i].y);
if(dx==dy) continue;
fa[dx]=dy;
num++;
ans+=edge[i].z;
if(num==n-) break;
}
printf("%d",ans);
}
cogs 7. 通信线路的更多相关文章
- cogs——7. 通信线路
7. 通信线路 ★★ 输入文件:mcst.in 输出文件:mcst.out 简单对比时间限制:1.5 s 内存限制:128 MB 问题描述 假设要在n个城市之间建立通信联络网,则连通n ...
- COGS——T 7. 通信线路
http://www.cogs.pro/cogs/problem/problem.php?pid=7 ★★ 输入文件:mcst.in 输出文件:mcst.out 简单对比时间限制:1.5 ...
- 【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 ...
随机推荐
- css bug(ie6兼容问题)
二.五大浏览器内核1.trident(MSHTML)(三叉戟:三叉线,三齿鱼叉) Gecko (壁虎) presto(迅速的) webkit(safari内核,Chrome内核原型,他是苹果公司自己的 ...
- nodejs操作mysql
var mysql = require('mysql');var pool = mysql.createPool({ host: 'localhost', user: 'root', password ...
- input如何去掉边框
outline: none; border:solid 0px; 两个属性,ok.
- SQL SERVER 获取给定时间段内的所有日期列表
declare @StartDate DATETIME = '2018/08/01'declare @EndDate DATETIME ='2018/09/27'SELECT CONVERT (VAR ...
- ACM_题目这么难,来局愉快的昆特牌吧
题目这么难,来局愉快的昆特牌吧 Time Limit: 2000/1000ms (Java/Others) Problem Description: 小Z打比赛,然而比赛太难了,他坐在电脑面前被题淹没 ...
- NHibernate学习(零)-本次学习遇到的错误汇总
问题一: "System.TypeInitializationException"类型的未经处理的异常在 KimismeDemo.exe 中发生 其他信息: "NHibe ...
- Struts之 拦截器配置 ( Interceptor )
struts2体系结构的核心就是拦截器. 以链式执行,对真正要执行的方法(execute())进行拦截.首先执行Action配置的拦截器,在Action和Result执行之后,拦截器再一次执行(与先前 ...
- Java—RequestMapping相关用法
RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径.它有6个属性:1.value:指定请求的具体地址:valu ...
- 01--TCP状态转换
参考大牛文章: http://www.cnblogs.com/qlee/archive/2011/07/12/2104089.html
- wpf绑定静态变量,模拟rem单位
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; usin ...