h题
Weird Flecks, But OK
https://ac.nowcoder.com/acm/contest/12606/A
求1到n的最短路减一
#include"bits/stdc++.h" using namespace std; int n,m; struct node{ int v,w; }; struct edge{ int id,num; friend bool operator <(edge a,edge b){ return a.num>b.num; } }; vector<node>v[100005]; priority_queue<edge>q; int visit[100005],dis[100005]; const int INF=0x3f3f3f3f; void dijk(int x){ dis[x]=0; q.push(edge{x,0}); while(!q.empty()){ edge top=q.top(); q.pop(); if(visit[top.id])continue; visit[top.id]=1; for(int i=0;i<v[top.id].size();i++){ int dv=v[top.id][i].v; int dw=v[top.id][i].w; if(dis[dv]>dis[top.id]+dw){ dis[dv]=dis[top.id]+dw; q.push(edge{dv,dis[dv]}); } } } } int main(){ cin>>n>>m; for(int i=1;i<=m;i++){ int a,b; cin>>a>>b; v[a].push_back(node{b,1}); v[b].push_back(node{a,1}); } memset(dis,INF,sizeof(dis)); dijk(1); cout<<dis[n]-1; return 0; }