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

【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.kler.cn/a/571311.html

相关文章:

  • 14天 -- Redis 的持久化机制有哪些?Redis 主从复制的实现原理是什么? Redis 数据过期后的删除策略是什么?
  • DeepSeek开源周-汇总
  • VB6网络通信软件开发,上位机开发,TCP网络通信,读写数据并处理,完整源码下载
  • Leetcode 3472. Longest Palindromic Subsequence After at Most K Operations
  • 【零基础到精通Java合集】第十六集:多线程与并发编程
  • vue2(笔记)4.0vueRouter.声明式/编程式导航以及跳转传参.重定向
  • 浅谈汽车系统电压优缺点分析
  • PyTorch 中结合迁移学习和强化学习的完整实现方案
  • 【2025rust笔记】超详细,小白,rust基本语法
  • vue 提升html2canvas渲染速度
  • 第十天-字符串:编程世界的文本基石
  • 深入 Vue.js 组件开发:从基础到实践
  • 深入探索像ChatGPT这样的大语言模型
  • 记一次渗透测试实战:SQL注入漏洞的挖掘与利用
  • Trae:国内首款AI原生IDE,编程效率大提升
  • AI大模型-提示工程学习笔记21-图提示 (Graph Prompting)
  • 从0到1,动漫短剧源码搭建,动漫短剧小程序
  • 【暴力枚举】P1618 三连击(升级版)
  • Mac远程桌面软件哪个好用?
  • conda环境管理 kernel注册到jupyter notebook