Sicily shortest path in unweighted graph
题目介绍:
输入一个无向图,指定一个顶点s开始bfs遍历,求出s到图中每个点的最短距离。
输入的第一行包含两个整数n和m,n是图的顶点数,m是边数。1<=n<=1000,0<=m<=10000。
记s=1,在一行中依次输出:顶点1到s的最短距离,顶点2到s的最短距离,...,顶点n到s的最短距离。
5 3
1 2
1 3
2 4
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的更多相关文章
- [SOJ] shortest path in unweighted graph
Description 输入一个无向图,指定一个顶点s开始bfs遍历,求出s到图中每个点的最短距离. 如果不存在s到t的路径,则记s到t的距离为-1. Input 输入的第一行包含两个整数n和m, ...
- 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 ...
- 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 ...
- 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 ...
- Hdu 4725 The Shortest Path in Nya Graph (spfa)
题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...
- 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 ...
- 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 ...
- 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 题意: 在一个图中跑最 ...
- 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 ...
随机推荐
- Linux下的memset函数
函数原型 void *memset(void *s, int c, size_t n); 函数功能 将以s为首的存储空间前n字节空间全部替换为参数c指定的数据. 返回值 更新后的首地址s. 头文件 # ...
- Android新浪微博客户端(六)——Home界面的ListView
原文出自:方杰|http://fangjie.info/?p=184转载请注明出处 最终效果演示:http://fangjie.info/?page_id=54该项目代码已经放到github:http ...
- 支付宝开通海外退税 阿里腾讯暗战跨境O2O_21世纪网
支付宝开通海外退税 阿里腾讯暗战跨境O2O_21世纪网 支付宝开通海外退税 阿里腾讯暗战跨境O2O
- ip地址中的网络号,主机号
当前使用的IP地址有4个字节(32bit)组成,即IPV4编码方式.每个IP地址包括两部分:网络号和主机号.当分配给主机号的二进制位越多,则能标识的主机数就越多,相应地能标识的网络数就越少,反之同理. ...
- @RestController
/* * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Vers ...
- DataGuard体系结构
一.DataGuard总体结构 总体目标 1. 描述计划和非计划停机的不同因数 2. DataGuard的主要组件 3. 物理以及逻辑DataGuard的异同 4. 建立DataGua ...
- JNI调用native方法出现 java.lang.UnsatisfiedLinkError: XXXclass.XXXmethod()异常的解决办法
昨天拿到JNI的Android工程Demo,然后把demo整合到开发的主线工程上,发现调用JNI方法一直抛同一个异常 java.lang.UnsatisfiedLinkError: XXXclass. ...
- JAVA-1-学习历程1:基础知识1
前言:此文属于个人学习中总结记录的较重要的知识点,分享一下.望对刚開始学习的人有点用. 视频04 语句.函数.数组 1.函数的重载 2.数组内存空间的划分 栈.堆 视频05 数组 1. ...
- Android - Service启动机制
以下资料摘录整理自老罗的Android之旅博客,是对老罗的博客关于Android底层原理的一个抽象的知识概括总结(如有错误欢迎指出)(侵删):http://blog.csdn.net/luoshe ...
- 配置ISCSI服务器
一.在linux下安装启动iscsi target 1.安装启动iscsi服务 [root@wjb10000 ~]# yum -y install targetcli.noarch 2.建立一个目录设 ...