CCF计算机职业资格认证考试 201703-4 地铁修建 (BFS)

CCF前夕和实验室的沙雕小伙伴自发进行了一场沙雕比赛,先把答案写在txt文本里然后统一交,结果拿了300分,第三道题大模拟愣是一分没拿到。。。

第四道一开始看觉得最小生成树没法做(后来看网上题解好多最小生成树过了的),然后就想到了BFS,每次将当前最小权值的边压入优先队列,途中记录一下最大的边,直到走到终点。存图就是常见的vector里放pair。网上好象没看到我这种写法的,都是最小生成树或者最短路,所以把我的作法写下来咯。

题目链接

代码:

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

typedef long long ll;
#define int ll
#define INF 0x3f3f3f3f
#define MAXM 100000 + 10
#define MAXN 100000 + 10
const ll mod = 1e9 + 7;
#define fir first
#define sec second

int n, m;
vector<pair<int, int> > G[MAXN];
bool vis[MAXN];
struct node{
    int a, b;
    int dis;
    bool operator < (const node &temp) const {
        return dis > temp.dis;
    }
};

signed main()
{
    cin >> n >> m;
    for(int i = 0; i < m; i ++) {
        int a, b, x; cin >> a >> b >> x;
        pair<int, int> p(b, x);
        G[a].push_back(p);
        p.fir = a;
        G[b].push_back(p);
    }
    priority_queue<node> Q;
    node temp;
    for(int i = 0; i < G[1].size(); i ++) {
        temp.a = 1, temp.b = G[1][i].fir;
        temp.dis = G[1][i].sec;
        Q.push(temp);
    }
    vis[1] = true;

    int ans = 0;
    while(!Q.empty()) {
        node fro = Q.top();
        Q.pop();
        ans = max(ans, fro.dis);
        if(fro.b == n) {
            break;
        }
        vis[fro.b] = true;
        for(int i = 0; i < G[fro.b].size(); i ++) {
            if(!vis[G[fro.b][i].fir]) {
                temp.a = fro.b;
                temp.b = G[fro.b][i].fir;
                temp.dis = G[fro.b][i].sec;
                Q.push(temp);
            }
        }

    }
    cout << ans << endl;

    return 0;
}

/*

The WAM is F**KING interesting .

*/

猜你喜欢

转载自blog.csdn.net/ooB0Boo/article/details/88593617