PTA天梯赛L3-008 喊山【最短路 BFS】

喊山,是人双手围在嘴边成喇叭状,对着远方高山发出“喂—喂喂—喂喂喂……”的呼唤。呼唤声通过空气的传递,回荡于深谷之间,传送到人们耳中,发出约定俗成的“讯号”,达到声讯传递交流的目的。原来它是彝族先民用来求援呼救的“讯号”,慢慢地人们在生活实践中发现了它的实用价值,便把它作为一种交流工具世代传袭使用。(图文摘自:http://news.xrxxw.com/newsshow-8018.html)

一个山头呼喊的声音可以被临近的山头同时听到。题目假设每个山头最多有两个能听到它的临近山头。给定任意一个发出原始信号的山头,本题请你找出这个信号最远能传达到的地方。

输入格式:

输入第一行给出3个正整数nmk,其中n(≤10000)是总的山头数(于是假设每个山头从1到n编号)。接下来的m行,每行给出2个不超过n的正整数,数字间用空格分开,分别代表可以听到彼此的两个山头的编号。这里保证每一对山头只被输入一次,不会有重复的关系输入。最后一行给出k(≤10)个不超过n的正整数,数字间用空格分开,代表需要查询的山头的编号。

输出格式:

依次对于输入中的每个被查询的山头,在一行中输出其发出的呼喊能够连锁传达到的最远的那个山头。注意:被输出的首先必须是被查询的个山头能连锁传到的。若这样的山头不只一个,则输出编号最小的那个。若此山头的呼喊无法传到任何其他山头,则输出0。

输入样例:

7 5 4
1 2
2 3
3 1
4 5
5 6
1 4 5 7

输出样例:

2
6
4
0

思路:就是让你求最长距离,用最短路和BFS都可以写。

最短路:用dijkstra算法加堆优化,nlogn的复杂度。

#include<set>
#include<map>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1e4 + 10;
struct node
{
    int v, c;
    bool operator < (const node &r) const
    {
        return c > r.c;
    }
};
struct Edge
{
    int v, cost;
};
vector<Edge> e[maxn];
bool vis[maxn];
int dist[maxn];
void Dijkstra(int n, int start)
{
    memset(vis, false, sizeof(vis));
    for(int i = 1; i <= n; ++i)
        dist[i] = inf;
    priority_queue<node> q;
    while(!q.empty())
        q.pop();
    dist[start] = 0;
    q.push(node{start, 0});
    node tmp;
    while(!q.empty())
    {
        tmp = q.top();
        q.pop();
        int u = tmp.v;
        if(vis[u]) continue;
        vis[u] = true;
        for(int i = 0; i < e[u].size(); ++i)
        {
            int v = e[u][i].v;
            int cost = e[u][i].cost;
            if(!vis[v] && dist[v] > dist[u] + cost)
            {
                dist[v] = dist[u] + cost;
                q.push(node{v, dist[v]});
            }
        }
    }
}
void addedge(int u, int v, int w)
{
    e[u].push_back(Edge{v, w});
}

int main()
{
    int n, m, k;
    cin >> n >> m >> k;
    for(int i = 0; i < maxn; ++i)
        e[i].clear();
    int u, v, w = 1;
    for(int i = 0; i < m; ++i)
    {
        cin >> u >> v;
        addedge(u, v, w);
        addedge(v, u, w);
    }
    int s;
    for(int j = 0; j < k; ++j)
    {
        cin >> s;
        Dijkstra(n, s);
        int ans = 0, pos = 0;
        for(int i = 1; i <= n; ++i)
        {
            if(dist[i] > ans && dist[i] <= n)
            {
                ans = dist[i];
                pos = i;
            }
        }
        cout << pos << endl;
    }
    return 0;
}

BFS:用一个结构体数组来记录 节点和步数,每次读取周围一层的节点,记录结果就行了。

#include<set>
#include<map>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1e4 + 10;
struct node
{
    int v, step;
};
vector<int> ve[maxn];
bool vis[maxn];
int bfs(int x)
{
    if(ve[x].size() == 0)
        return 0;
    memset(vis, false, sizeof(vis));
    node t;
    t.v = x, t.step = 0;
    vis[x] = true;
    queue<node> q;
    q.push(t);
    while(!q.empty())
    {
        node tmp = q.front();
        q.pop();
        if(tmp.step > t.step || (tmp.step == t.step && tmp.v < t.v))
        {
            t.step = tmp.step;
            t.v = tmp.v;
        }
        for(int i = 0; i < ve[tmp.v].size(); ++i)
        {
            if(!vis[ve[tmp.v][i]])
            {
                node now;
                now.step = tmp.step + 1;
                now.v = ve[tmp.v][i];
                q.push(now);
                vis[now.v] = true;
            }
        }
    }
    return t.v;
}

int main()
{
    int n, m, k;
    cin >> n >> m >> k;
    int u, v;
    for(int i = 0; i < m; ++i)
    {
        cin >> u >> v;
        ve[u].push_back(v);
        ve[v].push_back(u);
    }
    int s;
    for(int j = 0; j < k; ++j)
    {
        cin >> s;
        int pos = bfs(s);
        cout << pos << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41785863/article/details/88650527