当前位置: 首页 > news >正文

【http://noi.openjudge.cn/】4.3算法之图论——1538:Gopher II

@[【http://noi.openjudge.cn/】4.3算法之图论——1538:Gopher II]

题目

查看提交统计提问
总时间限制: 2000ms 内存限制: 65536kB
描述
The gopher family, having averted the canine threat, must face a new predator.

The are n gophers and m gopher holes, each at distinct (x, y) coordinates. A hawk arrives and if a gopher does not reach a hole in s seconds it is vulnerable to being eaten. A hole can save at most one gopher. All the gophers run at the same velocity v. The gopher family needs an escape strategy that minimizes the number of vulnerable gophers.
输入
The input contains several cases. The first line of each case contains four positive integers less than 100: n, m, s, and v. The next n lines give the coordinates of the gophers; the following m lines give the coordinates of the gopher holes. All distances are in metres; all times are in seconds; all velocities are in metres per second.
输出
Output consists of a single line for each case, giving the number of vulnerable gophers.
样例输入
2 2 5 10
1.0 1.0
2.0 2.0
100.0 100.0
20.0 20.0
样例输出
1

翻译

**题目:**地鼠II
描述:
地鼠家族在避免了犬科动物的威胁后,必须面对一个新的捕食者。
有n个地鼠洞和m个地鼠洞,每个洞都位于不同的(x,y)坐标处。一只鹰来了,如果地鼠在s秒内没有到达一个洞,它很容易被吃掉。一个洞最多只能救一只地鼠。所有的地鼠都以相同的速度奔跑。地鼠家族需要一种逃生策略,以尽量减少易受攻击的地鼠数量。
输入:
输入包含几个案例。每种情况的第一行包含四个小于100的正整数:n、m、s和v。接下来的n行给出地鼠的坐标;以下m线给出了地鼠洞的坐标。所有距离均以米为单位;所有时间均以秒为单位;所有速度均以米每秒为单位。
输出:
输出由每种情况的单行组成,给出了易受攻击的地鼠数量。
例如:
在语言模型中,编码器和解码器都是由一个个的 Transformer 组件拼接在一起形成的。

代码

#include <bits/stdc++.h>
using namespace std;
struct point {double x, y;int id,oid;vector<point*> h;point() { x = 0; y = 0;oid=0;}//成员要初始化,否则会"runtime error" point(int idx, double px, double py) : id(idx), oid(0), x(px), y(py) {}
}one[210];
bool k[210];
int n, m, s, v,ans;
double x, y;
void view(int x,point* p){cout<<x<<endl;cout<<"坐标"<<p->x<<","<<p->y<<endl; cout<<"可达目标:\n";for(vector<point*>::iterator i=p->h.begin();i!=p->h.end();i++)cout<<"("<<(*i)->x<<","<<(*i)->y<<")\t";cout<<"选中"<<p->oid<<endl;
}
void view(){for(int i=n+1;i<=n+m;i++){cout<<i<<"坐标"<<one[i].x<<","<<one[i].y<<"\t"<<"达"<<one[i].oid<<endl;	} cout<<endl;
}
bool go(point *p){//该老鼠能否找到洞__匈牙利算法,进行二分图匹配 for(vector<point*>::iterator i=p->h.begin();i!=p->h.end();i++){//该老鼠能达的洞 if(k[(*i)->id])continue;//该洞已经用过了 k[(*i)->id]=1;//标记该洞,此老鼠不能再用该洞了 if(!(*i)->oid||go(&one[(*i)->oid])){//该洞没用过或者该洞本来的老鼠可以找到别的洞 (*i)->oid=p->id;//该洞被该老鼠占用 return 1;}}return 0;
} 
int main() {//freopen("data.cpp", "r", stdin);while(cin >> n >> m ){//题目讲The input contains several cases. 有多组数据 ans=0; memset(one, 0, sizeof(one));cin>> s >> v;for (int i = 1; i <=n; i++) {//遍历每个老鼠 cin >> x >> y; one[i] = point(i,x,y);}for (int i = 1; i <=m; i++) {//遍历每个鼠洞 cin >> x >> y; one[n+i] = point(n+i, x,y );for (int j = 1; j <=n; j++) {//遍历每个老鼠 double xg = one[j].x, yg = one[j].y;if ((x - xg)* (x - xg) + (y - yg)* (y - yg) <= (s * v)* (s * v)){//看哪些老鼠可以跑进哪个洞 one[n+i].h.push_back(&one[j]);//该老鼠可以跑到该洞 one[j].h.push_back(&one[n+i]);//该洞成为该老鼠的一个选项 }}	}for (int i = 1; i <=n; i++){//遍历每个老鼠 memset(k,0,sizeof(k));//清空深搜标记 if(go(&one[i]))ans++;//判定该老鼠能否找到洞 //view(i,&one[i]);//view();}//view();cout << n-ans<<endl;	}return 0;
}

细节

  • 题目说有多组数据,The input contains several cases。
  • 结构体内元素一定要初始化,否则会提示runtime error
struct point {double x, y;int id,oid;vector<point*> h;point() { x = 0; y = 0;oid=0;}//成员要初始化,否则会"runtime error" point(int idx, double px, double py) : id(idx), oid(0), x(px), y(py) {}
}one[210];

-在结构体内,每个老鼠可以躲藏的洞可以用指针描述,坚决落实。

deep seek对过程进行的分析:

初始状态
老鼠:
老鼠 1:(0.0, 3.0)
老鼠 2:(0.0, 2.0)
老鼠 3:(0.0, 1.0)
鼠洞:
鼠洞 1:(0.0, 51.0)(未占用)
鼠洞 2:(0.0, 52.0)(未占用)
鼠洞 3:(0.0, 53.0)(未占用)
匹配结果:ans = 0
步骤 1:匹配老鼠 1
老鼠 1尝试匹配:
尝试鼠洞 1:
鼠洞 1 未占用,直接匹配。
匹配成功!
更新状态:
鼠洞 1 被老鼠 1 占用。
ans = 1。
步骤 2:匹配老鼠 2
老鼠 2尝试匹配:
尝试鼠洞 1:
鼠洞 1 已被老鼠 1 占用,无法直接匹配。
递归调用:尝试为老鼠 1 寻找其他鼠洞。
递归过程:
老鼠 1 尝试鼠洞 2:
鼠洞 2 未占用,直接匹配。
匹配成功!
更新状态:
鼠洞 1 被老鼠 2 占用。
鼠洞 2 被老鼠 1 占用。
ans = 2。
步骤 3:匹配老鼠 3
老鼠 3尝试匹配:
尝试鼠洞 1:
鼠洞 1 已被老鼠 2 占用,无法直接匹配。
递归调用:尝试为老鼠 2 寻找其他鼠洞。
递归过程:
老鼠 2 尝试鼠洞 2:
鼠洞 2 已被老鼠 1 占用,无法直接匹配。
递归调用:尝试为老鼠 1 寻找其他鼠洞。
老鼠 1 尝试鼠洞 3:
鼠洞 3 未占用,直接匹配。
匹配成功!
更新状态:
鼠洞 1 被老鼠 3 占用。
鼠洞 2 被老鼠 2 占用。
鼠洞 3 被老鼠 1 占用。
ans = 3。
最终状态
匹配结果:
老鼠 1 → 鼠洞 3
老鼠 2 → 鼠洞 2
老鼠 3 → 鼠洞 1
最终答案:ans = 3

算法

一群老鼠避难,在一堆洞里找藏身处(一洞一老鼠,老鼠只能在规定时间跑到一部分洞里),求最优化,就是更多的老鼠能找到洞。这种两个集合配对问题就是二分图问题。
思路就是找洞,如果该洞被占有了,烦请它去找别的洞(增广路径),找不到就不挪窝,找到了就挪。
这就是最早是由两位匈牙利数学家 Dénes Kőnig 和 Jenő Egerváry 在 20 世纪 30 年代提出的匈牙利算法。

http://www.lryc.cn/news/546269.html

相关文章:

  • Linux常见操作命令
  • Linux下测试Wifi性能——2.Linux下wifi指令
  • (十 九)趣学设计模式 之 中介者模式!
  • Leetcode 54: 螺旋矩阵
  • abseil-cpp:环境搭建
  • Centos7部署k8s(单master节点安装)
  • RPA 职业前景:个人职场发展的 “新机遇”
  • 详解DeepSeek模型底层原理及和ChatGPT区别点
  • 《2025年软件测试工程师面试》JAVA基础面试题
  • 【算法学习之路】5.贪心算法
  • 如何打造一个安全稳定的海外社媒账号?
  • 【Python 数据结构 5.栈】
  • Qt开发⑪Qt网络+Qt音视频_使用实操
  • JavaEE--计算机是如何工作的
  • API接口:企业名称、注册号、统一社会信用代码、企业类型、成立日期和法定代表人等数据 API 接口使用指南
  • 微信小程序text组件decode属性的小问题
  • 【计算机网络入门】初学计算机网络(九)
  • LeetCode 974:和可被 K 整除的子数组
  • vector习题
  • 001-码云操作
  • 数据结构:二叉搜索树(排序树)
  • 【愚公系列】《Python网络爬虫从入门到精通》036-DataFrame日期数据处理
  • C++(蓝桥杯常考点)
  • 支付宝 IoT 设备入门宝典(下)设备经营篇
  • 蓝桥杯 之 填空题-位运算与循环
  • iOS逆向工程概述与学习路线图
  • DeepSeek 助力 Vue3 开发:打造丝滑的时间选择器(Time Picker)
  • 基于 Ingress-Nginx 实现 mTLS 双向认证
  • 学到什么记什么(25.3.3)
  • 【子网掩码计算器:Python + Tkinter 实现】