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.

SAMPLE INPUT (file fence6.in)

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

OUTPUT FORMAT

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

SAMPLE OUTPUT (file fence6.out)

12

无向图求最小环
把图中边信息转化成点,floyd求最小环。
 /*
ID:hyx34931
LANG:C++
TASK:fence6
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector> using namespace std; const int MAX_N = ;
const int INF = 1e6 + ;
int g[MAX_N][MAX_N], f[MAX_N][MAX_N];
int id[MAX_N][];
int N, n = ;
bool done[MAX_N]; void dfs(int u) {
printf("%d\n", u);
done[u] = ;
for (int i = ; i < n; ++i) {
if (f[u][i] && !done[i]) dfs(i);
}
} void floyd() {
for (int i = ; i < n; ++i) {
for (int j = ; j < n; ++j) {
f[i][j] = g[i][j];
}
} int ans = INF;
for (int k = ; k < n; ++k) {
for (int i = ; i <= k - ; ++i) {
for (int j = i + ; j <= k - ; ++j) {
ans = min(ans, f[i][j] + g[i][k] + g[k][j]);
}
} for (int i = ; i < n; ++i) {
for (int j = ; j < n; ++j) {
f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
}
}
} //printf("%d\n", f[0][3]);
printf("%d\n", ans);
}
int main()
{
freopen("fence6.in", "r", stdin);
freopen("fence6.out", "w", stdout);
scanf("%d", &N);
memset(id, -, sizeof(id));
//printf("%d %d\n", id[7][0], id[7][1]);
for (int i = ; i <= N; ++i) {
int n1, n2, cost, u;
scanf("%d%d%d%d", &u, &cost, &n1, &n2); int t[];
for (int j = ; j <= n1; ++j) {
scanf("%d", &t[j]);
}
if (id[u][] != id[ t[]][] && id[u][] != id[ t[] ] []) swap(id[u][], id[u][]);
if (id[u][] == -) {
id[u][] = n++;
for (int j = ; j <= n1; ++j) {
if (id[ t[j] ][] == -) id[ t[j] ][] = id[u][];
else if (id[ t[j] ][] == -) id[ t[j] ][] = id[u][];
}
}
for (int j = ; j <= n2; ++j) {
scanf("%d", &t[j]);
} if (id[u][] == -) {
id[u][] = n++;
for (int j = ; j <= n2; ++j) {
if (id[ t[j] ][] == -) id[ t[j] ][] = id[u][];
else if (id[ t[j] ][] == -) id[ t[j] ][] = id[u][];
}
} g[ id[u][] ][ id[u][] ] = g[ id[u][] ][ id[u][] ] = cost;
} for (int i = ; i < n; ++i) {
for (int j = ; j < n; ++j) {
if (!g[i][j]) g[i][j] = INF;
if (i == j) g[i][j] = ; }
}
//memset(done, 0, sizeof(done));
/*printf("n = %d\n", n);
for (int i = 1; i <= N; ++i) {
printf("%d : %d %d\n", i, id[i][0], id[i][1]);
}*/
//dfs(0);
floyd();
//cout << "Hello world!" << endl;
return ;
}

USACO 4.1 Fence Loops的更多相关文章

  1. USACO 4.1 Fence Loops(Floyd求最小环)

    Fence Loops The fences that surround Farmer Brown's collection of pastures have gotten out of contro ...

  2. 洛谷P2738 [USACO4.1]篱笆回路Fence Loops

    P2738 [USACO4.1]篱笆回路Fence Loops 11通过 21提交 题目提供者该用户不存在 标签USACO 难度提高+/省选- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 ...

  3. 洛谷 P2738 [USACO4.1]篱笆回路Fence Loops

    P2738 [USACO4.1]篱笆回路Fence Loops 题目描述 农夫布朗的牧场上的篱笆已经失去控制了.它们分成了1~200英尺长的线段.只有在线段的端点处才能连接两个线段,有时给定的一个端点 ...

  4. USACO 6.3 Fence Rails(一道纯剪枝应用)

    Fence RailsBurch, Kolstad, and Schrijvers Farmer John is trying to erect a fence around part of his ...

  5. USACO 3.3 fence 欧拉回路

    题意:求给定图的欧拉回路(每条边只走一次) 若欧拉回路存在,图中只可能有0个or2个奇数度的点. 求解时,若有奇数度的点,则必须从该点开始.否则可以从任一点开始 求解过程:dfs //主程序部分 # ...

  6. USACO 4.1 Fence Rails

    Fence RailsBurch, Kolstad, and Schrijvers Farmer John is trying to erect a fence around part of his ...

  7. [USACO4.1]篱笆回路Fence Loops

    题目:USACO Training 4.1(在官网上提交需加文件输入输出).洛谷P2738. 题目大意:给你一张图里的边集,让你求出这张图的最小环. 解题思路:求最小环很简单,用Floyd即可.最重要 ...

  8. USACO4.1 Fence Loops【最小环&边->点转化】

    数据不是很大,如果要转换为正常的那种建图方式的话,可以给点进行标号,用一个二维数组存这两条边相交的那个点的标号,方便处理.一定要注意不要同一个点使用不同的编号也不要不同的点使用同一个编号(这不是废话嘛 ...

  9. 题解 P2738 【[USACO4.1]篱笆回路Fence Loops】

    这题是我期中测试的一题水题,然而英文题目太长了不想读...后面考完被同学提醒后20分钟切了(心塞) 切完看了波题解,发现貌似我的方法跟大家都不一样呢... 常规做法: \(Floyd\) 这个有三页的 ...

随机推荐

  1. 机器学习4logistic回归

    对于线性回归.logistic回归,在以前准备学习深度学习的时候看过一点,当时的数学基础有点薄弱,虽然现在还是有点差,当时看到神经网络之后就看不下去了. 不过这次是通过python对logistic回 ...

  2. spark streaming updateStateByKey 使用方法

    updateStateByKey 解释: 以DStream中的数据进行按key做reduce操作,然后对各个批次的数据进行累加 在有新的数据信息进入或更新时.能够让用户保持想要的不论什么状.使用这个功 ...

  3. python 002 文件输入输出

    python 文件对象不仅可以访问普通磁盘文件,也可以访问抽象层面上的文件对象(例如URL地址) 打开文件open() file()功能一致可以任意替代 fp = open('/etc/test.tx ...

  4. yun install java

    # yum install java-1.7.0-openjdk # yum install java-1.7.0-openjdk-devel 需要执行以上两步 查看版本 # java -versio ...

  5. WPF:通过Window.DataContext实现窗口间传值

    通过Window.DataContext实现窗口之间的传值,特别是跨窗口控件的联动,具有无可比拟的优势.实现方法如下: 1.  MainWindow.xaml,在Window.DataContext中 ...

  6. PCB LDI 实现周期自动更新 实现思路

    一.基本思路整理如下: 二.封周期启动程序C#代码(部份代码) /// <summary> /// 单个生产型号 更新周期 /// </summary> /// <par ...

  7. 网易UI自动化测试工具Airtest中导入air文件中的方法

    最近看了一下网易的Airtest ,UI测试工具,写了一些后在导入其他air文件中的.py文件,卡了一下,现在博客中纪录一下导入其他air文件的方式: 在Airtest 测试工具中,导入其他air文件 ...

  8. [转]RDLC报表——动态添加列

    本文转自:http://www.cnblogs.com/pszw/archive/2012/07/19/2599937.html 前言 最近接到一个需求:在给定的数据源中,某(些)列,可能需要单独统计 ...

  9. 5.30获取openid和createTime--mybatis自动生成接口和映射【这里需要自定义】

    自定义sql获取数据:        dao:            前提是反向成了代码:                A : 接口PhoneModelMapper extends IBaseMap ...

  10. Jsp页面报错状态码含义

    原来,全部在HttpServletResponse接口的字段里 状态码 (),表示一个请求已经被接受处理,但还没有完成.  状态码 (),表明HTTP服务器从一个服务器收到了一个无效的响应,当其作为一 ...