codevs——1003——电话连线
一个国家有n个城市。若干个城市之间有电话线连接,现在要增加m条电话线(电话线当然是双向的了),使得任意两个城市之间都直接或间接经过其他城市有电话线连接,你的程序应该能够找出最小费用及其一种连接方案。

输入文件的第一行是n的值(n<=100).
第二行至第n+1行是一个n*n的矩阵,第i行第j列的数如果为0表示城市i与城市j有电话线连接,否则为这两个城市之间的连接费用(范围不超过10000)。
输出文件的第一行为你连接的电话线总数m,第二行至第m+1行为你连接的每条电话线,格式为i j,(i<j), i j是电话线连接的两个城市。输出请按照Prim算法发现每一条边的顺序输出,起始点为1.
第m+2行是连接这些电话线的总费用。
5
0 15 27 6 0
15 0 33 19 11
27 33 0 0 17
6 19 0 0 9
0 11 17 9 0
2
1 4
2 5
17
n<=100
毕竟是模板,所以必须要保存下。
至于题解,看程序就可以懂啦O(∩_∩)O哈哈~
//Asimple
//#include <bits/stdc++.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <limits.h>
#include <time.h>
#define INF 0xfffffff
#define mod 999101
#define PI 3.14159265358979323
#define swap(a,b,t) t = a, a = b, b = t
#define CLS(a, v) memset(a, v, sizeof(a))
#define debug(a) cout << #a << " = " << a <<endl
#define dobug(a, b) cout << #a << " = " << a << " " << #b << " = " << b << endl
#define srd(a) scanf("%d", &a)
#define src(a) scanf("%c", &a)
#define srs(a) scanf("%s", a)
#define srdd(a,b) scanf("%d %d",&a, &b)
#define srddd(a,b,c) scanf("%d %d %d",&a, &b, &c)
#define prd(a) printf("%d\n", a)
#define prdd(a,b) printf("%d %d\n",a, b)
#define prs(a) printf("%s\n", a)
#define prc(a) printf("%c", a)
using namespace std;
inline int abs(int x){return x<?-x:x;}
typedef long long ll;
const int maxn = ;
int n, m, num, T, k, len, ans, sum, x, y;
int Map[maxn][maxn];
bool vis[maxn];int arr[maxn][];
void solve() {
int Min, sum = ;
int adv[maxn];
int lowc[maxn];
CLS(vis, false); for(int i=; i<=n; i++) lowc[i] = INF;
lowc[] = ; for(int l=; l<=n; l++) {
Min = INF;
int index = ;
for(int j=; j<=n; j++) {
if( !vis[j] && lowc[j]<Min ) {
Min = lowc[j];
index = j;
}
}
sum += Map[adv[index]][index];
vis[index] = true;
for(int i=; i<=n; i++) {
if( !vis[i] && Map[index][i]<lowc[i]) {
lowc[i] = Map[index][i];
adv[i] = index;
}
}
if( Min ) {
arr[len][] = min(adv[index], index);
arr[len][] = max(adv[index], index);
len++;
}
}
cout << len << endl;
for(int i=; i<len; i++) {
cout << arr[i][] << " " << arr[i][] << endl;
}
cout << sum << endl;
} void input() {
srd(n);
for(int i=; i<=n; i++) {
for(int j=; j<=n; j++) {
srd(Map[i][j]);
}
}
solve();
} int main(){
input();
return ;
}
codevs——1003——电话连线的更多相关文章
- Codevs 1003 电话连线
时间限制: 1 s 空间限制: 128000 K 题目等级 : 黄金 Gold 题目描述 Description 一个国家有n个城市.若干个城市之间有电话线连接,现在要增加m条电话线(电话线当 ...
- 电话连线(codevs 1003)
题目描述 Description 一个国家有n个城市.若干个城市之间有电话线连接,现在要增加m条电话线(电话线当然是双向的了),使得任意两个城市之间都直接或间接经过其他城市有电话线连接,你的程序应该能 ...
- 【wikioi】1003 电话连线
题目链接 算法: 最小生成树 PS:被卡过2天(中间的时间没去做).日期:2013-09-13 13:49:47 ~ 2013-09-17 13:01:07 此题为基础题 刚开始学图论时只会用Krus ...
- 1、Codevs 必做:2833、1002、1003、2627、2599
2833 奇怪的梦境 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description Aiden陷入了一个奇怪的梦境:他被困 ...
- T1003 电话连线 codevs
http://codevs.cn/problem/1003/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 一个国家有n个城市 ...
- 【CodeVS 1288】埃及分数
http://codevs.cn/problem/1288/ loli秘制面向高一的搜索,好难啊QAQ 我本来想按照分母从大到小搜,因为这样分母从小到大枚举到的第一个可行方案就是最优方案. 但貌似会T ...
- 【codevs】刷题记录→_→(推荐看!)
注:本文是我原先在csdn内写的一篇博文,现转到这里,两篇博文尽量同时更新. //#include<iostream->shuati> //define 为什么刷 学长☞hzwer ...
- 【CodeVS 1198】【NOIP 2012】国王游戏
http://codevs.cn/problem/1198/ 推导一翻,排好序后,直接上高精度. #include<cstdio> #include<cstring> #inc ...
- 【CodeVS 3290】【NOIP 2013】华容道
http://codevs.cn/problem/3290/ 据说2013年的noip非常难,但Purpleslz学长还是AK了.能A掉这道题真心orz. 设状态$(i,j,k)$表示目标棋子在$(i ...
随机推荐
- php使用solr全文搜索引擎
前言 本来以为网上已经有了类似博文,不想重复,可是一圈搜下来,都是一些内容不甚明了的文章,或者solr版本太过老,参考价值不高,更有甚者,直接拷贝的别人的内容.一篇博客,各大平台都能看到,也不见转载链 ...
- Linux文本编辑器之vim
VIM 文本编辑器 vim/vi是Unix/Linux上最常用的文本编辑器而且功能强大.注意:只有命令,没有菜单 VIM工作模式 命令模式:又称为一般模式 编辑模式:又称为低行模式,命令行模式 插入命 ...
- keras学习简单线性回归【1】
转自:https://morvanzhou.github.io/tutorials/machine-learning/keras/2-1-regressor/ 总的代码的过程就是: 1.导入模块+创建 ...
- 【LeetCode每天一题】Reverse String
Write a function that reverses a string. The input string is given as an array of characters char[]. ...
- Let Encrypt延期(转自虞大胆的叽叽喳喳)
前几天发现我的 letsencrypt 通配符证书快过期了,想为这两张证书续期(renew). 首先运行命令查看我的所有证书: $ certbot-auto certificates 其中证书名 si ...
- 引:Jmeter添加变量的四种方法
一.在样本中添加同请求一起发送的参数.根据服务器设置的数据类型,来添加不同类型的参数 二.用户定义的变量 1.创建:添加->配置元件->用户定义的变量 2.作用:当前的线程组内所有Samp ...
- 遇到问题---hosts不起作用问题的解决方法
c:\WINDOWS\system32\drivers\etc\hosts 文件的作用是添加 域名解析 定向 比如添加 127.0.0.1 www.baidu.com 那我们访问www.baidu. ...
- sklearn_线性回归
1. 普通线性回归 Linear Regression (1)目标: class sklearn.linear_model.LinearRegression (fit_intercept=True, n ...
- Cocos Creator 获得设备分辨率
var b = cc.director.getWinSizeInPixels() var bx = b.width var by = b.height
- gitlab4.0_工程提交
一,环境 gitlab linux系统 IP :10.2.177.31 ==>(我已经申请了一个账户 A@A) 客户端 windows系统 IP:10.2.256. ...