欧美1区2区3区激情无套,两个女人互添下身视频在线观看,久久av无码精品人妻系列,久久精品噜噜噜成人,末发育娇小性色xxxx

堆優(yōu)化dijkstra

【模板】單源最短路1

http://fangfengwang8.cn/practice/f24504c9ff544f7c808f76693cc34af8

#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
typedef struct node{
    int x, w;
    bool operator <(const struct node o) const{
        return w>o.w;
    }
}N;
int n, m, dis[5001];
vector<N> g[5001];
void dijkstra(int x){
    bool vis[5001];
    memset(vis, false, sizeof(vis));
    memset(dis, 0x7f, sizeof(dis));
    priority_queue<N> q;
    dis[x] = 0;
    q.push((N){x, 0});
    while(!q.empty()){
        N t = q.top();
        q.pop();
        for(int i=0; i<g[t.x].size(); i++){
            if(dis[t.x]+1<dis[g[t.x][i].x]){
                dis[g[t.x][i].x] = dis[t.x]+1;
                q.push((N){g[t.x][i].x, dis[g[t.x][i].x]});
            }
        }
    }
}
int main(){
    scanf("%d%d", &n, &m);
    for(int i=0; i<m; i++){
        int u, v;
        scanf("%d%d", &u, &v);
        g[u].push_back((N){v, 1});
        g[v].push_back((N){u, 1});
    }
    dijkstra(1);
    if(dis[n] == 0x7f7f7f7f)
        cout<<-1;
    else
        cout<<dis[n];
    return 0;
}

全部評(píng)論

相關(guān)推薦

評(píng)論
點(diǎn)贊
1
分享

創(chuàng)作者周榜

更多
??途W(wǎng)
??推髽I(yè)服務(wù)