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\) 这个有三页的 ...
随机推荐
- [转]十五天精通WCF——第一天 三种Binding让你KO80%的业务
转眼wcf技术已经出现很多年了,也在.net界混的风生水起,同时.net也是一个高度封装的框架,作为在wcf食物链最顶端的我们所能做的任务已经简单的不能再简单了, 再简单的话马路上的大妈也能写wcf了 ...
- HDU 2206 IP的计算(字符串处理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2206 Problem Description 在网络课程上,我学到了非常多有关IP的知识. IP全称叫 ...
- 为Chrome开发插件提高工作效率
工作生活,什么最珍贵,我觉得是时间,怎么节约时间是一个最重要的问题,如果你有重复的工作在网页上,请接着看 上手步骤: 打开https://developer.chrome.com/extensions ...
- java 工厂方法模式简单实例
工厂方法模式:也叫工厂模式,属于类创建型模式,工厂父类(接口)负责定义产品对象的公共接口,而子类工厂则负责创建具体的产品对象. 目的:是为了把产品的实例化操作延迟到子类工厂中完成,通过工厂子类来决定究 ...
- luogu1026 统计单词个数
题目大意 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份(1< k< =40),且每份中包含 ...
- sublime -text 删除已安装插件
按ctr+shift +p然后输入remove 回车,再输入要删除的插件名
- 【K8s】Kubernetes架构理解
抽空学习了一下Kubernetes,感觉和大数据领域内集群的资源管理.任务调度等有异曲同工之处,简单总结一下备忘. [概念] Kubernetes是一个工业级的容器编排平台,单词有点长,常用K8s代称 ...
- [SGU 199] Beautiful People
[SGU 199] Beautiful People The most prestigious sports club in one city has exactly N members. Each ...
- Windows7环境下Composer 安装包的Cache目录位置
http://segmentfault.com/a/1190000000355928 https://getcomposer.org/doc/ 要说Composer的用法,以后再说,现在只记录wind ...
- angular js 公告墙
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...