[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 ...
随机推荐
- Discuz! X2.5数据库字典(转)
DROP TABLE IF EXISTS pre_common_admincp_cmenu; CREATE TABLE pre_common_admincp_cmenu ( `id` SMALLINT ...
- c语言:将二进制数按位输出
问题: 1.输入int 20,其二进制为10100,按位输出10100; 2.或者将1转化为“+”,0转化为“-”,输出就是” + - + - - “; int biTofh(int bi,int l ...
- 使用STL处理分支限界法处理最优装载问题
使用STL处理分支限界法处理最优装载问题 #include <iostream>#include <vector>#include <queue>#include ...
- jQuery $.ajaxSend()
语法: $("#msg").ajaxSend(function(evt,request,settings){}); AJAX请求发送前执行函数.Ajax事件. XMLHttpReq ...
- iOS app 发布错误 ERROR ITMS-90167: "No .app bundles found in the package"
今天iOS 上传 APP 突然发生了这个错误,在排查非证书错误后感到非常奇怪, 因为昨天刚刚上传了另一个APP,一切正常. 仔细回忆了下 昨天和今天唯一的不同就是我升级了 电脑操作系统至 macOS ...
- 对C# 构造函数的理解
C#构造函数是在创建给定类型的对象时执行的类方法. 构造函数具有与类相同的名称,它通常初始化新对象的数据成员.不带参数的构造函数称为“默认构造函数”. 无论何时,只要使用 new 运算符实例化对象,并 ...
- MFC 数据库编程 增删改查的一个例子
1.先看下效果图: 主界面: 一个最简单的1对多的表,一张Article(文章)表,一张Category(类别)表.有添加,修改,删除文章按钮.点击类别编辑按钮就会跳到第二个对话框.点击数据库配置就会 ...
- Wcf 文件上传下载
wcf 文件上传的例子网上很多,我也是借鉴别人的示例.wcf 文件下载的示例网上就很少了,不知道是不是因为两者的处理方式比较类似,别人就没有再上传了.在此本人做下记录备忘. UploadFile.sv ...
- java 逆波兰表达式
最近想把这破机 装成WIN7 想想还是算了 ... 反正用的机会也不多. 不过 发现了一些 想念的东西 从前的作业. 从前的记忆. package org.lmz; import java.util ...
- C++ 头文件系列(array)
注意,该头文件仅在C++11中标准才开始出现. 简介 与语言内置的数组一样, array类模版支持几乎所有内置数组包含的特性: 顺序的(sequence) 内存连续的(contiguous stora ...