#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
int p[N];
struct edge{
int a;
int b;
int w;
}e[N*N];
int map[N][N],flag[N][N],num,n;
bool cmp(edge a,edge b)
{
return a.w<b.w;
}
int find(int x)
{
if(p[x]!=x)
p[x]=find(p[x]);
return p[x];
}
int main()
{
while(cin>>n&&n)
{
memset(flag,,sizeof flag);
memset(map,,sizeof map);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
cin>>map[i][j];
int m,x,y;
cin>>m;
while(m--)
{
cin>>x>>y;
flag[x][y]=;
}
num=;
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
if(flag[i][j])
{
e[num].a=i;
e[num].b=j;
e[num++].w=;
}
else
{
e[num].a=i;
e[num].b=j;
e[num++].w=map[i][j];
}
sort(e,e+num,cmp);
for(int i=;i<=n;i++)
p[i]=i;
int sum=;
for(int i=;i<num;i++)
{
int a=find(e[i].a);
int b=find(e[i].b);
int w=e[i].w;
if(a!=b)
{
p[a]=b;
sum+=w;
}
}
cout<<sum<<endl;
}
return ;
}

Constructing Roads POJ - 2421 最小生成树板子题的更多相关文章

  1. Constructing Roads POJ - 2421 (最小生成树)

    思路:首先使用二维数组dis[][]处理输入, 对于已经修好的路,将其对应的dis[i][j]置为零即可.最后再将    所有的dis[][]保存到边结构体中,使用Kruskal算法求得最小生成树. ...

  2. Constructing Roads POJ - 2421

    题目链接:https://vjudge.net/problem/POJ-2421 思路:一些村庄,建一些路,使得所有村庄能相连,而且使得所有路长度之和最短. 题目说了,有些村庄之间已经建了路,说明有些 ...

  3. (最小生成树)Constructing Roads -- poj -- 2421

    链接: http://poj.org/problem?id=2421 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2113 ...

  4. Networking POJ - 1287 最小生成树板子题

    #include<iostream> #include<algorithm> using namespace std; const int N=1e5; struct edge ...

  5. HDU 1102(Constructing Roads)(最小生成树之prim算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...

  6. Constructing Roads(1102 最小生成树 prim)

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  8. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...

  9. POJ - 2421 Constructing Roads 【最小生成树Kruscal】

    Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...

随机推荐

  1. Chrome恢复显示网址 https:// 和 www

    文章来自我的博客: https://blog.ljyngup.com/archives/686.html/ Chrome这个新规弄得我也很蛋疼,每次一点输入框就突然突出来一下.后来在Hostloc论坛 ...

  2. Spring Boot 2.1.7 启动项目失败,报错: "Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured."

    一开始按照网上的很多解决办法是: 启动类头部声明@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}),但是这样会排除 ...

  3. ajax 解决中文乱码问题

    最近遇到了ajax 中文乱码的问题.下面总结一下 1. HTTP协议的编码规定 在HTTP协议中,浏览器不能向服务器直接传递某些特殊字符,必须是这些字符进行URL编码后再进行传送.url编码遵循的规则 ...

  4. 01-Maven

    今日知识 1. Maven 2. 依赖管理 2. 项目构建 Maven 1. Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. 2. Ma ...

  5. php 全局变量 预定义变量

    //$GLOBALS 引用全局作用域中可用的全部变量 $_SERVER['SERVER_ADDR'] 返回运行脚本所在服务器的IP地址 $_SERVER['SERVER_NAME'] 返回运行脚本所在 ...

  6. Linux相关知识笔记

    Quagga要在linux下编译并配置运行,所有,学习一点linux的基础知识. 安装的Ubuntu,用户名linux,密码1 使能Ubuntu的IP转发功能,需要修改etc/sysctl.conf和 ...

  7. k8s系列---stateful(有状态应用副本集)控制器

    http://blog.itpub.net/28916011/viewspace-2215046/ 在应用程序中,可以分为有状态应用和无状态应用. 无状态的应用更关注于群体,任何一个成员都可以被取代. ...

  8. Docker Compose搭建Redis一主二从三哨兵高可用集群

    一.Docker Compose介绍 https://docs.docker.com/compose/ Docker官方的网站是这样介绍Docker Compose的: Compose是用于定义和运行 ...

  9. 2000_narrowband to wideband conversion of speech using GMM based transformation

    论文地址:基于GMM的语音窄带到宽带转换 博客作者:凌逆战 博客地址:https://www.cnblogs.com/LXP-Never/p/12151027.html 摘要 在不改变现有通信网络的情 ...

  10. 前缀和&差分

    一:差分数组概念  一.差分数组的定义及用途 1.定义:对于已知有n个元素的数列d,建立记录它每项与前一项差值的差分数组f:显然,f[1]=d[1]-0=d[1];对于整数i∈[2,n],我们让f[i ...