题目描述

Farmer John has bought property in the Caribbean and is going to try to raise dairy cows on a big farm composed of islands. Set in his ways, he wants to surround all the islands with fence.

Each island in the farm has the shape of a polygon. He fences the islands one side at a time (between a consecutive pair of vertices) and proceeds clockwise around a given island with his fencing

operations. Since he wants to fence all the islands, he must at some point travel to any other islands using a boat.

He can start fencing at any vertex and, at any vertex he encounters, travel to some vertex on another island, fence all the way around it, and then IMMEDIATELY return back to the same vertex on the original island using the same path he traveled before. Each boat trip has a cost defined by a supplied matrix.

The islands are described by a set of N (3 <= N <= 500) pairs of vertices V1,V2 (1 <= V1 <= N; 1 <= V2 <= N) although you must figure out how to assemble them into islands. The vertices are conveniently numbered 1..N.

The cost of traveling by boat between each pair of vertices is given by a symmetric cost matrix whose elements fall in the range 0..1000.

What is the minimum cost of surrounding the islands with the fence?

约翰在加勒比海买下地产,准备在这里的若干个岛屿上养奶牛.所以,他要给所有岛屿围上篱笆.每个岛屿都是多边形.他沿着岛屿的一条边界朝一个方向走,有时候坐船到另一个岛去.他可以从任意一个多边形顶点开始修篱笆的工作;在任意一个到达的顶点,他可以坐船去另一个岛屿的某个顶点,但修完那个岛的篱笆,他必须马上原路返回这个出发的岛屿顶点.任意两个顶点间都有航线,每条航线都需要一定的费用.请帮约翰计算最少的费用,让他修完所有篱笆.

输入输出格式

输入格式:

* Line 1: A single integer: N

* Lines 2..N+1: Each line describes an island's border with two space-separated integers: V1 and V2

* Lines N+2..2*N+1: Line i-N-1 contains N integers that describe row i of the cost matrix: Row_i

输出格式:

* Line 1: A single integer that specifies the minimum cost of building the fence

输入输出样例

输入样例#1:

12
1 7
7 3
3 6
6 10
10 1
2 12
2 9
8 9
8 12
11 5
5 4
11 4
0 15 9 20 25 8 10 13 17 8 8 7
15 0 12 12 10 10 8 15 15 8 8 9
9 12 0 25 20 18 16 14 13 7 12 12
20 12 25 0 8 13 14 15 15 10 10 10
25 10 20 8 0 16 20 18 17 18 9 11
8 10 18 13 16 0 10 9 11 10 8 12
10 8 16 14 20 10 0 18 20 6 16 15
13 15 14 15 18 9 18 0 5 12 12 13
17 15 13 15 17 11 20 5 0 22 8 10
8 8 7 10 18 10 6 12 22 0 11 12
8 8 12 10 9 8 16 12 8 11 0 9
7 9 12 10 11 12 15 13 10 12 9 0
输出样例#1:

30 

提交地址 : Luogu2941Bzoj3397; (然而并没有bzoj权限,桑心)

样例解析 : 一看样例就不想做系列;并没有好好看样例;
大致看了一下分的岛屿

大概是这个样子的,然后每个点之间都有一些连线表示之间是通路,每条路都有一个权值; 分析 :
其实仔细想想,既然在每个岛屿中行走不算进总花费,那么可以进行一波缩点,我是用并查集维护,直接求出每个联通块;
这样,我们就把一堆点,抽象成了一堆互不联通的块;
这时再加边权,在每一个联通块内,任何 一个点向外的连线都可看做是这个联通块向外连的线, 这样, 我们把每一个联通块向外的边的权值最小记录下来,当做这个联通块向外的边;
所以,我们只要枚举从哪一个联通块开始出发,把这个联通块与其他联通块的边权和加起来取min再乘2就是答案; 代码奉上:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
#define regi register int n; int fa[]; inline int Find(int x)
{
return x == fa[x] ? x : fa[x] = Find(fa[x]);
} int dis[][]; int cnt; int ans = 0x7f7f7f7f; int pos[]; int main()
{
cin >> n; for (regi int i = ; i <= n ; i ++) fa[i] = i; memset(dis, 0x7f, sizeof dis); for (regi int i = ; i <= n ; i ++){
int x, y;
scanf("%d%d", &x, &y);
int fx = Find(x), fy = Find(y);
if (fx != fy) fa[fx] = fy;
} for (regi int i = ; i <= n ; i ++){
if (fa[i] == i) pos[++cnt] = i;
} for (regi int i = ; i <= n ; i ++){
int fi = Find(i);
for (regi int j = ; j <= n ; j ++){
int fj = Find(j);
int d;
scanf("%d", &d);
dis[fi][fj] = min(dis[fi][fj], d);
}
} for (regi int i = ; i <= cnt ; i ++){
int sum = ;
for (regi int j = ; j <= cnt ; j ++){
if (i == j) continue;
sum += dis[pos[i]][pos[j]];
}
ans = min(ans, sum);
} // printf("dis==%d\n", dis[4][10]); cout << ans * << endl; return ;
}

zZhBr

可以转载但请注明出处,谢!

USACO环绕岛屿Surround the Islands 并查集 枚举暴力的更多相关文章

  1. hust 1385 islands 并查集+搜索

    islands Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/problem/show/1385 Descri ...

  2. USACO 2009 Open Treasure Cave /// DFS||并查集 oj26215

    题目大意: 输入 p,n,t :p为地点数 判断 t 能否回到源点1 接下来n行 每行输入 a b c: a能到达b和c Sample Input 13 6 76 7 82 3 410 11 128 ...

  3. USACO 2020 OPEN Favorite Colors【并查集-启发式合并-思考】

    题目链接 题意简述 仰慕喜欢同色奶牛的奶牛喜欢同色 (禁止套娃 ,求一种方案,奶牛喜欢的颜色种数最多,多种方案求字典序最小. 题目解析 这道题我最先想到的居然是二分+并查集,我在想啥 咳咳 首先,考虑 ...

  4. poj2912(种类并查集+枚举)

    题目:http://poj.org/problem?id=2912 题意:n个人进行m轮剪刀石头布游戏(0<n<=500,0<=m<=2000),接下来m行形如x, y, ch ...

  5. 并查集+bfs+暴力滑窗 Codeforces Round #356 (Div. 2) E

    http://codeforces.com/contest/680/problem/E 题目大意:给你一个n*n的图,然后图上的 . (我们下面都叫做‘点’)表示可以走,X表示不能走,你有如下的操作, ...

  6. 2019.01.22 zoj3583 Simple Path(并查集+枚举)

    传送门 题意简述:给出一张图问不在从sss到ttt所有简单路径上的点数. 思路: 枚举删去每个点然后把整张图用并查集处理一下,同时不跟sss和ttt在同一个连通块的点就是满足要求的点(被删去的不算). ...

  7. 1050. [HAOI2006]旅行【并查集+枚举】

    Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点S和T,求 一条路径,使得路径上最 ...

  8. BZOJ 1050 旅行comf 并查集+枚举下界

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1050 题目大意: 给你一个无向图,N(N<=500)个顶点, M(M<=5 ...

  9. POJ - 2912 Rochambeau (带权并查集+枚举)

    题意:有N个人被分为了三组,其中有一个人是开了挂的.同组的人的关系是‘=’,不同组的人关系是‘<’或'>',但是开了挂的人可以给出自己和他人任意的关系.现在要根据M条关系找出这个开了挂的人 ...

随机推荐

  1. WInform中实现设置ZedGraph中曲线的X轴与Y轴的上限与下限

    场景 Winforn中设置ZedGraph曲线图的属性.坐标轴属性.刻度属性: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...

  2. POJ - 3984 迷宫问题 (搜索)

    Problem Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...

  3. 装系统------- 了解常用的启动方式以及如何进入bios

    1.从硬盘启动:这种方式提供了最简单的维护解决方案,其基本原理就是增加一个系统的开机启动项,每次开机的时候您都可以选择是进入本地系统还是进入PE. 安装程序并不将PE的启动项作为默认启动项,而是提供一 ...

  4. 16 (OC)* UIAnimation和CoreAnimation

    目录 一 Core Animation 二 核心动画 2.1 基础动画 2.2 关键帧动画 2.3 动画组 2.4 转场动画 2.5 逐帧动画 三 UIView动画封装 3.1 基础动画 3.2 弹簧 ...

  5. HTML文档简介

    HTML简介 HTML标签 html文档标签: html源代码就好像word文档,有特殊的语法结构定义自己的功能. html文档标签 html标签,其下由两个主要节点标签head.body. head ...

  6. Android Studio [ImageView/使用第三方库加载图片]

    ImageViewActivity.class package com.xdw.a122; import android.support.v7.app.AppCompatActivity; impor ...

  7. 2019-2020-1 20199303《Linux内核原理与分析》第三周作业

    操作系统是如何工作的 除了存储程序计算机和函数调用堆栈机制,还有一个非常基础的概念就是中断,这三个关键性的方法机制可以称作计算机的三个法宝:程序存储计算机.函数调用.中断 堆栈的作用:记录函数调用框架 ...

  8. phaser学习总结之Tween详解

    前言 在上一章phaser学习总结之phaser入门教程中,我们已经初步入门了phaser,并通过一个案例了解了phaser,现在我们需要对phaser中的对象进行讲解,本章需要讲解的是tween,即 ...

  9. 浅拷贝&深拷贝的对比

    js中两种数据类型 浅拷贝:拷贝就是拷贝指向对象的指针,意思就是说:拷贝出来的目标对象的指针和源对象的指针指向的内存空间是同一块空间,浅拷贝只是一种简单的拷贝,让几个对象公用一个内存,然而当内存销毁的 ...

  10. spring5 源码深度解析----- AOP代理的生成

    在获取了所有对应bean的增强后,便可以进行代理的创建了.回到AbstractAutoProxyCreator的wrapIfNecessary方法中,如下所示: protected static fi ...