image.png

题目链接

链接

solution

这道题也没啥难点,纯属脑残。就是BFS搜索就行了。注意搜索的时候维护一下状态就行。

#include<map>
#include<set>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<bitset>
#include<cstdio>
#include<cstring>
#include<fstream>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<unordered_map>

using namespace std;
#define x first
#define y second
#define endl '\n'
#define IOS                       \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
typedef long long ll;
typedef pair<int,int> PII;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f, mod = 1000000007;
const int N = 2e5 + 10;
vector<PII> g[N];
int dist[N], cnt[N];
void spfa(){
    queue<PII> q;
    memset(dist, 0x3f, sizeof dist);
    memset(cnt, 0x3f, sizeof cnt);
    dist[1] = 0;
    cnt[1] = 1;
    q.push({1, 0});
    while(q.size()){
        auto t = q.front();
        q.pop();
        for(auto [x, y] : g[t.x]){
            if(cnt[x] >= cnt[t.x] + 1){
                cnt[x] = cnt[t.x] + 1;
                if(dist[x] > dist[t.x] + y){
                    dist[x] = dist[t.x] + y;
                    q.push({x, dist[x]});
                }
            }
        }
    }
}

void solve(){
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= m; i++){
        int a, b, c;
        cin >> a >> b >> c;
        g[a].push_back({b, c});
        g[b].push_back({a, c});
    }
    spfa();
    cout << cnt[n] << " " << dist[n] << endl;
}
int main(){
    // IOS;
    int t = 1;
    // cin >> t;
    while(t--){
        solve();
    }
    return 0;
}
最后修改:2023 年 03 月 14 日
如果觉得我的文章对你有用,请随意赞赏