[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 ...
随机推荐
- Sparse Filtering
Sparse Filtering 当前很多的特征学习(feature learning)算法需要很多的超参数(hyper-parameter)调节, Sparse Filtering则只需要一个超参数 ...
- Aliexpress API 测试工具
Aliexpress API 测试工具 上回简单说了 Aliexpress API 的认证流程, 这回在奉送一个小工具, API 测试工具. 点我下载 做这一行,和做程序员的生活完全不搭调, 格格不入 ...
- Excel 自定义函数
浅谈Excel开发:四 Excel 自定义函数 我们知道,Excel中有很多内置的函数,比如求和,求平均,字符串操作函数,金融函数等等.在有些时候,结合业务要求,这些函数可能不能满足我们的需求,比 ...
- iOS多线程的初步研究3
iOS多线程的初步研究(三) 弄清楚NSRunLoop确实需要花时间,这个类的概念和模式似乎是Apple的平台独有(iOS+MacOSX),很难彻底搞懂(iOS没开源,呜呜). 官网的解释是说run ...
- 设置 Ext.data.Store 传参的请求方式
设置 Ext.data.Store 传参的请求方式 1.extjs 给怎么给panel设背景色 设置bodyStyle:'background:#ffc;padding:10px;', var res ...
- iOS 开发问题集锦(一)
最近在QQ群里好多人问一些比较基础的问题,在这罗列一下: 1.键盘上的斜杠键坏掉了,怎么样用快捷键进行注释呢? 首先在Xcode中注释的默认快捷键为:command+/: 其次可以自行修改快捷键:Xc ...
- Mongodb 集群搭建以及常见错误(不分块,分片,以及加验证)
1 关于Replica Sets +Sharding(主从复制加分片)搭建,不这详细去说,网上有很多,大部分的例子就三台服务器之间做主从复制,分2个shard,架构图如下 mongodb节点 分别为 ...
- Dragon Balls(hdu3635带权并查集)
题意:n个城市有n个龙珠,T a,b 代表将龙珠a移动到b城市,Q a代表查询龙珠a所在城市,该城市有多少颗龙珠,该龙珠移动了多少次. 注意:移动时是将龙珠所在该城市所有龙珠都移动,每个龙珠不会再回 ...
- C#中WindowsForm常见控件的运用
C#中WindowsForm常见控件的运用 -- 1.button(曹操,贡天子以令不臣): 属性;text:我们经常可以看见将按钮命名为“登入”,在其属性面板里面编辑text即可:如下图: ...
- POJ 1915 经典马步 双向bfs
拿这个经典题目开刀...........可是双向时间优势在这题上的效果不太明显 #include <iostream> #include <algorithm> #includ ...