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. HDU 5502

    枚举所有的最大值盒子里糖果为K的情况,对于位置p,dp[p]为p以前的,第p个操作为抽到不是蓝球里的情况,盒子里最多糖果为k的情况的概率.而到p这个位置,可以有连续最多k-1(因为第k个操作必须为抽到 ...

  2. ZOJ 3213

    /* ZOJ 3213 好吧,看过那种括号表示法后,就崩溃了,实在受不了.情况复杂,写了两天,人也有点傻X了,只能放弃,转而用最小表示法. 最小表示法不难写: 1)首先,要承认路径上有格子不选的情况, ...

  3. Swift基本常识点

    import Foundation // 单行注释 // 多行注释(支持嵌套,OC是不支持的) // 常量let,初始化之后就不可改变. // 常量的具体类型可以自动识别,等号后面是什么类型,它就是什 ...

  4. Python入门 五、学着机器思考

    正则表达式(1) import re text = "Hi,I am Shirley Hilton.I am his wife." m = re.findall(r"hi ...

  5. clone的rails目录下命令无效问题

    异常坑爹,在公司克隆自己的项目.然后在项目目录下rails s还有一大堆命令无效,提示 Usage: rails new APP_PATH [options]   找了半天总算找到解决办法了,在项目目 ...

  6. C#中动态读取配置

    有些时候,文件修改需要及时的响应,这个时候就需要实时读取文件,预先想的是写一个计时器,每隔多久运行一次,但是不能实时响应,所以采用监听文件的方式实现读取数据 C#监听文件变化 /// <summ ...

  7. Jmeter - 服务器性能检测

    在对系统做压力测试时,往往需要对服务的性能进行监控,包括CPU,Memory,IO,还有网络情况进行监控. Jemter有个一插件,能很好的支持这些性能监控.原理是服务器启动服务之后,测试机发起请求, ...

  8. 可变长度参数列表(Stering...aaa)

  9. 获取Json中特定的值

      假如我们得到了一个json的数据:json===>   {"Head":{"TransCode":"X1009","Tr ...

  10. Deutsch lernen (07)

    1. die Einführung, -en 介绍:引言,导论 Könnten Sie uns zuerst eine kleine Einführung über das Klonen geben. ...