环绕岛屿Surround the Islands

题目描述

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

说明/提示

1 10 4

xxxxxxx x

xxxxxxxxx xxxx

7 xxxxxxxxxxx 6 xxxxxxx

xxxxxxxxxxx 11 xxxxxxxxxx 5

xxxxxxx

xxx 3 12 xxxxxxx 2

xxxxxxxx

xxxxxxxx

xxxxxxxxx

xxxxxxxxx

xxxxxxxxxx

xxxxxxxxxx

8 xxxxxxxxxx 9

The example describes three islands: {1,7,3,6,10}, {4,5,11} and {2,9,8,12}. The travel costs are provided as a matrix. For example, the travel cost from vertex 1 to 2 is 15.

There is more than one solution. One is: FJ starts from vertex 3 then 7 and stops at 1, travels to 11 followed by 4,5,11. He then returns back to 1, and travels to 12 followed by 2,9,8,12. Finally, he returns back to 1 and continues with 10,6,3,7. The costs are 8 * 2 = 16 for traveling from 1 to 11 and returning back, and 7 * 2 = 14 for traveling from 1 to 12 and back -- a total cost of 30.

arjan缩点,再在所有强连通分量中找一条最小的边作为强连通分量的边,因为还要回来,所以Ans最后要乘二

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
#define regi register 

int n;

int fa[505];

inline int Find(int x)
{
    return x == fa[x] ? x : fa[x] = Find(fa[x]);
}

int dis[505][505];

int cnt;

int ans = 0x7f7f7f7f;

int pos[505];

int main(){
    cin >> n;
    for (regi int i = 1 ; i <= n ; i ++) fa[i] = i;
    memset(dis, 0x7f, sizeof dis);
    for (regi int i = 1 ; 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 = 1 ; i <= n ; i ++){
        if (fa[i] == i) pos[++cnt] = i;
    }
    for (regi int i = 1 ; i <= n ; i ++){
        int fi = Find(i);
        for (regi int j = 1 ; j <= n ; j ++){
            int fj = Find(j);
            int d;
            scanf("%d", &d);
            dis[fi][fj] = min(dis[fi][fj], d);
        }
    }
    for (regi int i = 1 ; i <= cnt ; i ++){
        int sum = 0;
        for (regi int j = 1 ; j <= cnt ; j ++){
            if (i == j) continue;
            sum += dis[pos[i]][pos[j]];
        }
        ans = min(ans, sum);
    }
    cout << ans * 2 << endl;
    return 0;
}

原文地址:https://www.cnblogs.com/hrj1/p/11156524.html

时间: 2024-10-14 10:13:32

环绕岛屿Surround the Islands的相关文章

[USACO09FEB]环绕岛屿Surround the Islands

[Time Gate] https://www.luogu.org/problemnew/show/P2941 [解题思路] Tarjan缩点,再在所有强连通分量中找一条最小的边作为强连通分量的边,因为还要回来,所以Ans最后要乘二 [code] 1 #include<bits/stdc++.h> 2 using namespace std; 3 inline int read() 4 { 5 int x=0; 6 char c=getchar(); 7 bool flag=0; 8 whil

BZOJ3397: [Usaco2009 Feb]Surround the Islands 环岛篱笆

3397: [Usaco2009 Feb]Surround the Islands 环岛篱笆 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 11  Solved: 7[Submit][Status] Description 约翰在加勒比海买下地产,准备在这里的若干个岛屿上养奶牛.所以,他要给所有岛屿围上篱笆.每个岛屿都是多边形.他沿着岛屿的一条边界朝一个方向走,有时候坐船到另一个岛去.他可以从任意一个多边形顶点开始修篱笆的工作:在任意一个到达的顶点

[Usaco2009 Feb]Surround the Islands 环岛篱笆

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

国家二字码对照表

中文国家名 英文国家名 大洲 二位简码 法属南部领地 French Southern Territories Europe TF 夏威夷 Hawaii North America HW 阿拉斯加 Alaska North America AK 新西兰属土岛屿 New Zealand Islands Territories Oceania XL 加罗林群岛 Caroline Islands Asia XK 巴利阿里群岛 Balearic Islands Europe XJ 马德拉群岛 Madeir

游戏中常见(或必须)的42种要素

主要概括为以下几种 人物特效 光特效 粒子特效 物理效果 声音特效 天气特效 1. 水体效果 water effects 2. 血液消失的效果disappearing blood effects 3. 霓虹灯样的粒子系统neon particle system 4. 卡通着色cel-shading 5. 反射reflection 6. 能在头盔前的玻璃中看到自己的反射visor reflection 7. 环绕立体声surround sound 8. 立体3Dstereoscopic 3D 9.

[LeetCode] Number of Islands II 岛屿的数量之二

A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands after each addLand o

[LeetCode] 200. Number of Islands 岛屿的数量

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by

LeetCode | 0200. Number of Islands岛屿数量【Python】

LeetCode 0200. Number of Islands岛屿数量[Medium][Python][DFS] Problem LeetCode Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or v

lintcode 容易题:Number of Islands 岛屿的个数

题目: 岛屿的个数 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [ [1, 1, 0, 0, 0], [0, 1, 0, 0, 1], [0, 0, 0, 1, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 1] ] 中有 3 个岛. 解题: 在网上看到是根据深度优先算法,和递归思想进行解决的题目,当发现这一点是1的时候,将周围的点都设置成 0,如何设置成 0?第一步:将当前