问题 B: 最优乘车
#include<bits/stdc++.h> using namespace std; int m,n,vis[501]; vector<int> g[501];int st; void bfs(){ queue<int> q; while(!q.empty())q.pop(); q.push(1); vis[1]=0; while(!q.empty()){ int h=q.front();q.pop(); for(int i=0;i<g[h].size();i++){ if(vis[g[h][i]]==0){ q.push(g[h][i]); vis[g[h][i]]=vis[h]+1; } if(g[h][i]==m){ cout<<vis[m]-1; exit(0); } } } cout<<"NO"; } int main(){ char ch;int a[505]; cin>>n>>m; ch=getchar(); for(int i=1;i<=n;i++) { int cnt=0; cin>>a[++cnt]; ch=getchar(); while(ch==' '){ cin>>a[++cnt]; ch=getchar(); } for(int i=1;i<=cnt;i++){ for(int j=i+1;j<=cnt;j++){ g[a[i]].push_back(a[j]); } } } bfs(); return 0; }
标签:26,ch,int,pop,vis,while,getchar From: https://www.cnblogs.com/zangqy/p/17582883.html