【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 天前
算法竞赛C++常用的STL
c++·算法
小羊没烦恼!2 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
猎头南楼2 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
旖旎夜光2 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark2 天前
大规模并行计算中的负载均衡算法研究4
算法
Because_of_Her12 天前
并查集-听课笔记
笔记·算法·并查集
码流子2 天前
高速公路安全监测实践:碰撞监测预警+物联网底座,从感知到处置的闭环
大数据·人工智能·物联网·算法·架构
another heaven2 天前
【算法/C++ MD5算法能否逆解码?原理、C++实现与同类哈希算法对比】
c++·算法·哈希算法
wzdark2 天前
从算法设计模式看编程思维的抽象能力4
算法
2601_962218612 天前
万象生鲜系统称重自动多退少补算法解决生鲜非标品痛点
大数据·数据库·人工智能·python·算法