[SOJ] 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的最短距离。
Copy sample input to clipboard5 3
1 2
1 3
2 4
0 1 1 2 -1
//Dijiskra algorithm
#include<iostream>
#include<memory>
#include<algorithm>
using namespace std; const int MAX = 1005;
const int INF = 0x3f3f3f3f; int n, m;
int d[MAX];
bool v[MAX];
int edge[MAX][MAX]; void Dijiskra(int start)
{
//initial
memset(v, false, sizeof(v));
for(int i=1;i<MAX;i++)
d[i]=INF; //start at node 'Start'
d[start]=0;
for(int i=1;i<=n;i++)
{
int temp;
int minx = INF;
for(int j=1;j<=n;j++)
{
if(!v[j]&&d[j]<minx)
{
temp=j;
minx=d[j];
}
}
v[temp]=true; //refresh distance array
for(int k=1;k<=n;k++)
d[k]=min(d[k], d[temp]+edge[temp][k]);
}
} int main()
{
cin>>n>>m;
memset(edge, INF, sizeof(edge));
for(int i=0;i<m;i++)
{
int a, b;
cin>>a>>b;
edge[a][b]=1;
edge[b][a]=1;
} Dijiskra(1); for(int i=1;i<=n;i++)
{
if(d[i]==INF)cout<<-1<<" ";
else cout<<d[i]<<" ";
}
cout<<endl; return 0;
}
[SOJ] shortest path in unweighted graph的更多相关文章
- Sicily shortest path in unweighted graph
题目介绍: 输入一个无向图,指定一个顶点s开始bfs遍历,求出s到图中每个点的最短距离. 如果不存在s到t的路径,则记s到t的距离为-1. Input 输入的第一行包含两个整数n和m,n是图的顶点 ...
- 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 ...
随机推荐
- [原创]opencv实现图像拼接,制做全景图
转载请注明:http://www.cnblogs.com/ausk/p/3332255.html 调用opencv2.4.6中的库函数,实现图像的拼接功能,傻瓜式拼接,不需要太多的专业知识.. ...
- c:forEach 实现换行
Map<String,String> map = new TreeMap<String,String>(); map.put("key1", "v ...
- ASP.NET MVC C#知识点提要
ASP.NET MVC C#知识点提要 本篇博文主要对asp.net mvc开发需要撑握的C#语言知识点进行简单回顾,尤其是C# 3.0才有的一些C#语言特性.对于正在学asp.net mvc的童鞋, ...
- mosquitto MQTT message broker on ubuntu/debian built from source
what is mosquitto? check this out! http://mosquitto.org what is MQTT? check this out. http://mqtt.or ...
- QT 菜单程序
MainWindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include<QMainWindow> #include<QMess ...
- 浅谈java垃圾回收机制
今天看thinking in java,里面很详细的谈到java垃圾回收器机制,看完后让我对这神秘的区域有一定的了解,特写一些小总结记录下来. 分两点来说. 第一点:Object.finalize() ...
- js处理层级数据结构的一些总结
开发者对复杂的数据结构的处理能力也是体现开发者水平的一个度量吧...最近发现自己对一些嵌套数据结构.层级数据结构的处理能力不大足...经常被这些把自己绕晕...严重影响开发效率...就稍微低总结了一下 ...
- jQuery Ajax用法
jQuery Ajax在web应用开发中很常用,它主要包括有ajax,get,post,load,getscript等等这几种常用无刷新操作方法,下面我来给各位同学介绍介绍. 我们先从最简单的方法看起 ...
- 初次配置eclipse, jdk, tomcat, maven, mysql, alt+/
eclipse 官网下载eclipse-inst-win64.exe, 选择安装java ee. jdk 官网下载jdk-8u102-windows-x64.exe, next到底. 接下来配置环境变 ...
- 有关css3的一些问题
CSS3新特性(阴影.动画.渐变.变形.伪元素等) CSS3与页面布局学习总结--CSS3新特性(阴影.动画.渐变.变形.伪元素等) 目录 一.阴影 1.1.文字阴影 1.2.盒子阴影 二.背 ...