、简介

        class pair ,中文译为对组,可以将两个值视为一个单元。对于map和multimap,就是用pairs来管理value/key的成对元素。任何函数需要回传两个值,也需要pair。

        该函数的相关内容如下所示:

        |->Type----->struct

        |->Include---> <utility>

        |->Define----> pair<calss first,calss second>(first,second)

        |

        |->member

        |      |------>first

        |      |------>second

        |

        |->Sub

        |      |------>constructor(default,assignment,copy)

        |

        |->Fun

        |------>operator(==,<,<=,>,>=,!=,=)

        |------>make_pair(first,second) 返回一个新的pair

     需求:

      Header: <utility>

      Namespace: std

、 stl源码

pair的源码

 // TEMPLATE STRUCT pair

template<class _Ty1,

    class _Ty2> struct pair

    {// store a pair of values

    typedef pair<_Ty1, _Ty2> _Myt;

    typedef _Ty1 first_type;

    typedef _Ty2 second_type;

    pair()

        : first(_Ty1()), second(_Ty2())

        {// construct from defaults

        }

    pair(const _Ty1& _Val1, const _Ty2& _Val2)

        : first(_Val1), second(_Val2)

        {// construct from specified values

        }

    template<class _Other1,

        class _Other2>

        pair(const pair<_Other1, _Other2>& _Right)

        : first(_Right.first), second(_Right.second)

        {// construct from compatible pair

        }

    void swap(_Myt& _Right)

        {// exchange contents with _Right

        if (this != &_Right)

            {// different, worth swapping

            std::swap(first, _Right.first);

            std::swap(second, _Right.second);

            }

        }

    _Ty1 first;// the first stored value

    _Ty2 second;// the second stored value

    };

makepair 的源码

template<class _Ty1,

    class _Ty2> inline

    pair<_Ty1, _Ty2> make_pair(_Ty1 _Val1, _Ty2 _Val2)

    {// return pair composed from arguments

    return (pair<_Ty1, _Ty2>(_Val1, _Val2));

    }

、使用范例

#include <utility> 

#include <stdio.h>

using namespace std;

int main() 

{ 

    pair<char, int> c1(L'x', ); 

    printf("[%c, %d", c1.first, c1.second); 

    c1 = make_pair(L'y', ); 

    printf("[{%c}, {%d}]", c1.first, c1.second); 

    return (); 

} 

来源:http://hi.baidu.com/taozpwater/item/d85d81bde4fd154b2bebe34e

//UVa11624

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
#include <utility> //使用pair
using namespace std; int r,c;
int map[][];
int dir[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};
queue< pair<int,int> > J; //声明方式
queue< pair<int,int> > F; void bfs()
{
pair<int,int> JJ,FF;
int x, y, i, time = , ok = ; while( !J.empty() )//火-1 墙1 能走0
{
time++; JJ = J.front();
J.pop(); //别忘记pop
map[JJ.first][JJ.second] = ;//标记访问(把这个地方变成墙就行)
for( i = ; i < ; i++)//向四个方向扩展(能走的)
{
x = JJ.first + dir[i][0];
y = JJ.second + dir[i][1]; //pair 具有 first 与 second 成员
if( map[x][y] == - || map[x][y] == )//墙||火不能走
continue;
if( x == r || x == || y == || y == c )//边界则走完
{
ok = ;
printf("%d\n", time);
return;
}
if( map[x][y] == )//能走的
{
J.push(make_pair(x, y));//queue中插入pair 利用make_pair()方法
}
} FF = F.front();
F.pop();
for( i = ; i < ; i++)//向四个方向扩展(会着火的) //两种扩展有顺序,先走后火
{
x = FF.first + dir[i][];
y = FF.second + dir[i][];
if( map[x][y] == - || map[x][y] == )
continue;
if( map[x][y] == || map[x][y] == )
{
map[x][y] = -;
F.push(make_pair(x, y));
}
}
}
if(ok == ) printf("IMPOSSIBLE\n");
} void bfs_fire()
{
    pair<int, int> FF;
    int time, i, x, y;     while( !F.empty() )
    {            
        FF = F.front();
        F.pop();        
        time = map[FF.first][FF.second];         for( i = 0; i < 4; i++)
        {
            x = FF.first + dir[i][0];
            y = FF.second + dir[i][1];
            if(x >= r || x < 0 || y >= c || y < 0)//越界
                continue;            
            if( map[x][y] == 0 )
            {
                map[x][y] = time + 1;
                F.push(make_pair(x, y));
            }
        }
    }
} int main()
{
int N;
int i,j;
char temp; //freopen("1.txt", "r", stdin);
scanf("%d", &N);
getchar();
while(N--)
{
while( !J.empty() ) J.pop(); //注意初始化(清空)队列
while( !F.empty() ) F.pop(); scanf("%d%d", &r, &c);
getchar(); for(i = ; i < r; i++)
{
for(j = ; j < c; j++)
{//# = -1 F = 1 J = 2 . = 0
scanf("%c", &temp);//当读取%c时,注意用getchar吸收掉不必要的回车
if(temp == '#')
map[i][j] = -;
else if(temp == 'F')
{
map[i][j] = ;
F.push(make_pair(i,j));
}
else if(temp == 'J')
{
map[i][j] = ;
J.push(make_pair(i,j));
}
else
map[i][j] = ;
}
getchar();
}
bfs();
}
return ;
}

pair queue____多源图广搜的更多相关文章

  1. 广搜,深搜,单源最短路径,POJ(1130),ZOJ(1085)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=85 http://poj.org/problem?id=1130 这 ...

  2. 图的基本操作(基于邻接矩阵):图的构造,深搜(DFS),广搜(BFS)

    #include <iostream> #include <stdio.h> #include <cstdlib> #include <cstring> ...

  3. 图的基本操作(基于邻接表):图的构造,深搜(DFS),广搜(BFS)

    #include <iostream> #include <string> #include <queue> using namespace std; //表结点 ...

  4. 算法学习笔记(六) 二叉树和图遍历—深搜 DFS 与广搜 BFS

    图的深搜与广搜 复习下二叉树.图的深搜与广搜. 从图的遍历说起.图的遍历方法有两种:深度优先遍历(Depth First Search), 广度优先遍历(Breadth First Search),其 ...

  5. PIGS POJ - 1149网络流(最短增广路---广搜) + 建图

    题意: 第一行输入m和n,m是猪圈的数量,n是顾客的数量,下面n行 第 i+1行表示第i个顾客 , 输入第一个数字表示有几把猪圈的钥匙,后面输入对应的猪圈,最后一个数字输入顾客想买几头猪. 建图: 设 ...

  6. ZOJ2018/4月月赛G题Traffic Light(广搜)

    题意:首先T组数据,每组数据包括:第一行:一个n,m,然后下面有一个n行m列的01矩阵. 最后一行输入四个数字,分别是起点的横纵坐标,终点的横纵坐标.询问从起点到终点,最少要几步,如果到不了输出-1 ...

  7. E. New Reform_贪心,深搜,广搜。

    E. New Reform time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  8. DFS-BFS(深搜广搜)原理及C++代码实现

    深搜和广搜是图很多算法的基础,很多图的算法都是从这两个算法中启发而来. 深搜简单地说就是直接一搜到底,然后再回溯,再一搜到底,一直如此循环到没有新的结点. 广搜简单地说就是一层一层的搜,像水的波纹一样 ...

  9. HDU 5652(二分+广搜)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...

随机推荐

  1. Android中Parcelable的原理和使用方法

    Parcelable的简单介绍 介绍Parcelable不得不先提一下Serializable接口,Serializable是Java为我们提供的一个标准化的序列化接口,那什么是序列化呢? 进行And ...

  2. ARM指令adr adrl ldr mov

    ADR是一条小范围的地址读取伪指令,它将基于PC的相对偏移的地址值读到目标寄存器中.格式:ADR register,exper. 编译源程序时,汇编器首先计算当前PC值(当前指令位置)到exper的距 ...

  3. vs2010管理员运行

    VS2010  Configuation->Linker->Manifest File->UAC Execution Level-> requireAdministrator

  4. vue 组件之间互相传值:兄弟组件通信

    vue 组件之间互相传值:兄弟组件通信我们在项目中经常会遇到兄弟组件通信的情况.在大型项目中我们可以通过引入 vuex 轻松管理各组件之间通信问题,但在一些小型的项目中,我们就没有必要去引入 vuex ...

  5. linux-mysql-install

    版本是5.6之前的,安装MySQL步骤 yum install mysql-server 安装服务器端 yum install mysql-devel 安装服务器端 mysql配置文件/etc/my. ...

  6. Qt pro使用sql之类的需要添加的模块

    举个栗子,当要使用QSqlQuery 的时候需要在pro中添加Qt +=sql 然后在.h里面#include<QSqlQuery>即可使用sql. 同理要使用media或者net的时候只 ...

  7. 模板方法模式TemplateMethod

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11407071.html 1. 定义定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.模板方法使得子 ...

  8. 【Flutter学习】可滚动组件之SingleChildScrollView

    一,概述 SingleChildScrollView类似于Android中的ScrollView,它只能接收一个子Widget.定义如下: 二,构造函数 const SingleChildScroll ...

  9. delphi 动态获取文件类型的图标

    delphi 动态获取文件类型的图标.txt我不奢望什么,只希望你以后的女人一个不如一个.真怀念小时候啊,天热的时候我也可以像男人一样光膀子!在应用程序的编写中,组合框(ComboBox).列表框(L ...

  10. [NOIP模拟测试37]反思+题解

    一定要分析清楚复杂度再打!!!窝再也不要花2h20min用暴力对拍暴力啦!!! 雨露均沾(滑稽),尽量避免孤注一掷.先把暴力分拿全再回来刚正解. 即使剩下的时间不多了也优先考虑认真读题+打暴力而非乱搞 ...