Fence Loops题解

The fences that surround Farmer Brown's collection of pastures have gotten out of control. They are made up of straight segments from 1 through 200 feet long that join together only at their endpoints though sometimes
more than two fences join together at a given endpoint. The result is a web of fences enclosing his pastures. Farmer Brown wants to start to straighten things out. In particular, he wants to know which of the pastures has the smallest perimeter.

Farmer Brown has numbered his fence segments from 1 to N (N = the total number of segments). He knows the following about each fence segment:

  • the length of the segment
  • the segments which connect to it at one end
  • the segments which connect to it at the other end.

Happily, no fence connects to itself.

Given a list of fence segments that represents a set of surrounded pastures, write a program to compute the smallest perimeter of any pasture. As an example, consider a pasture arrangement, with fences numbered
1 to 10 that looks like this one (the numbers are fence ID numbers):

           1
   +---------------+
   |\             /|
  2| \7          / |
   |  \         /  |
   +---+       /   |6
   | 8  \     /10  |
  3|     \9  /     |
   |      \ /      |
   +-------+-------+
       4       5

The pasture with the smallest perimeter is the one that is enclosed by fence segments 2, 7, and 8.

PROGRAM NAME: fence6

INPUT FORMAT

Line 1: N (1 <= N <= 100)
Line 2..3*N+1:

N sets of three line records:

  • The first line of each record contains four integers: s, the segment number (1 <= s <= N); Ls, the length of the segment (1 <= Ls <= 255); N1s (1 <= N1s <= 8) the number of items on the subsequent line; and N2sthe
    number of items on the line after that (1 <= N2s <= 8).
  • The second line of the record contains N1 integers, each representing a connected line segment on one end of the fence.
  • The third line of the record contains N2 integers, each representing a connected line segment on the other end of the fence.

OUTPUT FORMAT

The output file should contain a single line with a single integer that represents the shortest surrounded perimeter.

描述

农夫布朗的牧场上的篱笆已经失去控制了。它们分成了1~200英尺长的线段。只有在线段的端点处才能连接两个线段,有时给定的一个端点上会有两个以上的篱笆。结果篱笆形成了一张网分割了布朗的牧场。布朗想将牧场恢复原样,出于这个考虑,他首先得知道牧场上哪一块区域的周长最小。 布朗将他的每段篱笆从1到N进行了标号(N=线段的总数)。他知道每段篱笆有如下属性:

  • 该段篱笆的长度
  • 该段篱笆的一端所连接的另一段篱笆的标号
  • 该段篱笆的另一端所连接的另一段篱笆的标号

幸运的是,没有篱笆连接它自身。对于一组有关篱笆如何分割牧场的数据,写一个程序来计算出所有分割出的区域中最小的周长。

例如,标号1~10的篱笆由下图的形式组成(下面的数字是篱笆的标号):

         1
 +---------------+
 |\             /|
2| \7          / |
 |  \         /  |
 +---+       /   |6
 | 8  \     /10  |
3|     \9  /     |
 |      \ /      |
 +-------+-------+
     4       5

上图中周长最小的区域是由2,7,8号篱笆形成的。

[编辑]格式

PROGRAM NAME: fence6

INPUT FORMAT:

(file fence6.in)

第1行: N (1 <= N <= 100)

第2行到第3*N+1行: 每三行为一组,共N组信息:

每组信息的第1行有4个整数: s, 这段篱笆的标号(1 <= s <= N); Ls, 这段篱笆的长度 (1 <= Ls <= 255); N1s (1 <= N1s <= 8) 与本段篱笆的一端 所相邻的篱笆的数量; N2s与本段篱笆的另一端所相邻的篱笆的数量。 (1 <= N2s <= 8).

每组信息的的第2行有 N1s个整数, 分别描述与本段篱笆的一端所相邻的篱笆的标号。

每组信息的的第3行有N2s个整数, 分别描述与本段篱笆的另一端所相邻的篱笆的标号。

OUTPUT FORMAT:

(file fence6.out)

输出的内容为单独的一行,用一个整数来表示最小的周长。

[编辑]SAMPLE
INPUT

10
1 16 2 2
2 7
10 6
2 3 2 2
1 7
8 3
3 3 2 1
8 2
4
4 8 1 3
3
9 10 5
5 8 3 1
9 10 4
6
6 6 1 2
5
1 10
7 5 2 2
1 2
8 9
8 4 2 2
2 3
7 9
9 5 2 3
7 8
4 5 10
10 10 2 3
1 6
4 9 5

[编辑]SAMPLE
OUTPUT

12

-------------------------------------------------分割线-----------------------------------------------------------

这道题可以看出是求最小环。因为n的范围很小,可以采用如下方法:

①floyd求最小环(更推荐这个,因为代码简洁)

②用最短路的dijskra算法或是SPFA算法,每次删掉一条边求最短路,如果从左侧顶点到右侧定点仍存在最短路,那么加上这条边后,就是一个最小环了。同时更新答案。

但是我想说,这两种方法都不好O(∩_∩)O~~

因为读入的是边集而不是我们日常做的点集,所以在转化的过程中会比较麻烦。推荐用DFS直接0ms秒过。

代码:

/*
ID:juan1973
LANG:C++
PROG:fence6
*/

#include<stdio.h>
#include<cstring>
using namespace std;
const int maxn=101;
int num[maxn][2],map[maxn][2][10],f[maxn],n,i,ans,start,c,j;
bool flag[maxn];
int find(int a,int b)
{
  for (int i=1;i<=num[b][0];i++)
    if (map[b][0][i]==a) return 0;
  return 1;
}
void dfs(int k,int d,int s)
{
  if (s>ans) return;
  if (k==start&&s>0) {ans=s;return;}
  flag[k]=true;
  for (int i=1;i<=num[k][d];i++)
  {
    int go=map[k][d][i];
    if (!flag[go]||go==start) dfs(go,1-find(k,go),s+f[k]);
  }
  flag[k]=false;
}
int main()
{
  freopen("fence6.in","r",stdin);
  freopen("fence6.out","w",stdout);
  scanf("%ld",&n);
  for (i=1;i<=n;i++)
  {
    scanf("%ld",&c);
    scanf("%ld%ld%ld",&f[c],&num[c][0],&num[c][1]);
    for (j=1;j<=num[c][0];j++) scanf("%ld",&map[c][0][j]);
    for (j=1;j<=num[c][1];j++) scanf("%ld",&map[c][1][j]);
  }
  ans=9999999;
  for (start=1;start<=n;start++)
    {
      memset(flag,0,sizeof(flag));
      dfs(start,0,0);
    }
  printf("%ld\n",ans);
  return 0;
}
//果断放弃一下转化代码。  
/*for (i=1;i<=n;i++)

    scanf("%ld%ld%ld%ld",&p[i],&map_e[i],&map[0][p[i]][0],map[1][p[i]][0]);
    for (j=1;j<=map[0][p[i]][0];j++) scanf("%ld",&map[0][p[i]][j]);
    for (j=1;j<=map[1][p[i]][0];j++) scanf("%ld",&map[1][p[i]][j]);
  }
  flag[0][1]=true;now_e=1;now_v;cnt=1;
  while (true)
  {
    for (i=1;i<=n;i++)
      {
        find=false;
        for (j=0;j<=1;j++)
          for (k=1;k<=map[j][i][0];k++)
            if (map[j][i][k]==now_e)*/                   

usaco training 4.1.3 fence6 题解的更多相关文章

  1. usaco training 3.4.3 fence9 题解

    Electric Fence题解 Don Piele In this problem, `lattice points' in the plane are points with integer co ...

  2. usaco training 4.2.4 Cowcycles 题解

    Cowcycles题解 Originally by Don Gillies [International readers should note that some words are puns on ...

  3. 关于USACO Training

    做了这么久的题目,突然发现最经典的 USACO Training 还没有做过?加速水一遍吧!我会把题解放在上面的.

  4. USACO Training Section 1.1 坏掉的项链Broken Necklace

    题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 n=29 的二个例子: 第一和第二个珠子在图片中已经被作记号. 图片 A ...

  5. USACO Training Section 1.1 贪婪的送礼者Greedy Gift Givers

    P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers 题目描述 对于一群(NP个)要互送礼物的朋友,GY要确定每个人送出的钱比收到的多多少.在这一个问题中,每个人都准备了一 ...

  6. USACO Training Section 1.1 Your Ride Is Here

    题目描述 众所周知,在每一个彗星后都有一只UFO.这些UFO时常来收集地球上的忠诚支持者.不幸的是,他们的飞碟每次出行都只能带上一组支持者.因此,他们要用一种聪明的方案让这些小组提前知道谁会被彗星带走 ...

  7. USACO Training Section 1.2 双重回文数 Dual Palindrom

    题目描述 如果一个数从左往右读和从右往左读都是一样,那么这个数就叫做"回文数".例如,12321就是一个回文数,而77778就不是.当然,回文数的首和尾都应是非零的,因此0220就 ...

  8. usaco training 4.2.3 Job Processing 题解

    Job Processing题解 IOI'96 A factory is running a production line that requires two operations to be pe ...

  9. usaco training 4.2.2 The Perfect Stall 最佳牛栏 题解

    The Perfect Stall题解 Hal Burch Farmer John completed his new barn just last week, complete with all t ...

随机推荐

  1. [oracle]Oracle数据库安全管理

    目录 +  1.数据库安全控制策略概述 +  2.用户管理 +  3.资源限制与口令管理 +  4.权限管理 +  5.角色管理 +  6.审计 1.数据库安全控制策略概述 安全性是评估一个数据库的重 ...

  2. React学习小结(三)

    一.React数据的传输 1.属性和状态是react中数据传递的载体 2.属性是声明以后不允许被修改的东西 3.属性只能在组件初始化的时候声明并传入组件内部,并且在组件内部通过this.props获取 ...

  3. 【2017-06-01】Linq基础+Lambda表达式实现对数据库的增删改查

    一.Linq to sql 类 高集成化的数据库访问技术 使用Linq可以代替之前的Ado.Net.省去了自己敲代码的实体类和数据访问类的大量工作. 实体类: 添加一个Linq to sql 类 -- ...

  4. 可视化之Berkeley Earth

    去年冬天雾霾严重的那几天,写了两篇关于空气质量的文章,<可视化之PM2.5>和<谈谈我对雾霾的认识>.坦白说,环境问题是一个无法逃避又无能为力的话题.最近因为工作中有一些数据可 ...

  5. 点评阿里JAVA手册之编程规约(OOP 规约 、集合处理 、并发处理 、其他)

    下载原版阿里JAVA开发手册  [阿里巴巴Java开发手册v1.2.0] 本文主要是对照阿里开发手册,注释自己在工作中运用情况. 本文难度系数为三星(★★★) 本文为第二篇 第一篇 点评阿里JAVA手 ...

  6. 生成简单的php验证码

    之前发表过,但是上面只是一个截图,不便于大家复制和使用,所以在这重新发表一遍. <?php //生成验证码图片 Header("Content-type: image/JPEG&quo ...

  7. python3-如何正常使用HTMLTestRunner.py,生成自动化测试报告

    其实HTMLTestRunner.py是基于python2开发的,为了使其支持python3环境,需要对其的部分内容进行修改.下面我们通过编辑器打开HTMLTestRunner.py文件(编辑器可以选 ...

  8. Mac OSX 搭建 Apache php mySql phpMyAdmin 开发环境

    基本环境和配置 Mac 系统:  OS X EI Caption  10.11.4 当前Mac用户名: ceshi 需要熟知的几个基本概念和操作: 1. 新建一个终端默认的是路径是: /Users/当 ...

  9. [codeforces113D]Museum

    D. Museum time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard input ...

  10. nginx之 nginx + tomcat + redis 负载均衡且session一致性

    说明: 本文描述的是 nginx + tomcat + redis 实现应用负载均衡且满足session一致性,从安装到配置的全部过程,供大家学习!nginx 代理服务器ip: 10.219.24.2 ...