[USACO07DEC]Mud Puddles S
Posted MT默哥的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[USACO07DEC]Mud Puddles S相关的知识,希望对你有一定的参考价值。
[USACO07DEC]Mud Puddles S
题目描述
Farmer John is leaving his house promptly at 6 AM for his daily milking of Bessie. However, the previous evening saw a heavy rain, and the fields are quite muddy. FJ starts at the point (0, 0) in the coordinate plane and heads toward Bessie who is located at (X, Y) (-500 ≤ X ≤ 500; -500 ≤ Y ≤ 500). He can see all N (1 ≤ N ≤ 10,000) puddles of mud, located at points (Ai, Bi) (-500 ≤ Ai ≤ 500; -500 ≤ Bi ≤ 500) on the field. Each puddle occupies only the point it is on.
Having just bought new boots, Farmer John absolutely does not want to dirty them by stepping in a puddle, but he also wants to get to Bessie as quickly as possible. He\'s already late because he had to count all the puddles. If Farmer John can only travel parallel to the axes and turn at points with integer coordinates, what is the shortest distance he must travel to reach Bessie and keep his boots clean? There will always be a path without mud that Farmer John can take to reach Bessie.
清早6:00,Farmer John就离开了他的屋子,开始了他的例行工作:为贝茜挤奶。前一天晚上,整个农场刚经受过一场瓢泼大雨的洗礼,于是不难想见,FJ 现在面对的是一大片泥泞的土地。FJ的屋子在平面坐标\\((0, 0)\\)的位置,贝茜所在的牛棚则位于坐标\\((X,Y)\\) \\((-500 <= X <= 500; -500 <= Y <= 500)\\)处。当然咯, FJ也看到了地上的所有N(1 <= N <= 10,000)个泥塘,第i个泥塘的坐标为 \\((A\\_i, B\\_i)\\) \\((-500 <= A\\_i <= 500;-500 <= B\\_i <= 500)\\)。每个泥塘都只占据了它所在的那个格子。 Farmer John自然不愿意弄脏他新买的靴子,但他同时想尽快到达贝茜所在的位置。为了数那些讨厌的泥塘,他已经耽搁了一些时间了。如果Farmer John 只能平行于坐标轴移动,并且只在x、y均为整数的坐标处转弯,那么他从屋子门口出发,最少要走多少路才能到贝茜所在的牛棚呢?你可以认为从FJ的屋子到牛棚总是存在至少一条不经过任何泥塘的路径。
输入格式
* Line 1: Three space-separate integers: X, Y, and N.
第1行: 3个用空格隔开的整数:X,Y 和 N
* Lines 2..N+1: Line i+1 contains two space-separated integers: Ai and Bi
第2..N+1行: 第i+1行为2个用空格隔开的整数:A_i 和 B_i
输出格式
* Line 1: The minimum distance that Farmer John has to travel to reach Bessie without stepping in mud.
第1行: 输出1个整数,即FJ在不踏进泥塘的情况下,到达贝茜所在牛棚所需要 走过的最小距离
样例
样例输入
1 2 7
0 2
-1 3
3 1
1 1
4 2
-1 1
2 2
样例输出
11
代码
#include <bits/stdc++.h>
using namespace std;
struct Node
int x,y,dis;
;
queue<Node> q;
bool m[1001][1001];
int x,y,n;
const int dx[4]=-1,0,1,0;
const int dy[4]=0,1,0,-1;
bool f(int x,int y)
return x<-500||x>500||y<-500||y>500||m[x+500][y+500];
void bfs()
q.push(0,0,0);
while(!q.empty())
int nx=q.front().x,ny=q.front().y,val=q.front().dis;
q.pop();
if(f(nx,ny)) continue;
m[nx+500][ny+500]=1;
if(nx==x&&ny==y)
cout << val << endl;
return;
for(int i=0;i<4;i++)
q.push(nx+dx[i],ny+dy[i],val+1);
int main()
cin >> x >> y >> n;
while(n--)
int X,Y;
cin >> X >> Y;
m[X+500][Y+500]=1;
bfs();
return 0;
本文来自小默的博客,转载请注明原文链接:https://www.cnblogs.com/momotrace/p/p2873.html
151. [USACO Dec07] 建造路径
★★ 输入文件:roads.in
输出文件:roads.out
简单对比
时间限制:1 s 内存限制:128 MB
译 by CmYkRgB123
描述
Farmer John 刚刚得到了几个新农场!他想把这几个农场用路连接起来,这样他就可以通过笔直的公路从一个农场到另一个农场了。现在已经有了几条连接着的农场。
N (1 ≤ N ≤ 1,000) 个农场中,每个农场的位置在坐标平面的 (Xi, Yi) (0 ≤ Xi ≤ 1,000,000; 0 ≤ Yi ≤ 1,000,000)。已经有 M (1 ≤ M ≤ 1,000) 条路以前就被建好了。请你帮助 Farmer John 考虑建设尽量少长度的额外的路,使他的农场连在一起。
输入
* 第 1 行: 两个整数: N , M
* 第 2..N+1 行: 两个整数 Xi , Yi
* 第 N+2..N+M+2 行: 两个整数: i , j, 表示已经存在从农场i到农场j的路。
输出
* 第 1 行: 额外的路的最少长度,保留2小数。 请使用 64 位的浮点数。
样例输入
4 1
1 1
3 1
2 3
4 3
1 4
样例输出
4.00
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> using namespace std; const int N=1010; bool vis[N][N]; int fa[N]; int n,m,js,xx,yy,tot,total; double answer; struct node{ double x,y; }E[N]; struct NODE{ int st,ed; double dis; }EE[500000]; inline int read() { int x=0;char c=getchar(); while(c<‘0‘||c>‘9‘)c=getchar(); while(c>=‘0‘&&c<=‘9‘)x=x*10+c-‘0‘,c=getchar(); return x; } inline double calc(int a,int b) { return sqrt(abs(E[a].x-E[b].x)*abs(E[a].x-E[b].x)+abs(E[a].y-E[b].y)*abs(E[a].y-E[b].y)); } bool cmp(NODE a,NODE b) { return a.dis<b.dis; } int getfa(int x) { return fa[x]==x?x:fa[x]=getfa(fa[x]); } int main() { freopen("roads.in","r",stdin); freopen("roads.out","w",stdout); n=read(),m=read(); for(int i=1;i<=n;i++) fa[i]=i,scanf("%lf%lf",&E[i].x,&E[i].y); for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) EE[++js].st=i, EE[js].ed=j, EE[js].dis=calc(i,j); for(int i=1;i<=m;i++) xx=read(), yy=read(), vis[xx][yy]=vis[yy][xx]=1; for(int i=1;i<=js;i++) if(vis[EE[i].st][EE[i].ed]) { int fx=getfa(EE[i].st); int fy=getfa(EE[i].ed); fa[fx]=fy; EE[i].dis=double(N<<4); } tot=n-1-m; sort(EE+1,EE+js+1,cmp); for(int i=1;i<=js;i++) { int fx=getfa(EE[i].st); int fy=getfa(EE[i].ed); if(fx!=fy) { fa[fx]=fy; total++; answer+=EE[i].dis; if(total==tot) break; } } printf("%.2lf",answer); return 0; }
以上是关于[USACO07DEC]Mud Puddles S的主要内容,如果未能解决你的问题,请参考以下文章
bzoj1627 / P2873 [USACO07DEC]泥水坑Mud Puddles
P2872 [USACO07DEC]Building Roads S
洛谷 P2872 [USACO07DEC]Building Roads S
P2872 [USACO07DEC]Building Roads S(最小生成树)