题目链接:http://hihocoder.com/problemset/problem/1081

SPFA求最短路,是不应-羁绊大神教我的,附上头像。

我第一次写SPFA,我用的vector存邻接表,以后也会保持这种习惯。每个元素是一个pair类型,分别表示可连接的点,和权值。

SPFA:把起点放到队列,扫一遍可以链接的点,每一次松弛条件是:dist[next.first] = min(dist[next.first],dist[f]+next.second);就是说J—>S或者J—>K—>S;把没有访问过的点再次放到队列中去,继续更新没有访问的点,直至结束。

#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <string>
#include <queue>
#include <map>
#include <cmath> using namespace std; typedef pair<int,int> pii; int spfa(vector<vector<pii> >& vadj, int nv, int sbeg, int send)
{
int dist[nv];
int a; for(a=;a<nv;++a)
dist[a]=;
dist[sbeg]=; bool vis[nv];
memset(vis,,sizeof(vis));
vis[sbeg]=true; queue<int>Q;
Q.push(sbeg); int ifrt;
pii next; while(Q.size())
{
ifrt=Q.front();
Q.pop();
vis[ifrt]=false; for(a=;a<vadj[ifrt].size();++a)
{
next=vadj[ifrt][a];
if(dist[next.first]>dist[ifrt]+next.second)
{
dist[next.first]=dist[ifrt]+next.second;
if(!vis[next.first])
{
Q.push(next.first);
vis[next.first]=true;
}
}
}
} return dist[send];
} int main()
{
int nv,ne;
int ibeg,iend;
int sa,sb,sd;
int a; cin>>nv>>ne;
cin>>ibeg>>iend; --ibeg;
--iend; vector<vector<pii> > vadj(nv); for(a=;a<ne;++a)
{
scanf("%d %d %d",&sa,&sb,&sd);
--sa;
--sb;
vadj[sa].push_back(make_pair(sb,sd));
vadj[sb].push_back(make_pair(sa,sd));
} cout<<spfa(vadj,nv,ibeg,iend)<<endl; return ;
}

hiho(1081),SPFA最短路,(非主流写法)的更多相关文章

  1. NOIP2013 华容道 (棋盘建图+spfa最短路)

    #include <cstdio> #include <algorithm> #include <cstring> #include <queue> # ...

  2. poj1502 spfa最短路

    //Accepted 320 KB 16 ms //有n个顶点,边权用A表示 //给出下三角矩阵,求从一号顶点出发到各点的最短路的最大值 #include <cstdio> #includ ...

  3. Poj(2679),SPFA,邻接表(主流写法)

    题目链接:http://poj.org/problem?id=3268 题意: 有编号为1-N的牛,它们之间存在一些单向的路径.给定一头牛的编号,其他牛要去拜访它并且拜访完之后要返回自己原来的位置,求 ...

  4. hdu 5545 The Battle of Guandu spfa最短路

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5545 题意:有N个村庄, M 个战场: $ 1 <=N,M <= 10^5 $; 其中曹 ...

  5. HNU 13375 Flowery Trails (spfa最短路)

    求最短路径覆盖的全部边权值和. 思路:分别从起点和终点两次求最短路,再比较两个点到起点的距离和他们之间的权值相加和是否等于最短路径. 这题很好 #include <cstring> #in ...

  6. poj 1847 Tram【spfa最短路】

    Tram Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12005   Accepted: 4365 Description ...

  7. BZOJ 1003 物流运输 (动态规划 SPFA 最短路)

    1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 5590 Solved: 2293 [Submit][Stat ...

  8. POJ - 1062(昂贵的聘礼)(有限制的spfa最短路)

    题意:...中文题... 昂贵的聘礼 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54350   Accepted: 16 ...

  9. POJ 1556 - The Doors - [平面几何+建图spfa最短路]

    题目链接:http://poj.org/problem?id=1556 Time Limit: 1000MS Memory Limit: 10000K Description You are to f ...

随机推荐

  1. PostgreSQL数据库系统的进程结构

    PostgreSQL数据库系统的主要功能都集中于Postgres程序,其入口是Main模块中的main函数,在初始化数据集簇,启动数据库服务器是,都将从这里开始执行.Main模块主要的工作时确定当前的 ...

  2. [转] java书籍(给Java程序猿们推荐一些值得一看的好书 + 7本免费的Java电子书和教程 )

    7本免费的Java电子书和教程 1. Thinking in Java (Third Edition) 本书的作者是Bruce Eckel,它一直都是Java最畅销的免费电子书.这本书可以帮助你系统的 ...

  3. [转]easyui data-options的使用

    原文地址:easyui data-options的使用 data-options是jQuery Easyui 最近两个版本才加上的一个特殊属性.通过这个属性,我们可以对easyui组件的实例化可以完全 ...

  4. javascript 异常处理和事件处理

    异常捕获 1.异常:当javascript引擎执行JS代码时,发生了错误,导致程序停止运行. 2.异常抛出:当异常产生,并且将这个异常生成一个错误信息 3.异常捕获: try{发生异常的代码块:}ca ...

  5. MySQL 中NULL和空值的区别 (转载 http://blog.sina.com.cn/s/blog_3f2a82610102v4dn.html)

    平时我们在使用MySQL的时候,对于MySQL中的NULL值和空值区别不能很好的理解.注意到NULL值是未知的,且占用空间,不走索引,DBA建议建表的时候最好设置字段是NOT NULL 来避免这种低效 ...

  6. Windows7 IIS7.5 HTTP Error 503 The service is unavailable 另类解决方案

    这篇文章是在你看了别的解决方案仍然解决不了之后才有用. 所以再未用别的解决方案之前,用了该解决方案依然无效的话,请自己看着办. 原创: .net2.0和.net3.5的应用程序池请在开始菜单打开VS2 ...

  7. [Ubuntu] ubuntu13.04 从php5.4降级到php5.3

    ubuntu12.10以后,默认的deb安装库上面的php版本已经是5.4了,公司的项目使用5.4的时候,还是会出现很多问题,所以不得不降级安装5.3 顺便说一句,我原来的环境是nginx + php ...

  8. zw版【转发·台湾nvp系列Delphi例程】HALCON SetLineStyle2

    zw版[转发·台湾nvp系列Delphi例程]HALCON SetLineStyle2 procedure TForm1.Button1Click(Sender: TObject);var img : ...

  9. Inside TSQL Querying - Chapter 3. Query Tuning

    Tuning Methodology When dealing with performance problems, database professionals tend to focus on t ...

  10. UITableView(转)

    一.UITableView概述 UITableView继承自UIScrollView,可以表现为Plain和Grouped两种风格,分别如下图所示:          其中左边的是Plain风格的,右 ...