【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 组件拼接在一起形成的。

代码

cpp 复制代码
#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 年代提出的匈牙利算法。

相关推荐
你撅嘴真丑2 小时前
第九章-数字三角形
算法
uesowys2 小时前
Apache Spark算法开发指导-One-vs-Rest classifier
人工智能·算法·spark
ValhallaCoder3 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
董董灿是个攻城狮3 小时前
AI 视觉连载1:像素
算法
智驱力人工智能3 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算
孞㐑¥4 小时前
算法——BFS
开发语言·c++·经验分享·笔记·算法
月挽清风4 小时前
代码随想录第十五天
数据结构·算法·leetcode
XX風4 小时前
8.1 PFH&&FPFH
图像处理·算法
NEXT064 小时前
前端算法:从 O(n²) 到 O(n),列表转树的极致优化
前端·数据结构·算法
代码游侠5 小时前
学习笔记——设备树基础
linux·运维·开发语言·单片机·算法