USACO 4.1 Fence Loops
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:
|
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的更多相关文章
- USACO 4.1 Fence Loops(Floyd求最小环)
Fence Loops The fences that surround Farmer Brown's collection of pastures have gotten out of contro ...
- 洛谷P2738 [USACO4.1]篱笆回路Fence Loops
P2738 [USACO4.1]篱笆回路Fence Loops 11通过 21提交 题目提供者该用户不存在 标签USACO 难度提高+/省选- 提交 讨论 题解 最新讨论 暂时没有讨论 题目描述 ...
- 洛谷 P2738 [USACO4.1]篱笆回路Fence Loops
P2738 [USACO4.1]篱笆回路Fence Loops 题目描述 农夫布朗的牧场上的篱笆已经失去控制了.它们分成了1~200英尺长的线段.只有在线段的端点处才能连接两个线段,有时给定的一个端点 ...
- USACO 6.3 Fence Rails(一道纯剪枝应用)
Fence RailsBurch, Kolstad, and Schrijvers Farmer John is trying to erect a fence around part of his ...
- USACO 3.3 fence 欧拉回路
题意:求给定图的欧拉回路(每条边只走一次) 若欧拉回路存在,图中只可能有0个or2个奇数度的点. 求解时,若有奇数度的点,则必须从该点开始.否则可以从任一点开始 求解过程:dfs //主程序部分 # ...
- USACO 4.1 Fence Rails
Fence RailsBurch, Kolstad, and Schrijvers Farmer John is trying to erect a fence around part of his ...
- [USACO4.1]篱笆回路Fence Loops
题目:USACO Training 4.1(在官网上提交需加文件输入输出).洛谷P2738. 题目大意:给你一张图里的边集,让你求出这张图的最小环. 解题思路:求最小环很简单,用Floyd即可.最重要 ...
- USACO4.1 Fence Loops【最小环&边->点转化】
数据不是很大,如果要转换为正常的那种建图方式的话,可以给点进行标号,用一个二维数组存这两条边相交的那个点的标号,方便处理.一定要注意不要同一个点使用不同的编号也不要不同的点使用同一个编号(这不是废话嘛 ...
- 题解 P2738 【[USACO4.1]篱笆回路Fence Loops】
这题是我期中测试的一题水题,然而英文题目太长了不想读...后面考完被同学提醒后20分钟切了(心塞) 切完看了波题解,发现貌似我的方法跟大家都不一样呢... 常规做法: \(Floyd\) 这个有三页的 ...
随机推荐
- 4.非关系型数据库(Nosql)之mongodb:普通索引,唯一索引
一:普通索引 1创建一个新的数据库 > use toto; switched to db toto > show dbs; admin (empty) local 0.078GB & ...
- Linuxpassword破解及grub加密演示
password破解及grub加密演示 so easy,不可不会! 原理: 通过进入单用户模式(单用户模式也即是仅仅有一个用户能够訪问资源的状态,且单用户模式就是系统处于最原始的状态,大部分服务还未开 ...
- MyBatis -- 对表进行增删改查(基于注解的实现)
1.MyBatis对数据库表进行增/删/改/查 前一篇使用基于XML的方式实现对数据库的增/删/改/查 以下我们来看怎么使用注解的方式实现对数据库表的增/删/改/查 1.1 首先须要定义映射sql的 ...
- mysql 日期计算,今天,明天,本周,下周,本月,下月
--今天 DATE_FORMAT(BIRTH_DATE,'%Y-%m-%d') = CURDATE() --明天 DATE_FORMAT(BIRTH_DATE,'%Y-%m-%d') = TIMEST ...
- The Elder HDU - 5956
/* 树上斜率优化 一开始想的是构造出一个序列 转化成一般的dp但是可能被卡 扫把状的树的话可能变成n*n 其实可以直接在树上维护这个单调队列 dfs虽然搞得是一棵树,但是每次都是dfs到的都是一个序 ...
- 【BZOJ 3620】 似乎在梦中见过的样子
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3620 [算法] KMP [代码] #include<bits/stdc++.h ...
- 最新昆石VOS2009/VOS3000手机号段导入文件(手机归属地)
使用2017年4月最新版手机号段归属地制作,支持所有版本的VOS 共360569条记录,兼容所有版本的昆石VOS,包括VOS2009.vos3000.vos5000 导入比较简单.下载后解压到桌面在V ...
- PCB 奥宝LDI 输出正负片转换关系
今天继续对P2 奥宝LDI改造,在文件输出的时候遇到了一个正负片转换问题,研究了半天一直没有得到解决, 回来后前前后后整理今天参数输出与输出的关系,最终还梳理清楚了, 今天小结:一项技术只要用心去研究 ...
- idea新建文件无法识别(二)
1,出现问题的步骤: 当想新增一个Hello.java文件时候 选错了文件类型.新建了一个text文件.当回过头来把文件后缀改掉的时候发现idea无法识别,颜色一直是灰色. 2,解决问题的办法: 选择 ...
- CAS配置(2)之主配置
WEB-INF目录 1.cas.properties文件(打开关闭SSL,主题,定制页面设置) #默认端口配置 #server.name=http://localhost:8080server.nam ...