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 ...
随机推荐
- vue router的嵌套使用与传值的query方式
嵌套路由 当我们不满足与 /home这种路由,而是希望通过 /home/news和/home/message访问一些内内容 那么就需要嵌套路由了 实现嵌套路由有两个步骤: ·创建对应的子组件,并且在路 ...
- gtid环境下mysqldump对于set-gtid-purged的取值
gtid环境备份的时候,还在为set-gtid-purged=0|1的选择而烦恼吗,一起来分析一下. [mysql@lxd-vm1@/home/mysql]$ mysqldump --help | g ...
- CSS:display:flex详解
水平居中很容易实现,但是一般垂直居中好像不是很好实现,一般我们都会用position.left等等进行定位:但是flex很好的解决了这个问题 Flex就是"弹性布局",现在应用很多 ...
- java面试记录二:spring加载流程、springmvc请求流程、spring事务失效、synchronized和volatile、JMM和JVM模型、二分查找的实现、垃圾收集器、控制台顺序打印ABC的三种线程实现
注:部分答案引用网络文章 简答题 1.Spring项目启动后的加载流程 (1)使用spring框架的web项目,在tomcat下,是根据web.xml来启动的.web.xml中负责配置启动spring ...
- 【分享】一款特别轻量的gif生成工具
github链接:https://github.com/NickeManarin/ScreenToGif/releases/tag/2.19.3 超级好用!支持多种方式(摄像头也可√)录制gif
- HTTPClient模拟Get和Post请求
一.模拟Get请求(无参) 首先导入HttpClient依赖 <dependency> <groupId>org.apache.httpcomponents</group ...
- 关于Oracle内存分配-解决实际运行时最大Session数不一致远小于系统配置最大的Session数目
一.相关的技术准备 1. 关于内存的介绍:https://blog.csdn.net/u013641333/article/details/82732526 2. PGA_AGGREGATE_TARG ...
- linux - python:卸载
[root@test ~]# rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps ##强制删除已安装程序及其关联[root@test ~]# ...
- Alan Walker MV 合辑01 by defender
Alan Walker MV合辑 出来啦! 百度网盘下载地址: 链接:https://pan.baidu.com/s/10WSool70XBe_8tJOae8V-w 提取码:uckq 地址查看 Mi ...
- IDEA构建maven项目生成的文件详解
IDEA构建的maven+springBoot项目结构如下: 1. .gitignore:分布式版本控制系统git的配置文件,意思为忽略提交 在 .gitingore 文件中,遵循相应的语法,即在每一 ...