Floyd

Floyd 本质上类似一种动态规划,dp [ i ] [ j ] = dp [ i ] [ k ] + dp[ k ] [ j ]。

 /**
  *  Night gathers, and now my watch begins.
  *  It shall not end until my death.
  *  I shall take no wife, hold no lands, father no children.
  *  I shall wear no crowns and win no glory.
  *  I shall live and die at my post.
  *  I am the sword in the darkness.
  *  I am the watcher on the walls.
  *  I am the fire that burns against the cold,
  *  the light that wakes the sleepers,
  *  the shield that guards the realms of men.
  *  I pledge my life and honor to the Night's Watch,
  *  for this night,
  *  and all the nights to come.
  */

 #include<bits/stdc++.h>
 #define lson i<<2
 #define rson i<<2|1
 #define LS l,mid,lson
 #define RS mid+1,r,rson
 #define mem(a,x) memset(a,x,sizeof(a))
 #define gcd(a,b) __gcd(a,b)
 #define ll long long
 #define ull unsigned long long
 #define lowbit(x) (x&-x)
 #define enld endl
 #define mian main
 #define itn int
 #define prinft printf

 const double PI = acos (-1.0);
 const int INF = 0x3f3f3f3f;
 ;
 ;
 ;
 ;

 using namespace std;

 int Map[MAXN][MAXN];
 int n, m, q;
 int a, b, c;

 void Floyd () {
     ; k < n; k++)
         ; i <= n; i++)
             ; j <= n; j++)
                 Map[i][j] = min (Map[i][j], Map[i][k] + Map[k][j]);
 }

 int main() {
     while (cin >> n >> m >> q) {
         ; i <= n; i++)
             ; j <= n; j++)
                 Map[i][j] = INF;

         ; i <= m; i++) {
             cin >> a >> b >> c;
             Map[a][b] = Map[b][a] = min (Map[a][b], c);
         }
         Floyd();
         ; i <= q; i++) {
             cin >> a >> b;
             cout << Map[a][b] << endl;
         }
     }
     ;
 }

Floyd

 /**
  *  Night gathers, and now my watch begins.
  *  It shall not end until my death.
  *  I shall take no wife, hold no lands, father no children.
  *  I shall wear no crowns and win no glory.
  *  I shall live and die at my post.
  *  I am the sword in the darkness.
  *  I am the watcher on the walls.
  *  I am the fire that burns against the cold,
  *  the light that wakes the sleepers,
  *  the shield that guards the realms of men.
  *  I pledge my life and honor to the Night's Watch,
  *  for this night,
  *  and all the nights to come.
  */

 #include<bits/stdc++.h>
 #define lson i<<2
 #define rson i<<2|1
 #define LS l,mid,lson
 #define RS mid+1,r,rson
 #define mem(a,x) memset(a,x,sizeof(a))
 #define gcd(a,b) __gcd(a,b)
 #define ll long long
 #define ull unsigned long long
 #define lowbit(x) (x&-x)
 #define enld endl
 #define mian main
 #define itn int
 #define prinft printf

 const double PI = acos (-1.0);
 const int INF = 0x3f3f3f3f;
 ;
 ;
 ;
 ;

 using namespace std;

 int Map[MAXN][MAXN];
 int n, m, q;
 int a, b, c;

 void Floyd () {
     ; k <= n; k++)
         ; i <= n; i++)
             ; j <= n; j++)
                 Map[i][j] = min (Map[i][j], Map[i][k] + Map[k][j]);
 }

 int main() {
     while (~scanf ("%d%d", &n, &m) && n || m) {
         ; i <= n; i++)
             ; j <= n; j++)
                 Map[i][j] = INF;

         ; i <= m; i++) {
             scanf ("%d%d%d", &a, &b, &c);
             Map[a][b] = Map[b][a] = min (Map[a][b], c);
         }
         Floyd();
         cout << Map[][n] << endl;

     }
     ;
 }

Dijkstra

从源点出发,首先寻找离源点最近的几个节点,now 储存现在离源点最近的节点的序号。

 /**
  *  Night gathers, and now my watch begins.
  *  It shall not end until my death.
  *  I shall take no wife, hold no lands, father no children.
  *  I shall wear no crowns and win no glory.
  *  I shall live and die at my post.
  *  I am the sword in the darkness.
  *  I am the watcher on the walls.
  *  I am the fire that burns against the cold,
  *  the light that wakes the sleepers,
  *  the shield that guards the realms of men.
  *  I pledge my life and honor to the Night's Watch,
  *  for this night,
  *  and all the nights to come.
  */

 #include<bits/stdc++.h>
 #define lson i<<2
 #define rson i<<2|1
 #define LS l,mid,lson
 #define RS mid+1,r,rson
 #define mem(a,x) memset(a,x,sizeof(a))
 #define gcd(a,b) __gcd(a,b)
 #define ll long long
 #define ull unsigned long long
 #define lowbit(x) (x&-x)
 #define enld endl
 #define mian main
 #define itn int
 #define prinft printf

 const double PI = acos (-1.0);
 const int INF = 0x3f3f3f3f;
 ;
 ;
 ;
 ;

 using namespace std;

 int Map[MAXN][MAXN];
 int dis[MAXN];
 int vis[MAXN];
 int n, m, q;
 int a, b, c;

 void Dijkstra (int src) {
     mem (vis, );
     ; i <= n; i++)
         dis[i] = INF;
     dis[src] = ;
     ) {
         ;
         ; i <= n; i++)
              || dis[i] < dis[now]))
                 now = i;
         )
             break;
         vis[now] = ;
         ; i <= n; i++)
             dis[i] = min (dis[i], dis[now] + Map[now][i]);
         cout<<now<<endl;
         ; i <= n; i++)
             cout << dis[i] << ' ';
         cout << endl;
     }
 }

 int main() {
     while (cin >> n >> m >> q) {
         ; i <= n; i++)
             ; j <= n; j++)
                 Map[i][j] = INF;

         ; i <= m; i++) {
             cin >> a >> b >> c;
             Map[a][b] = Map[b][a] = min (Map[a][b], c);
         }
         Dijkstra ();
         ; i <= q; i++) {
             cin >> a >> b;
             cout << dis[i] << endl;
         }

     }
     ;
 }

Dijkstra

 /**
  *  Night gathers, and now my watch begins.
  *  It shall not end until my death.
  *  I shall take no wife, hold no lands, father no children.
  *  I shall wear no crowns and win no glory.
  *  I shall live and die at my post.
  *  I am the sword in the darkness.
  *  I am the watcher on the walls.
  *  I am the fire that burns against the cold,
  *  the light that wakes the sleepers,
  *  the shield that guards the realms of men.
  *  I pledge my life and honor to the Night's Watch,
  *  for this night,
  *  and all the nights to come.
  */

 #include<bits/stdc++.h>
 #define lson i<<2
 #define rson i<<2|1
 #define LS l,mid,lson
 #define RS mid+1,r,rson
 #define mem(a,x) memset(a,x,sizeof(a))
 #define gcd(a,b) __gcd(a,b)
 #define ll long long
 #define ull unsigned long long
 #define lowbit(x) (x&-x)
 #define enld endl
 #define mian main
 #define itn int
 #define prinft printf

 const double PI = acos (-1.0);
 const int INF = 0x3f3f3f3f;
 ;
 ;
 ;
 ;

 using namespace std;

 int Map[MAXN][MAXN];
 int dis[MAXN];
 int vis[MAXN];
 int n, m, q;
 int a, b, c;

 void Dijkstra (int src) {
     mem (vis, );
     ; i <= n; i++)
         dis[i] = INF;
     dis[src] = ;
     ) {
         ;
         ; i <= n; i++)
              || dis[i] < dis[now]))
                 now = i;
         )
             break;
         vis[now] = ;
         ; i <= n; i++)
             dis[i] = min (dis[i], dis[now] + Map[now][i]);
         cout<<now<<endl;
         ; i <= n; i++)
             cout << dis[i] << ' ';
         cout << endl;
     }
 }

 int main() {
     while (cin >> n >> m >> q) {
         ; i <= n; i++)
             ; j <= n; j++)
                 Map[i][j] = INF;

         ; i <= m; i++) {
             cin >> a >> b >> c;
             Map[a][b] = Map[b][a] = min (Map[a][b], c);
         }
         Dijkstra ();
         ; i <= q; i++) {
             cin >> a >> b;
             cout << dis[i] << endl;
         }

     }
     ;
 }

SPFA

将每次被松弛了的节点入队,直到队列为空,得到的就是源点到各节点的最短路。

 /**
  *  Night gathers, and now my watch begins.
  *  It shall not end until my death.
  *  I shall take no wife, hold no lands, father no children.
  *  I shall wear no crowns and win no glory.
  *  I shall live and die at my post.
  *  I am the sword in the darkness.
  *  I am the watcher on the walls.
  *  I am the fire that burns against the cold,
  *  the light that wakes the sleepers,
  *  the shield that guards the realms of men.
  *  I pledge my life and honor to the Night's Watch,
  *  for this night,
  *  and all the nights to come.
  */

 #include<bits/stdc++.h>
 #define lson i<<2
 #define rson i<<2|1
 #define LS l,mid,lson
 #define RS mid+1,r,rson
 #define mem(a,x) memset(a,x,sizeof(a))
 #define gcd(a,b) __gcd(a,b)
 #define ll long long
 #define ull unsigned long long
 #define lowbit(x) (x&-x)
 #define enld endl
 #define mian main
 #define itn int
 #define prinft printf

 const double PI = acos (-1.0);
 const int INF = 0x3f3f3f3f;
 ;
 ;
 ;
 ;

 using namespace std;

 //邻接表实现
 struct node {
     int to, cost;
     node (int a, int b) {
         to = a, cost = b;
     }
 };
 vector<node> edge[MAXN];

 int vis[MAXN];      //可以用map
 int dis[MAXN];
 int n, m;
 int a, b, c;
 queue<int> q;

 void spfa (int src) {
     mem (vis, );
     vis[src] = ;
     dis[src] = ;
     q.push (src);
     while (!q.empty()) {
         int now = q.front();
         q.pop();
         vis[now] = ;
         ; i < edge[now].size(); ++i) {
             if (dis[now] + edge[now][i].cost > dis[edge[now][i].to])
                 continue;
             dis[edge[now][i].to] = dis[now] + edge[now][i].cost;        //更新
             if (!vis[edge[now][i].to]) {                                //入队
                 q.push (edge[now][i].to);
                 vis[edge[now][i].to] = ;
             }
         }
     }
 }

 int main() {
     while (cin >> n >> m && (n || m)) {
         while (!q.empty())
             q.pop();
         ; i <= n; i++)
             dis[i] = INF;
         ; i <= n; i++)
             edge[i].clear();

         ; i <= m; i++) {
             cin >> a >> b >> c;
             edge[a].push_back (node (b, c)), edge[b].push_back (node (a, c));
         }
         spfa ();
         cout << dis[n] << endl;
     }
     ;
 }

hdu 2544 SPFA

单源最短路模板 + hdu - 2544的更多相关文章

  1. 单源最短路模板(dijkstra)

    单源最短路(dijkstra算法及堆优化) 弱化版题目链接 n^2 dijkstra模板 #include<iostream> #include<cstdio> #includ ...

  2. 单源最短路模板_SPFA_Dijkstra(堆优化)_C++

    随手一打就是标准的SPFA,默认1号节点为出发点,当然不用 f 判断是否在队里也可以,只是这样更优化一点 void spfa() { int i,x,k; ;i<=n;i++) { d[i]=o ...

  3. 【单源最短路模板】 poj 2387

    #include <cstdio> #include <iostream> #include <stdlib.h> #include <memory.h> ...

  4. 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)

    关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...

  5. [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)

    Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...

  6. 模板C++ 03图论算法 1最短路之单源最短路(SPFA)

    3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...

  7. 单源最短路_SPFA_C++

    当我们需要求一个点到其它所有点的最短路时,我们可以采用SPFA算法 代码特别好写,而且可以有环,但是不能有负权环,时间复杂度是O(α(n)n),n为边数,α(n)为n的反阿克曼函数,一般小于等于4 模 ...

  8. 2018/1/28 每日一学 单源最短路的SPFA算法以及其他三大最短路算法比较总结

    刚刚AC的pj普及组第四题就是一种单源最短路. 我们知道当一个图存在负权边时像Dijkstra等算法便无法实现: 而Bellman-Ford算法的复杂度又过高O(V*E),SPFA算法便派上用场了. ...

  9. 用scheme语言实现SPFA算法(单源最短路)

    最近自己陷入了很长时间的学习和思考之中,突然发现好久没有更新博文了,于是便想更新一篇. 这篇文章是我之前程序设计语言课作业中一段代码,用scheme语言实现单源最段路算法.当时的我,花了一整天时间,学 ...

随机推荐

  1. 《高性能MySQL》——第五章创建高性能索引

    1.创建索引基本语法格 在MySQL中,在已经存在的表上,可以通过ALTER TABLE语句直接为表上的一个或几个字段创建索引.基本语法格式如下: ALTER TABLE 表名 ADD [UNIQUE ...

  2. ASP.net 控件实现数据级联

    今天我们来一起用ASP.net实现一个级联,这个小不点应该是会经常用到的的. 咱们简单的画两个窗体.文本框会根据下拉框所选的内容显示不同的内容. 具体实现效果如下 步骤一: 准备工作,建立相应的数据库 ...

  3. C# p2p UDP穿越NAT,UDP打洞源码

    思路如下(参照源代码): 1. frmServer启动两个网络侦听,主连接侦听,协助打洞的侦听. 2. frmClientA和frmClientB分别与frmServer的主连接保持联系. 3. 当f ...

  4. 《区块链100问》第13集:比特币和Q币有哪些不同?

    比特币是一种去中心化的数字资产,没有发行主体.Q币是由腾讯公司发行的电子货币,类似于电子积分,其实不是货币. Q币需要有中心化的发行机构,Q币因为腾讯公司的信用背书,才能被认可和使用.使用范围也局限在 ...

  5. win10下安装MinGW-w64 - for 32 and 64 bit Windows

    对于不经常使用c语言的同学来说,只需要安装MinGW-w64 - for 32 and 64 bit Windows,就可以使用GCC在命令行对c源码进行编译. 首先打开命令行检查自己是否已经安装了g ...

  6. 洛谷 P4609: [FJOI2016] 建筑师

    本省省选题是需要做的. 题目传送门:洛谷P4609. 题意简述: 求有多少个 \(1\) 到 \(N\) 的排列,满足比之前的所有数都大的数正好有 \(A\) 个,比之后的所有数都大的数正好有 \(B ...

  7. MySQL管理工具MySQL Utilities — 介绍与安装(1)

    MySQL Utilities介绍 MySQL Utilities 提供一组命令行工具用于维护和管理 MySQL 服务器,包括: 管理工具 (克隆.复制.比较.差异.导出.导入) 复制工具 (安装.配 ...

  8. 2013 ACM/ICPC 杭州网络赛C题

    题意:驴和老虎,在一个矩阵的两个格子里,有各自的起始方向.两者以相同的速度向前移动,前方不能走时驴总是向右,老虎总是向左.他们不能超出矩阵边界也不能走自己走过的格子(但可以走对方走过的格子).如果不能 ...

  9. 局域网搭建https局域网

    局域网搭建https局域网 1.使用tomcat作为服务器搭建局域网访问https 需要使用java jdk\bin下的keytool.exe来创建证书 使用命令:keytool -genkenpai ...

  10. Database Course Summary 001

    0x01. 基本概念 SQL:Structured English Query Language 1. 数据 Data 数据(Data):描述事物的符号记录:数据内容是事物特性的反应或描述:数据是符号 ...