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 ...
随机推荐
- 小白月赛22 B : 树上子链
B:树上子链 考察点 : 树的直径 坑点 : long long, 是点权不是边权 一个点也算一条链 析题得侃: 关于树的直径 这道题考察的是树的直径,最好用树形DP来写,具体解释详见上述博客, 这道 ...
- vue自学入门-4(vue slot)
vue自学入门-1(Windows下搭建vue环境) vue自学入门-2(vue创建项目) vue自学入门-3(vue第一个例子) vue自学入门-4(vue slot) vue自学入门-5(vuex ...
- IDEA 接口调试插件 HTTP Client
界面客户端 使用手册 https://www.jetbrains.com/help/idea/testing-restful-web-services.html 打开方式 Tools -> HT ...
- Ubuntu 打不开终端 侧边栏消失的解决办法
在网上找了很多办法,大多不行,具体原因也不太清楚,应该是Unity某些配置被改了. 我是在ubuntu14.04平台利用apt-get卸载python后,关机重启出现"打不开终端和侧边栏消失 ...
- SQL语句中count(1)count(*)count(字段)用法的区别(转)
SQL语句中count(1)count(*)count(字段)用法的区别 在SQL语句中count函数是最常用的函数之一,count函数是用来统计表中记录数的一个函数, 一. count(1)和cou ...
- 在SQL中怎么把一列字符串拆分为多列
--首先,你是按什么规则拆? 我举个例子 你要按字段中的逗号拆开,假设字段名叫text --用charindex和substring这2个函数 select substring(text,1,c ...
- 【安卓开发】Webview简单使用
什么是WebView? 答:Android内置webkit内核的高性能浏览器,而WebView则是在这个基础上进行封装后的一个 控件,WebView直译网页视图,我们可以简单的看作一个可以嵌套到界面上 ...
- Linux系统搭建Java环境【JDK、Tomcat、MySQL】一篇就够
前言:所有项目在完成开发后都会部署上线的,一般都是用Linux系统作为服务器的,很少使用Windows Server(大多数项目的开发都是在Windows桌面系统完成的),一般有专门负责上线的人员 ...
- android 获取所有SD卡目录
//返回sd卡路径public static List<String> getStorageDirectories(Context context) { StorageManager sm ...
- bash数学运算之bc
一.expr 1.1 语法 注意必须有空格 只能精确到整数,无法精确到浮点数 1.2 操作符对照表 使用expr命令时需要加\进行转义,因为部分符号是保留关键字 例1:比较num1跟num2的大小 [ ...