试题编号: | 201403-4 |
试题名称: | 无线网络 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: | 问题描述 目前在一个很大的平面房间里有 n 个无线路由器,每个无线路由器都固定在某个点上。任何两个无线路由器只要距离不超过 r 就能互相建立网络连接。 输入格式 第一行包含四个正整数 n,m,k,r。(2 ≤ n ≤ 100,1 ≤ k ≤ m ≤ 100, 1 ≤ r ≤ 108)。 输出格式 输出只有一个数,即在指定的位置中增设 k 个路由器后,从第 1 个路 由器到第 2 个路由器最少经过的中转路由器的个数。 样例输入 5 3 1 3 样例输出 2 |
思路:
(1)可以看做最短路(加了一些限制),即有些点可能不是全部可用,可以用一个状态量记录准备更新新的点v时候,是否满足到达该点(包括该点)所经过的被限制的点的数目是否超过k,如果没有超过,说明可以到达这里,更新点d[v]否则不更新。
(2)可以用bfs做,在一个struct里面把到达目标点(2号点)的路径记录下来,然后判断里面被限制点的个数是否超过k,如果没有超过,则取其最小值。否则不考虑。
code(spfa):
#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<cstring>
#include<set>
#include<cmath>
#include<queue>
#include<algorithm>
#define rep(i,j,k) for(int i=j;i<k;++i)
#define INF 0x3f3f3f3f
#define mst(a,b) memset((a),(b),sizeof(a))
#include<cstring>
#define pb push_back
using namespace std;
typedef long long LL;
typedef vector<int,int> pii;
int n,m,k,r;
const int MAXN = 205;
struct Point{
LL x,y;
}pt[MAXN];
struct S{
int v,kcnt;
S(){}
S(int vv, int k):v(vv),kcnt(k){}
};
double distance(Point a, Point b){
return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
vector<int> G[MAXN];
bool vis[MAXN][MAXN];
int d[MAXN];
void spfa(int s){
rep(i,0,n+m+1) d[i] = (i == s ? 0 : INF);
mst(vis,false);
queue<S> q;
q.push(S(s,0));
S cur,tmp;
while(!q.empty()){
cur = q.front();
int u = cur.v;
q.pop();
vis[u][0] = false;
for(int i = 0; i < G[u].size(); ++i){
int v = G[u][i];
int kk = cur.kcnt;
if(v >= n) ++kk;
if(d[v] > d[u] + 1 && kk <= k){
d[v] = d[u] + 1;
if(!vis[v][kk]){
q.push(S(v,kk));
vis[v][kk] = true;
}
}
}
}
printf("%d\n",d[1]-1);
}
int main()
{
scanf("%d%d%d%d",&n,&m,&k,&r);
int cnt = 0;
int x,y;
rep(i,0,n+m){
scanf("%lld%lld",&pt[cnt].x,&pt[cnt].y);
++cnt;
}
rep(i,0,cnt){
rep(j,i+1,cnt){
if(distance(pt[i],pt[j]) <= 1.0*r){
G[i].pb(j);
G[j].pb(i);
}
}
}
spfa(0);
return 0;
}
bfs
这个bfs思路应该没问题但是只90,找不到原因。。。
code:
#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<cstring>
#include<set>
#include<cmath>
#include<queue>
#include<algorithm>
#define rep(i,j,k) for(int i=j;i<k;++i)
#define mst(a,b) memset((a),(b),sizeof(a))
#define pb push_back
#include<cstring>
using namespace std;
typedef long long LL;
typedef vector<int,int> pii;
const int MAXN = 205;
struct P{
LL x,y;
}pt[MAXN];
struct Path{
vector<int> pa;
Path(){}
Path(int u){
pa.clear();
pa.pb(u);
}
operator = (const Path obj){
pa = obj.pa;
}
};
vector<int> G[MAXN];
int n,m,k;
LL r;
bool vis[MAXN];
int mmin = 0x3f3f3f3f;
void out(vector<int> vec){
rep(i,0,vec.size()){
printf("%d ",vec[i]);
}
printf("\n");
}
void bfs(int s){
mst(vis,false);
queue<Path> q;
while(!q.empty()) q.pop();
q.push(Path(s));
Path cur,tmp;
while(!q.empty()){
cur = q.front(),q.pop();
//out(cur.pa);
int u = cur.pa[cur.pa.size()-1];
if(u == 2){
int ct = 0,sz = cur.pa.size();
rep(i,0,sz){
if(cur.pa[i] > n && cur.pa[i] <= m+n)
++ct;
}
if(ct <= k){
mmin = min(mmin,sz-2);
}
}
for(int i = 0; i < G[u].size(); ++i){
int v = G[u][i];
if(!vis[v]){
vis[v] = true;
tmp = cur;
tmp.pa.pb(v);
q.push(Path(tmp));
}
}
}
}
int main()
{
scanf("%d%d%d%lld",&n,&m,&k,&r);
int cnt = 1;
rep(i,0,n+m){
scanf("%lld%lld",&pt[cnt].x,&pt[cnt].y);
++cnt;
}
for(int i = 1; i < cnt; ++i){
for(int j = i+1; j < cnt; ++j){
LL x = pt[i].x-pt[j].x;
LL y = pt[i].y-pt[j].y;
if((LL)x*x + (LL)y*y <= LL(r*r)){
G[i].pb(j);
G[j].pb(i);
}
}
}
bfs(1);
printf("%d\n",mmin);
return 0;
}