#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. SpringCloud五大神兽之Eureka

    注册中心概述 什么是注册中心? 相当于服务之间的'通讯录',记录了服务和服务地址之间的映射关系.在分布式架构中服务会注册到这里.当服务需要调用其他服务时,就在注册中心找到其他服务的地址,进行调用 注册 ...

  2. 安装symfony3.4的坑,也是PHP7.3的经典坑之解决办法

    对于刚入手symfony3.4的同学,肯定会发现,安装symfony后部署后看到的往往不是hello world,也不是symfony的欢迎页面,而是给你一个下马威,唉,给你来个bug开开胃. 当然这 ...

  3. python学习(9)字典的基本应用

    字典是一种通过名字或者关键字引用的得数据结构,其键可以是数字.字符串.元组,这种结构类型也称之为映射.字典类型是Python中唯一內建的映射类型.字典可以理解为列表的升级版. dict是无序的 key ...

  4. 使用requests、re、BeautifulSoup、线程池爬取携程酒店信息并保存到Excel中

    import requests import json import re import csv import threadpool import time, random from bs4 impo ...

  5. js垃圾回收与内存泄漏

    js垃圾回收机制 概念: javascript具有自动垃圾收集机制,也就是说,执行环境会负责管理代码执行过程中的使用的内存.而在C和C++之类的语言中,开发人员的一项基本任务就是手动跟踪内存的使用情况 ...

  6. 小浩算法|一文让你学会如何用代码判断"24"点

    “24点”是一种数学游戏,正如象棋.围棋一样是一种人们喜闻乐见的娱乐活动.它始于何年何月已无从考究,但它以自己独具的数学魅力和丰富的内涵正逐渐被越来越多的人们所接受.今天就为大家分享一道关于“24点” ...

  7. Java日志介绍(3)-Logback

    Logback 继承自Log4j,它建立在有十年工业经验的日志系统之上.它比其它所有的日志系统更快并且更小,包含了许多独特并且有用的特性. 1.配置 1.1.加载配置 Logback能够在初始化期间自 ...

  8. SpringCloud踩坑

    今天在使用 SpringCloud 时遇到了一个问题,感觉有不少小伙伴会遇到,所以记录下来 版本说明 SpringBoot 2.2.4.RELEASE SpringCloud Greenwich.SR ...

  9. cra

    const paths = require('react-scripts/config/paths'); paths.appBuild = path.join(path.dirname(paths.a ...

  10. 转:静态链表及其创建(C语言实现)

    http://c.biancheng.net/view/3339.html 折半插入排序算法(C语言代码实现) http://c.biancheng.net/view/3440.html 二叉树层次遍 ...