2016/11/24 1002.shortest path in unweighted graph
来源:互联网 发布:广州软件开发公司排名 编辑:程序博客网 时间:2024/05/16 08:37
本质上是需要做一个分层的广度优先搜索,只需要对每一个从队列中取出来的节点进行广度优先前用一个数记录接下来的多少个节点是属于同一层的,给相应的变量赋值即可。
#include <iostream>#include <list>using namespace std;int main(){int n, m;cin >> n >> m;bool** road = new bool*[n];bool* visit = new bool[n];int* ans = new int[n];list <int> l;for (int i = 0; i <= n - 1; i++){road[i] = new bool[n];visit[i] = false;ans[i] = -1;for (int j = 0; j <= n - 1; j++){road[i][j] = false;}}for (int i = 0; i <= m - 1; i++){int x, y;cin >> x >> y;road[x-1][y-1] = true;road[y-1][x-1] = true;}int start = 0;l.push_back(0);visit[0] = true;int count =0;ans[0] = 0;while(!l.empty()){count++;int j = l.size();while (j--){start = l.front();l.pop_front();for (int i = 0; i <= n - 1; i++){if (road[start][i] && !visit[i]){ans[i] = count;visit[i] = true;l.push_back(i);}}}}for (int i = 0; i <= n - 1; i++){cout << ans[i] << " ";}cout << endl;}
0 0
- 2016/11/24 1002.shortest path in unweighted graph
- Sicily 4376. shortest path in unweighted graph
- Sicily Shortest path in unweighted graph
- hdu4725 The Shortest Path in Nya Graph
- hdu4725 The Shortest Path in Nya Graph
- The Shortest Path in Nya Graph
- HDU4725-The Shortest Path in Nya Graph
- The Shortest Path in Nya Graph
- The Shortest Path in Nya Graph HDU
- The Shortest Path in Nya Graph HDU
- hdu 4725 The Shortest Path in Nya Graph
- HDU-4725-The Shortest Path in Nya Graph
- HDU 4725 The Shortest Path in Nya Graph
- HDU 4725 The Shortest Path in Nya Graph
- hdu 4725 The Shortest Path in Nya Graph
- HDU 1009 The Shortest Path in Nya Graph
- HDU 4725 The Shortest Path in Nya Graph priority_queue + spfa
- HDU 4725 The Shortest Path in Nya Graph
- Jmeter组件执行顺序与作用域
- 第十四周2
- js下拉触发
- 看起来很舒心的高性能线性代数运算库在线文档
- iOS——AFN监控网络
- 2016/11/24 1002.shortest path in unweighted graph
- 为什么你做事总是“三分钟热度”、“半途而废”?
- 开发工具注册码
- 程序员,你为什么值这么多钱?
- 安卓APP装机量统计相关
- ActionBar的Fragment
- redis与jedis
- php进行支付宝开发中return_url和notify_url的区别分析
- VC++ 操作ftp http接口