USACO 4.1 Fence Loops(Floyd求最小环)
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写最小环,但是始终没想出来怎么写,结果发现好简单啊……果然还是自己蒟蒻
回归floyd的三维形式g[k,i,j]也就是用前k个点更新了i,j,然后我们发现我们求的环就是
i到j不包括k的最短路+i到k距离+k到j距离
我们更新一次floyd二维矩阵存储的就是g[k-1,i,j],然后我们得到的k点是新的,未被使用过,这样就可以做了
【这道题图给的形式真是好恶心啊……】
/*
ID: ivorysi
PROG: fence6
LANG: C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x1f1f1f1f
#define ivorysi
#define mo 97797977
#define ha 974711
#define ba 47
#define fi first
#define se second
#define pii pair<int,int>
using namespace std;
typedef long long ll;
int s,len[],num[][],cnt,a1[],a2[],use[];
int g[][],f[][];
int n,t1,t2,ans;
void solve(){
scanf("%d",&n);
siji(i,,n) {
scanf("%d",&s);
scanf("%d",&len[s]);
scanf("%d%d",&t1,&t2); bool f1=,f2=;
siji(j,,t1) {
scanf("%d",&a1[j]);
if(use[a1[j]]) f1=;
}
siji(j,,t2) {
scanf("%d",&a2[j]);
if(use[a2[j]]) f2=;
}
if(num[s][]>=)continue;
if(f1) {
num[s][++num[s][]]=++cnt;
siji(j,,t1) {
num[a1[j]][++num[a1[j]][]]=cnt;
}
}
if(f2) {
num[s][++num[s][]]=++cnt;
siji(j,,t2) {
num[a2[j]][++num[a2[j]][]]=cnt;
}
}
use[s]=;
}
memset(g,inf,sizeof(g));
memset(f,inf,sizeof(f));
siji(i,,n) {
g[num[i][]][num[i][]]=g[num[i][]][num[i][]]=len[i];
f[num[i][]][num[i][]]=f[num[i][]][num[i][]]=len[i];
}
siji(i,,cnt) {g[i][i]=;f[i][i]=;}
ans=inf;
siji(k,,cnt) {
xiaosiji(i,,k) {//因为不能用k点所以是<k
xiaosiji(j,i+,k) {
ans=min(ans,g[i][j]+f[i][k]+f[k][j]);
}
}
siji(i,,cnt) {
siji(j,,cnt) {
g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
}
}
} printf("%d\n",ans);
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("fence6.in","r",stdin);
freopen("fence6.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
}
USACO 4.1 Fence Loops(Floyd求最小环)的更多相关文章
- USACO 4.1 Fence Loops
Fence Loops The fences that surround Farmer Brown's collection of pastures have gotten out of contro ...
- 2017"百度之星"程序设计大赛 - 资格赛【1001 Floyd求最小环 1002 歪解(并查集),1003 完全背包 1004 01背包 1005 打表找规律+卡特兰数】
度度熊保护村庄 Accepts: 13 Submissions: 488 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3276 ...
- Floyd求最小环!(转载,非原创) 附加习题(原创。)HDU-1599
//Floyd 的 改进写法可以解决最小环问题,时间复杂度依然是 O(n^3),储存结构也是邻接矩阵 int mincircle = infinity; Dist = Graph; ;k<nVe ...
- 2018.09.15 hdu1599find the mincost route(floyd求最小环)
传送门 floyd求最小环的板子题目. 就是枚举两个相邻的点求最小环就行了. 代码: #include<bits/stdc++.h> #define inf 0x3f3f3f3f3f3f ...
- 【BZOJ 1027】 (凸包+floyd求最小环)
[题意] 某公司加工一种由铁.铝.锡组成的合金.他们的工作很简单.首先进口一些铁铝锡合金原材料,不同种类的原材料中铁铝锡的比重不同.然后,将每种原材料取出一定量,经过融解.混合,得到新的合金.新的合金 ...
- 算法复习——floyd求最小环(poj1734)
题目: 题目描述 N 个景区,任意两个景区之间有一条或多条双向的路来连接,现在 Mr.Zeng 想找一条旅游路线,这个路线从A点出发并且最后回到 A 点,假设经过的路线为 V1,V2,....VK,V ...
- floyd求最小环 模板
http://www.cnblogs.com/Yz81128/archive/2012/08/15/2640940.html 求最小环 floyd求最小环 2011-08-14 9:42 1 定义: ...
- CF 1206D - Shortest Cycle Floyd求最小环
Shortest Cycle 题意 有n(n <= 100000)个数字,两个数字间取&运算结果大于0的话连一条边.问图中的最小环. 思路 可以发现当非0数的个数很大,比如大于200时, ...
- 弗洛伊德Floyd求最小环
模板: #include<bits/stdc++.h> using namespace std; ; const int INF = 0xffffff0; ]; void Solve(in ...
- POJ1734 Sightseeing trip (Floyd求最小环)
学习了一下用Floyd求最小环,思路还是比较清晰的. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring ...
随机推荐
- 淘宝code
淘宝code 相信大家都听说过GitHub,也有很多人在用,但是GitHub毕竟在国外,速度不是很给力,而且安装过程也是很漫长.今天来给大家介绍一个国内的免费的开源项目平台,当然也是一个SVN版本控制 ...
- WINHTTP的API接口说明
BOOL WINAPI WinHttpAddRequestHeaders( _In_ HINTERNET hRequest, _In_ LPCWSTR pwszHeaders, _In ...
- {{angular.js 使用技巧}} - 实现计算列属性
前端MV*框架现在有很多,其中某些框架有计算列(又叫监控属性),比如:微软推荐的 Knockout.js 和博客园司徒正美的 avalon.js 框架. 本人只使用过 Knockout.js,aval ...
- SpringMVC之 数据绑定-1
SpringMVC学习系列(4) 之 数据绑定-1 在系列(3)中我们介绍了请求是如何映射到一个action上的,下一步当然是如何获取到请求中的数据,这就引出了本篇所要讲的内容—数据绑定. 首先看一下 ...
- 通过反射生成SQL的例子
全文摘自http://www.cnblogs.com/g1mist/p/3227290.html,很好的一个实例. 反射提供了封装程序集.模块和类型的对象.您可以使用反射动态地创建类型的实例,将类型绑 ...
- 用Python复习离散数学(一)
最近要复习离散数学,不想挂啊,但是又想编程,大家知道啦,程序员离不开代码啊,所用想边复习边写代码,所以就自己用代码去实现一下离散的知识点,当做复习,自知自己的Python很渣,也想借此巩固一下基础,哈 ...
- Mac OS X安装之虚拟机环境下的总结
最近一直忙着公司iOS Touch的新版发布,终于忙过了.现在,又开始了新的阶段,不过算是轻松了很多.回来一看,自己的博客空空如也,实在受不了了.于是,开始更一下吧,哈哈. 这个文档是我几个月前,开始 ...
- poj2187(未完、有错)
凸包求直径(socalled..) 采用Graham+Rotating_Calipers,Graham复杂度nlogn,RC算法复杂度n,所以时间复杂度不会很高. 学习RC算法,可到http://cg ...
- ASP.NET网站单独
解决ASP.NET网站单独发布指定页面问题 目录 前提 开始 aspx.cs文件放到单独的类库项目 一个可选择勾选页面的发布工具:LimusicAddin 前提 Asp.net 发布分为:动态编译和预 ...
- [原]iOS Makefile Template
export DEVELOPER_DIR := $(shell xcode-select --print-path) PLATFORM="$(DEVELOPER_DIR)/Platforms ...