#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define M 25000
#define maxn 100000000
struct node
{
int v1,v2;
int dis;
}s[M];
int cmp(const node a,const node b)
{
if(a.dis<b.dis)
return 1;
else
return 0;
}
int father[M];
int main()
{
int n;
while(scanf("%d",&n)>0)
{
//int m=(n*(n-1))/2;
int cnt=0;
for(int i=0;i<=n;i++)
father[i]=i;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
int tmp;
scanf("%d",&tmp);
if(i==j)
{
s[cnt].v1=i;
s[cnt].v2=j;
s[cnt++].dis=maxn;
}
else
{
s[cnt].v1=i;
s[cnt].v2=j;
s[cnt++].dis=tmp;
}
}
int q;
scanf("%d",&q);
while(q--)
{
int tmp,tmp1;
scanf("%d%d",&tmp,&tmp1);
s[cnt].v1=tmp;
s[cnt].v2=tmp1;
s[cnt++].dis=0;
}
sort(s,s+cnt,cmp);
int j=0,ans=0;
while(j<cnt)
{
int tmp=s[j].v1;
int tmp1=s[j].v2;
int s1=father[tmp];
int s2=father[tmp1];
if(s1!=s2)
{
ans+=s[j].dis;
for(int i=0;i<=n;i++)
if(s2==father[i])
father[i]=s1;
}
j++;
}
printf("%d\n",ans);
}
return 0;
}

hdu1102(最小生成树水题)的更多相关文章

  1. POJ-1861,Network,最小生成树水题,,注意题面输出有问题,不必理会~~

    Network Time Limit: 1000MS   Memory Limit: 30000K          Special Judge http://poj.org/problem?id=1 ...

  2. [BZOJ1601][Usaco2008 Oct]灌水 最小生成树水题

    1601: [Usaco2008 Oct]灌水 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 2121  Solved: 1393[Submit][St ...

  3. 最短路&&最小生成树水题

    训练赛20151122 5:00:00     Overview Problem Status Rank Discuss Current Time: 2015-11-23 17:33:18 Conte ...

  4. POJ2485Highways(prime 水题)

    Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26516   Accepted: 12136 Descri ...

  5. BZOJ USACO 银组 水题集锦

    最近刷银组刷得好欢快,好像都是水题,在这里吧他们都记录一下吧(都是水题大家一定是道道都虐的把= =)几道比较神奇的题到时再列出来单独讲一下吧= =(其实我会说是BZOJ蹦了无聊再来写的么 = =) [ ...

  6. POJ 水题(刷题)进阶

    转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...

  7. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  8. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  9. ACM :漫漫上学路 -DP -水题

    CSU 1772 漫漫上学路 Time Limit: 1000MS   Memory Limit: 131072KB   64bit IO Format: %lld & %llu Submit ...

随机推荐

  1. IOC 之深入理解 Spring IoC

    在一开始学习 Spring 的时候,我们就接触 IoC 了,作为 Spring 第一个最核心的概念,我们在解读它源码之前一定需要对其有深入的认识,本篇为[死磕 Spring]系列博客的第一篇博文,主要 ...

  2. 系统服务中没有Windows Installer服务怎么办

    在安装软件时,发现安装不了,提示没有Windows Installer服务,到系统服务中一看,果真没有这一项,这是什么问题呢? 出现这种情况,多为与Windows Installer服务相关的文件丢失 ...

  3. 【Oracle】详解ORACLE中的trigger(触发器)

    本篇主要内容如下: 8.1 触发器类型 8.1.1 DML触发器 8.1.2 替代触发器 8.1.3 系统触发器 8.2 创建触发器 8.2.1 触发器触发次序 8.2.2 创建DML触发器 8.2. ...

  4. 【MySQL】MySQL的常规操作

    MySQL的常规知识 标准的SQL语句通常可分为如下的几种类型: 1,DCL(Database Control Language) :数据控制语言,主要由grant和revoke关键字组成. 2.DD ...

  5. Linux下面安装RabbitMQ Cluster

    安装rabbitmq cluster: 设置 Erlang Cookie安装完RabbitMQ之后,在第一台机器上面启动RabbitMQ,然后在停止.复制node1上的/var/lib/rabbitm ...

  6. mybatis自定义插件动态修改sql语句

    step1:定义Interceptor实现org.apache.ibatis.plugin.Interceptor import org.apache.commons.logging.Log; imp ...

  7. IntelliJ IDEA 缺少 javax 包 支持

    在初次使用 IntelliJ IDEA 中,当你使用javax.servlet包下的类时(例:javax.servlet.http.HttpServlet), 在你会发现在IntelliJ IDEA里 ...

  8. block(九)Block 和 Delegate 的使用比较

    Block 和 Delegate中的方法都可以理解成回调函数,当某件事情发生的时候取执行一段代码片段 Block(代码块) 优点:是一种轻量级的回调,能够直接访问上下文,使用块的地方和块的实现地方在同 ...

  9. 日志收集之--将Kafka数据导入elasticsearch

    最近需要搭建一套日志监控平台,结合系统本身的特性总结一句话也就是:需要将Kafka中的数据导入到elasticsearch中.那么如何将Kafka中的数据导入到elasticsearch中去呢,总结起 ...

  10. mvc 文件压缩 减少文件大小

    using System; using System.Collections.Generic; using System.IO.Compression; using System.Linq; usin ...