【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 年代提出的匈牙利算法。

相关推荐
闻缺陷则喜何志丹35 分钟前
【Assimp】三维模型读取库
c++·算法·assimp·三维模型库
FlightYe1 小时前
音视频修炼之容器与协议(六):HTTP 协议
android·linux·网络·网络协议·算法·http·音视频
老师我太想进步了20261 小时前
【数据结构-13】堆的实现
数据结构·算法
怕浪猫1 小时前
GLM-5.4 深度拆解:750B 参数如何「循环」出 2-3T 级智能?
算法·面试·github
张祥6422889042 小时前
RTKLIB 源码解析:方差计算与高度角定权模型深度剖析
算法
电科_银尘3 小时前
【大模型算法与工程 37】语言模型强化学习基础:策略、奖励与优势
人工智能·算法·语言模型
青少儿编程课堂1 天前
蓝桥杯真题解析:三维偏序的CDQ分治(全国青少年信息素养大赛与电子学会编程考级)
c++·python·算法·bfs·信息学竞赛
Navigator_Z1 天前
LeetCode //C - 1323. Maximum 69 Number
c语言·算法·leetcode
geovindu1 天前
python:Lasso
开发语言·后端·python·算法