题目介绍:

输入一个无向图,指定一个顶点s开始bfs遍历,求出s到图中每个点的最短距离。

如果不存在s到t的路径,则记s到t的距离为-1。
 
Input

输入的第一行包含两个整数n和m,n是图的顶点数,m是边数。1<=n<=1000,0<=m<=10000。

以下m行,每行是一个数对v y,表示存在边(v,y)。顶点编号从1开始。 
 
Output

记s=1,在一行中依次输出:顶点1到s的最短距离,顶点2到s的最短距离,...,顶点n到s的最短距离。

每项输出之后加一个空格,包括最后一项。
 
Sample Input
5 3
1 2
1 3
2 4
Sample Output
0 1 1 2 -1 

思路:

利用广度搜索,标记层数,依次计算距离即可。

具体代码如下:

 #include <iostream>
#include <queue>
using namespace std; bool path[][];
int shortest[]; int main() {
int n, m;
cin >> n >> m; for (int i = ; i <= m; i++) {
int node1, node2;
cin >> node1 >> node2;
path[node1][node2] = true;
path[node2][node1] = true;
} for (int i = ; i <= n; i++)
i == ? shortest[i] = : shortest[i] = -; int distance = ;
queue<int> store;
store.push();
while (!store.empty()) {
int size = store.size();
distance++;
while (size--) {
for (int i = ; i <= n; i++) {
if (path[store.front()][i] && shortest[i] == -) {
shortest[i] = distance;
store.push(i);
}
}
store.pop();
}
} for (int i = ; i <= n; i++)
cout << shortest[i] << " ";
cout << endl; return ;
}

Sicily shortest path in unweighted graph的更多相关文章

  1. [SOJ] shortest path in unweighted graph

    Description 输入一个无向图,指定一个顶点s开始bfs遍历,求出s到图中每个点的最短距离. 如果不存在s到t的路径,则记s到t的距离为-1.   Input 输入的第一行包含两个整数n和m, ...

  2. HDU 4725 The Shortest Path in Nya Graph(构图)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  3. HDU 4725 The Shortest Path in Nya Graph (最短路)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  4. HDU4725:The Shortest Path in Nya Graph(最短路)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  5. Hdu 4725 The Shortest Path in Nya Graph (spfa)

    题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...

  6. HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]

    HDU - 4725 The Shortest Path in Nya Graph http://acm.hdu.edu.cn/showproblem.php?pid=4725 This is a v ...

  7. HDU 4725 The Shortest Path in Nya Graph

    he Shortest Path in Nya Graph Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged o ...

  8. HDU-4725 The Shortest Path in Nya Graph (拆点+dji)

    HDU 4725 The Shortest Path in Nya Graph : http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意: 在一个图中跑最 ...

  9. hdu 4725 The Shortest Path in Nya Graph (最短路+建图)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

随机推荐

  1. 构建你的第一个App

    Building Your First App 原文链接:http://developer.android.com/training/basics/firstapp/index.html 译文链接1: ...

  2. selenium webdriver python 开始

    学习资料: Selenium with Python: http://selenium-python.readthedocs.org/en/latest/index.html 乙醇的python se ...

  3. Ubuntu 用户及组管理

    $是普通管员,#是系统管理员,在Ubuntu下,root用户默认是没有密码的,因此也就无法使用(据说是为了安全).想用root的话,得给root用户设置一个密码:sudo passwd root然后登 ...

  4. Tomcat中server.xml配置及Context的配置说明

    server.xml配置简介         下面是这个文件中的基本配置信息,更具体的配置信息见tomcat的文档         server:         port     指定一个端口,这个 ...

  5. 学习微信小程序之css16常见布局

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. iOS--导航栏样式

    push返回按钮样式: UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBar ...

  7. Cocos2dx 多线程

    多-threaded负荷plist特征.获取知识的必要性: 1.多线程开启:pthread 2.怎样在线程中载入plist 一.多线程开启 当我们想在程序中开多线程中.第一想到的是cocos2d-x有 ...

  8. View原理

    View处理: 绘制(paint canvas path:tween等动画效果).事件处理   参考整理自: Custom Components: http://developer.android.c ...

  9. api接口

    目录(?)[-] 接口特点汇总 PHP Token令牌 先说第一个tokenapi_token 服务端接口校验PHP实现流程如下 再说第二个tokenuser_token 接口用例如下 接口特点汇总: ...

  10. JAVA彩色图片变灰处理

    File file = new File("F:/firefox.png"); File destFile = new File("F:/pic/" + Sys ...