OJ:GPLT L3-008 喊山 BFS模板题

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

输入格式:

输入第一行给出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即可,但是注意到里面其实有一个比较成分,无论怎么设计顺序也没法讨巧减少时间,索性就不剪枝了,同一层的节点都进入队列,比较就行了
  • 现在才发现邻接表是真的快,像这道题就是,用其他的数据结构会超时

代码

#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdio>
#include <bitset>
using namespace std;

const int N = 10000 + 3;
static int n, m, k;
static vector<int> v1[N];

int query(int index){
    int curr, clevel;
    int result = N, rlevel = -1;
    queue<int> q1, qlevel;
    bitset<N> isUsed;
    isUsed.reset();
    q1.push(index);
    qlevel.push(0);
    while(!q1.empty()){
        curr = q1.front(), clevel = qlevel.front();
        // cout << curr << " " << clevel << endl;
        q1.pop(); qlevel.pop();
        if(isUsed[curr]) continue;
        else isUsed[curr] = 1;
        if(clevel > rlevel || (clevel == rlevel && result > curr)){
            result = curr;
            rlevel = clevel;
        }
        for(int i=0; i<v1[curr].size(); i++){
            if(!isUsed[v1[curr][i]]){
                q1.push(v1[curr][i]);
                qlevel.push(clevel + 1);
            }
        }
    }
    return result;
}

int main(){
    cin >> n >> m >> k;
    for(int i=0; i<m; i++){
        int i1, i2;
        cin >> i1 >> i2;
        v1[i1].push_back(i2);
        v1[i2].push_back(i1);
    }
    for(int i=0; i<k; i++){
        // cout << "i = " << i << endl;
        int index; cin >> index;
        if(query(index) != index) cout << query(index) << endl;
        else cout << "0" << endl;
    }
    return 0;
}
这道目的大致思路如下: 1. 首先读入原有文件中的据,并将其保存到一个组中; 2. 读入要插入的据,并将其插入到组中相应的位置; 3. 将组中的据写回到原有文件中。 下面是一个可能的实现: ```c++ #include <iostream> #include <fstream> #include <vector> using namespace std; struct Record { int id; string name; int age; }; int main() { // 读入原有文件中的据 vector<Record> records; ifstream fin("data.txt"); if (fin.is_open()) { int id, age; string name; while (fin >> id >> name >> age) { records.push_back({id, name, age}); } fin.close(); } // 读入要插入的据 Record new_record; cin >> new_record.id >> new_record.name >> new_record.age; // 将新据插入到组中相应的位置 int pos = -1; for (int i = 0; i < records.size(); ++i) { if (records[i].id > new_record.id) { pos = i; break; } } if (pos == -1) { records.push_back(new_record); } else { records.insert(records.begin() + pos, new_record); } // 将组中的据写回到原有文件中 ofstream fout("data.txt"); if (fout.is_open()) { for (const auto& record : records) { fout << record.id << " " << record.name << " " << record.age << "\n"; } fout.close(); } return 0; } ``` 其中,我们定义了一个 `Record` 结构体来表示每一条记录,然后使用一个 `vector` 来保存所有的记录。在读入原有文件中的据时,我们使用了文件读取流 `ifstream`,在写回到文件中时,我们使用了文件写入流 `ofstream`。读入要插入的据时,我们直接使用标准输入流 `cin`。 在将新据插入到组中时,我们首先需要找到相应的位置。这里我们使用了一种简单的方法,即遍历组,找到第一个 ID 大于新据 ID 的位置,然后将新据插入到该位置。如果没有找到这样的位置,说明新据 ID 是最大的,我们将其追加到组末尾即可。在将新据插入到组中时,我们使用了 `vector` 的 `insert` 方法。 最后,我们将组中的据写回到原有文件中。在写回到文件中时,我们使用了 `ofstream` 的输出流运算符 `<<`。由于每条记录都需要以一行的形式写入文件,因此我们在输出时需要加上换行符 `\n`。 希望这个解答能够帮助到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值