ccf 201403-4 无线网络(100分)

问题描述

  目前在一个很大的平面房间里有 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
先判断哪些点可以直接连接距离置1,不能连接置正无穷,再用floyd算法算出1到2点的最短距离,用sum数组来存放各个点之前所用新增路由器的个数,可是后来发现这样做会封锁一些路径 。得100分不等于程序正确 例如:
10 4 2 1
0 0
5 0
0 1
0 2
0 3
1 3
3 0
3 1
3 2
3 3
1 0
2 0
2 3
4 0
应该输出10,可以下面程序输出正无穷。。。
提交后得100分的C++程序如下(有BUG):

#include<iostream>
#include<string>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 210;
const int inf = 0x3f3f3f3f;
int connect[maxn][maxn]; //如果两个路由器之间能建立连接则置1,否则置0
int n, m, k;
typedef long long ll;
ll r;
int visit[maxn],dist[maxn]; //距离源点的最短距离
struct node1
{
    ll x, y;
    node1(ll x1,ll y1)
    {
        x = x1;
        y = y1;
    }
    node1()
    {
    }
}no[maxn];
void spfa()
{
    int max1,sum[maxn];//sum[i]表示在i之前所用新增路由器的个数
    memset(sum, 0, sizeof(sum));
    queue<int> q;
    for (int i = 1; i <= n + m; i++)
    {
        dist[i] = inf, visit[i] = 0;
    }
    q.push(1);
    visit[1] = 1, dist[1] = 0;
    while (!q.empty())
    {
        int front = q.front();
        q.pop(), visit[front] = 0;
        if (sum[front] == k&&front > n) continue;
        if (sum[front] == k&&front<=n) max1 = n;
        else if (sum[front] == k - 1 && front > n) max1 = n;
        else  max1 = n+m;
        for (int i = 1; i <= max1; i++)
        {
            if (i == front) continue;
            if (connect[front][i] && dist[front] + 1 < dist[i])
            {
                dist[i] = dist[front] + 1;
                if (front > n) sum[i]=sum[front]+1; //如果front是新增的路由器则在原来的基础上加1
                else sum[i] = sum[front];
                if (visit[i] == 0)
                {
                    q.push(i);
                    visit[i] = 1;
                }
            }
        }
    }
}
int main()
{
    cin >> n >> m >> k >> r;
    memset(connect, 0, sizeof(connect));
    for (int i = 1; i <= n+m; i++)
    {
        cin >> no[i].x >> no[i].y;
    }
    for (int i = 1; i <= n + m - 1; i++)
    {
        for (int j = i + 1; j <= n + m; j++)
        {
            if ((no[i].x - no[j].x)*(no[i].x - no[j].x) + (no[i].y - no[j].y)*(no[i].y - no[j].y)<=r*r)
            {
                connect[i][j] = 1;
                connect[j][i] = 1;
            }
        }
    }
    spfa();
    cout << dist[2] - 1 << endl;
    return 0;
}

提交后得100分的C++程序如下(修改后):

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 210;
int connect[maxn][maxn]; //如果两个路由器之间能建立连接则置1,否则置0
int n, m, k;
typedef long long ll;
ll r;
int visit[maxn],sum[maxn];//sum[i]代表在i之前经过几个新的路由器
struct node1
{
    ll x, y;
    node1(ll x1,ll y1)
    {
        x = x1;
        y = y1;
    }
    node1()
    {
    }
}no[maxn];
struct move1
{
    int no,new1, step;
    move1(int n, int ne,int s)
    {
        no = n, new1=ne,step = s;
    }
};
int bfs()
{
    queue<move1> q;
    q.push(move1(1, 0,0));
    visit[1] = 1;
    while (!q.empty())
    {
        move1 front = q.front();
        q.pop();
        if (front.no == 2)
        {
            return front.step - 1;
        }
        for (int i = 1; i <= m + n; i++)
        {
            if (connect[front.no][i] == 0)
            {
                continue;
            }
            if ((visit[i] == 1 && front.new1 >= sum[i]))//避免路径封锁
            {
                continue;
            }
                visit[i] = 1;
                sum[i] = front.new1;
                int kk;
                if (i > n) kk = front.new1 + 1;
                else kk = front.new1;
                if (kk <= k)
                {
                    q.push(move1(i, kk, front.step + 1));
                }           
        }
    }
    return 0;
}
int main()
{
    cin >> n >> m >> k >> r;
    memset(connect, 0, sizeof(connect));
    memset(visit, 0, sizeof(visit));
    for (int i = 1; i <= n+m; i++)
    {
        cin >> no[i].x >> no[i].y;
    }
    for (int i = 1; i <= n + m - 1; i++)
    {
        for (int j = i + 1; j <= n + m; j++)
        {
            if ((no[i].x - no[j].x)*(no[i].x - no[j].x) + (no[i].y - no[j].y)*(no[i].y - no[j].y)<=r*r)
            {
                connect[i][j] = 1;
                connect[j][i] = 1;
            }
        }
    }
    int ans = bfs();
    cout << ans << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jinduo16/article/details/82191785