题目描述 Description

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

输入描述 Input Description

输入文件的第一行是n的值(n<=100).

第二行至第n+1行是一个n*n的矩阵,第i行第j列的数如果为0表示城市i与城市j有电话线连接,否则为这两个城市之间的连接费用(范围不超过10000)。

输出描述 Output Description

输出文件的第一行为你连接的电话线总数m,第二行至第m+1行为你连接的每条电话线,格式为i j,(i<j), i j是电话线连接的两个城市。输出请按照Prim算法发现每一条边的顺序输出,起始点为1.

第m+2行是连接这些电话线的总费用。

样例输入 Sample Input

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

样例输出 Sample Output

2

1 4

2 5

17

数据范围及提示 Data Size & Hint

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——电话连线的更多相关文章

  1. Codevs 1003 电话连线

    时间限制: 1 s   空间限制: 128000 K   题目等级 : 黄金 Gold 题目描述 Description 一个国家有n个城市.若干个城市之间有电话线连接,现在要增加m条电话线(电话线当 ...

  2. 电话连线(codevs 1003)

    题目描述 Description 一个国家有n个城市.若干个城市之间有电话线连接,现在要增加m条电话线(电话线当然是双向的了),使得任意两个城市之间都直接或间接经过其他城市有电话线连接,你的程序应该能 ...

  3. 【wikioi】1003 电话连线

    题目链接 算法: 最小生成树 PS:被卡过2天(中间的时间没去做).日期:2013-09-13 13:49:47 ~ 2013-09-17 13:01:07 此题为基础题 刚开始学图论时只会用Krus ...

  4. 1、Codevs 必做:2833、1002、1003、2627、2599

    2833 奇怪的梦境  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description Aiden陷入了一个奇怪的梦境:他被困 ...

  5. T1003 电话连线 codevs

    http://codevs.cn/problem/1003/ 时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 一个国家有n个城市 ...

  6. 【CodeVS 1288】埃及分数

    http://codevs.cn/problem/1288/ loli秘制面向高一的搜索,好难啊QAQ 我本来想按照分母从大到小搜,因为这样分母从小到大枚举到的第一个可行方案就是最优方案. 但貌似会T ...

  7. 【codevs】刷题记录→_→(推荐看!)

    注:本文是我原先在csdn内写的一篇博文,现转到这里,两篇博文尽量同时更新. //#include<iostream->shuati> //define 为什么刷  学长☞hzwer ...

  8. 【CodeVS 1198】【NOIP 2012】国王游戏

    http://codevs.cn/problem/1198/ 推导一翻,排好序后,直接上高精度. #include<cstdio> #include<cstring> #inc ...

  9. 【CodeVS 3290】【NOIP 2013】华容道

    http://codevs.cn/problem/3290/ 据说2013年的noip非常难,但Purpleslz学长还是AK了.能A掉这道题真心orz. 设状态$(i,j,k)$表示目标棋子在$(i ...

随机推荐

  1. ORM之视图层

    1.request对象 前台POST传来的数据,包装到POST字典中request.POST 前台浏览器窗口携带的数据,包装到GET字典中request.GET 前台请求的方式,request.met ...

  2. 解决ScrollView中包含ListView,导致ListView显示不全

    ScrollView 中包含 ListView 的问题 : ScrollView和ListView会冲突,会导致ListView显示不全 <?xml version="1.0" ...

  3. Linux修改IP网络配置

    文件路径:/etc/sysconfig/networe-scripts/ifcfg-eth0 BOOTPROTO="static" IPADDR=IP地址 NETMASK=掩码 G ...

  4. composer install 遭遇404错误

    [Composer\Downloader\TransportException] The "https://packagist.phpcomposer.com/p/provider-2019 ...

  5. 超参数调试、Batch正则化和编程框架

    1.调试处理 2.为超参数选择合适的范围 3.超参数在实践中调整:熊猫与鱼子酱 4.正则化网络的激活函数 5.将batch norm拟合进神经网络 6. 为什么Batch Norm会起作用? 7.测试 ...

  6. iOS UI布局-定时器

    定时器是比较常用的一个UI控件,在付款.抢购时,经常会使用到.提取成一个通用的方法 /** * 倒计时GCD通用方法 * 通常用的计时器都是用NSTimer,但是NSTimer在线程很吃紧的时候效果不 ...

  7. HDU 2842 Chinese Rings(常数矩阵)

    Chinese Rings 转载自:点这里 [题目链接]Chinese Rings [题目类型]常数矩阵 &题意: 一种中国环,解开第k个环需要先解开全部的前(k-2)个环,并留有第(k-1) ...

  8. MyBatis基础入门《十》添加数据

    MyBatis基础入门<十>添加数据 描述: 修改了实体类:TblClient.java,将其字段:cbirthday 由String类型改成了Date类型. TblClient.java ...

  9. sql 一列拼接成一行,再分割成列

    原始数据,需要拼接成一行 select * from (select d.*,(SELECT count ([Keyword])FROM [DragonGuoShi].[dbo].[ArticleIn ...

  10. spark2.2.1 sql001

    package sql import org.apache.spark.sql.SparkSession import org.apache.spark.SparkContext object Par ...