题干:

试题编号: 201403-4
试题名称: 无线网络
时间限制: 1.0s
内存限制: 256.0MB
问题描述:

问题描述

  目前在一个很大的平面房间里有 n 个无线路由器,每个无线路由器都固定在某个点上。任何两个无线路由器只要距离不超过 r 就能互相建立网络连接。
  除此以外,另有 m 个可以摆放无线路由器的位置。你可以在这些位置中选择至多 k 个增设新的路由器。
  你的目标是使得第 1 个路由器和第 2 个路由器之间的网络连接经过尽量少的中转路由器。请问在最优方案下中转路由器的最少个数是多少?

输入格式

  第一行包含四个正整数 n,m,k,r。(2 ≤ n ≤ 100,1 ≤ k ≤ m ≤ 100, 1 ≤ r ≤ 108)。
  接下来 n 行,每行包含两个整数 xi 和 yi,表示一个已经放置好的无线 路由器在 (xi, yi) 点处。输入数据保证第 1 和第 2 个路由器在仅有这 n 个路由器的情况下已经可以互相连接(经过一系列的中转路由器)。
  接下来 m 行,每行包含两个整数 xi 和 yi,表示 (xi, yi) 点处可以增设 一个路由器。
  输入中所有的坐标的绝对值不超过 108,保证输入中的坐标各不相同。

输出格式

  输出只有一个数,即在指定的位置中增设 k 个路由器后,从第 1 个路 由器到第 2 个路由器最少经过的中转路由器的个数。

样例输入

5 3 1 3
0 0
5 5
0 3
0 5
3 5
3 3
4 4
3 0

样例输出

2

 

 

解题报告:

   也写了个建图版本的,但是模型建的不太对,会使得可以从虚拟建的回到已经建成的节点,然后再回第一层。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
const int MAX = 2e3 + 5;
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
int n,m,k;
ll r;
int dis[222][222];
int tot,head[MAX],vis[222][222];
struct Edge {
	int to,ne;
	int w;
} e[20005];//数组别开小了
void add(int u,int v,double w) {
	e[++tot].to = v;
	e[tot].w = w;
	e[tot].ne = head[u];
	head[u] = tot;
}
struct Point {
	ll x,y;
} p[205];
struct Node {
	int id,kk;
	Node() {}
	Node(int id,int kk):id(id),kk(kk) {}
};
bool spfa() {
	memset(dis,INF,sizeof dis);
	dis[1][0]=0;
	vis[1][0]=1;
	queue<Node > q;
	q.push(Node(1,0));
	while (!q.empty()) {
		Node cur=q.front();
		q.pop();
		vis[cur.id][cur.kk]=0;
		for(int i=head[cur.id]; i!=-1; i=e[i].ne) {
			int nowkk = cur.kk;
			if(e[i].to > n) nowkk++;
			if (dis[e[i].to][nowkk]>dis[cur.id][cur.kk]+e[i].w) {
				dis[e[i].to][nowkk] = dis[cur.id][cur.kk]+e[i].w;
				if (vis[e[i].to][nowkk]==0) {
					vis[e[i].to][nowkk]=1;
					q.push(Node(e[i].to,nowkk));
				}
			}
		}
	}
}

int main() {
	cin>>n>>m>>k>>r;
	memset(head,-1,sizeof head);
	for(int i = 1; i<=n+m; i++) {
		int u,v;
		scanf("%lld%lld",&p[i].x,&p[i].y);
		for(int j = 1; j<i; j++) {
			double dist = (p[i].x-p[j].x) * (p[i].x-p[j].x) + (p[i].y-p[j].y)*(p[i].y-p[j].y) ;
			if(dist <= r*r) add(i,j,1),add(j,i,1);
		}
	}
	spfa();
	int ans = INF;
	for(int i = 0; i<=k; i++) ans = min(ans,dis[2][i]);
	printf("%d\n",ans-1);
	return 0 ;
}
/*
5 3 1 3
0 0
5 5
0 3
0 5
3 5
3 3
4 4
3 0

*/