hdu1874 (spfa 最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874
很简单的最短路问题,刚刚学习spfa,其实很简单,思想和一维动态规划差不多,数组d[i]表示起点s到i的最短距离,不断用bfs更新这个距离就行,如果终点为t,那么最终d[t]就是起点s到t的最短路。d[i] = min(d[i] ,d[j] + e(i , j) ) ; e(i , j)是 i 点到 j点的边权。
该题AC代码:
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
struct node{
vector<pair<int,int> > vex;
}g[205];
int inq[205],d[205];
int main(){
int n,m;
while(cin>>n>>m){
for(int i = 0;i<205;i++){
g[i].vex.clear() ;
inq[i] = 0;
d[i] = 1e9;
}
for(int i = 0;i<m;i++){
int a,b,x;
cin>>a>>b>>x;
g[a].vex.push_back(make_pair(b,x));
g[b].vex.push_back(make_pair(a,x));
}
queue<int> q;
int s,t;
cin>>s>>t;
q.push(s);
d[s] = 0;
inq[s] = 1;
while(!q.empty() ){
int now = q.front();
q.pop() ;
inq[now] = 0;
for(int i = 0;i<g[now].vex.size() ;i++ ){
int v = g[now].vex[i].first;
if(d[v] > d[now] + g[now].vex[i].second ){
d[v] = d[now] + g[now].vex[i].second;
if(inq[v] == 1){
continue;
}
inq[v] == 1;
q.push(v);
}
}
}
if(d[t]==1e9){
cout<<-1<<endl;
}
else{
cout<<d[t]<<endl;
}
}
return 0;
}
hdu1874 (spfa 最短路)的更多相关文章
- NOIP2013 华容道 (棋盘建图+spfa最短路)
#include <cstdio> #include <algorithm> #include <cstring> #include <queue> # ...
- poj1502 spfa最短路
//Accepted 320 KB 16 ms //有n个顶点,边权用A表示 //给出下三角矩阵,求从一号顶点出发到各点的最短路的最大值 #include <cstdio> #includ ...
- hiho(1081),SPFA最短路,(非主流写法)
题目链接:http://hihocoder.com/problemset/problem/1081 SPFA求最短路,是不应-羁绊大神教我的,附上头像. 我第一次写SPFA,我用的vector存邻接表 ...
- hdu 5545 The Battle of Guandu spfa最短路
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5545 题意:有N个村庄, M 个战场: $ 1 <=N,M <= 10^5 $; 其中曹 ...
- HNU 13375 Flowery Trails (spfa最短路)
求最短路径覆盖的全部边权值和. 思路:分别从起点和终点两次求最短路,再比较两个点到起点的距离和他们之间的权值相加和是否等于最短路径. 这题很好 #include <cstring> #in ...
- poj 1847 Tram【spfa最短路】
Tram Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12005 Accepted: 4365 Description ...
- BZOJ 1003 物流运输 (动态规划 SPFA 最短路)
1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 5590 Solved: 2293 [Submit][Stat ...
- POJ - 1062(昂贵的聘礼)(有限制的spfa最短路)
题意:...中文题... 昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 54350 Accepted: 16 ...
- POJ 1556 - The Doors - [平面几何+建图spfa最短路]
题目链接:http://poj.org/problem?id=1556 Time Limit: 1000MS Memory Limit: 10000K Description You are to f ...
随机推荐
- openssl 生成免费证书
原文链接:https://www.cnblogs.com/tugenhua0707/p/10927722.html 一:什么是openssl? 它的作用是?应用场景是什么? 即百度百科说:openss ...
- centos7最小版配置
配置启用dns cd /etc/sysconfig/network-scripts/ vi ifcfg-ens33 # 修改ONBOOT为yes ONBOOT=yes 重启系统 reboot 安装ne ...
- linux-crond_计划任务
定时计划任务 主要文件介绍: [root@nginx ~]# ll /etc/cron* -d drwxr-xr-x. 2 root root 21 7月 11 20:28 /etc/cron.d d ...
- Hdu2097 Sky数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2097 Problem Description Sky从小喜欢奇特的东西,而且天生对数字特别敏感,一次偶 ...
- 杭电oj_2047——阿牛的EOF牛肉串(java实现)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2047 思路:先是列出了四个,但是没发现规律,然后开始画递归树,在其中找到了规律,算出递归式为f(n) ...
- 如何在macOS下安装geoserver
macOS 下的编译包 如果是使用安装文件,请查看官网文档,如果想要部署在已有的tomcat服务下,请查看网页压缩包章节. Web archive. An alternate way of insta ...
- linux - mysql 异常:Ignoring query to other database
问题描述 Ignoring query to other database(忽略其他数据库查询) 问题原因 登录方式错误,登录命令用的是 “mysql -root -p”,应该用命令 “mysql - ...
- linux - 查看 python 版本
命令 python -V 结果
- 超简单的OpenGL & WebGL & Three.js介绍_1
专业解释 什么是OpenGL OpenGL(Open Graphics Library即开放图形库或者“开放式图形库”)是用于渲染2D.3D矢量图形的跨语言.跨平台的应用程序编程接口(API). 这个 ...
- JDBC用户访问被拒绝
线程“主”java中的异常.于sq1.sQLException:用户“root”@“localhost”被拒绝访问(使用密码:YES)root密码错误