[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 ...
随机推荐
- AJAX异步提交,浏览器总跳出下载界面
问题: 我在写一个网页的“用户登录”部分时,要将用户名和密码传到后端验证,想在前端用了AJAX异步提交功能,将 用户名密码传到后端,然后后端返回验证结果.但AJAX写好后每次刷新网页都会跳出下载窗口, ...
- .net postsharp编译时生成的代码?
使用PostSharp进行AOP框架设计:一个简单的原型 AOP已经不是一个什么新名词了,在博客园使用关键字搜索可以查出n多条关于AOP的介绍,这里就不再赘述了. 在Bruce Zhang's B ...
- LigerUI权限系统之用户管理
用户管理较之前的的组织结构和菜单管理稍显复杂.不管怎样还是先上图吧,再来讲解 左边是组织结构,右边是用户,用户是跟组织机构挂钩的,通过点击左边的组织结构,来刷新右边,加载该组织机构下的用户. 用户管理 ...
- elasticsearch data importing
ElasticSearch stores each piece of data in a document. That's what I need. Using the bulk API. Trans ...
- 通过jquery来实现文本框和下拉框动态添加效果,能根据自己的需求来自定义最多允许添加数量,实用的jquery动态添加文本框特效
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- grub 的安装与使用
安装与使用grub 要开始探究 GRUB 的精妙之处,首先需要下载.编译和安装它.但不要害怕 -- 根本不会修改您的引导记录 -- 我们只是要编译和安装 GRUB,就像其它程序一样,在此过程中我们可以 ...
- Ecshop出现问题 includes\lib_main.php on line 1329 includes\lib_base.php on line
php 5.3版本兼容问题不少,以上函数参数传递问题可以将lib_main.php on line 1329这句 $ext = end(explode('.', $tmp)); 改为 : $extsu ...
- aix 禁止root远程登录
Aix禁止root远程登录 aix用户扩展信息大都在/etc/security/user这个文本文件里.你可以修改: login=false 用户不能登录系统 rlogin=false 用户不能从远程 ...
- 酒店管理web项目总结
酒店管理web项目总结 半个月的努力,一个完整的酒店项目也就新鲜出炉了,在项目的制作中总结了一些酒店管理项目的特点. 1.需求分析,酒店管理需要什么? 1)首先系统的安全性,对于任何一个系统来说,安全 ...
- 背水一战 Windows 10 (39) - 控件(布局类): VariableSizedWrapGrid, Border, Viewbox, SplitView
[源码下载] 背水一战 Windows 10 (39) - 控件(布局类): VariableSizedWrapGrid, Border, Viewbox, SplitView 作者:webabcd ...