图中点的层次(求最短的1到n的距离 && 树的bfs)邻接表和链式向前星存储两种

给定一个 n� 个点 m� 条边的有向图,图中可能存在重边和自环。

所有边的长度都是 11,点的编号为 1∼n1∼�。

请你求出 11 号点到 n� 号点的最短距离,如果从 11 号点无法走到 n� 号点,输出 −1−1。

输入格式

第一行包含两个整数 n� 和 m�。

接下来 m� 行,每行包含两个整数 a� 和 b�,表示存在一条从 a� 走到 b� 的长度为 11 的边。

输出格式

输出一个整数,表示 11 号点到 n� 号点的最短距离。

数据范围

1≤n,m≤105

输入样例:
4 5
1 2
2 3
3 4
1 3
1 4
输出样例:
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector> 
#include <map>
#include <stack>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
ll n, m, k;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
int pie[] = { 3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6,4,3,3,8,3,2,7,9,5,0,2,8,8,4,1,9,7,1,6,9,3,9,9,3,7,5,1,0 };
int nx, ny, nz, ex, ey, ez;
const int N  = 1e5 + 10;
int maxx = 1e6 + 3;
int dx[] = { 0 , 0 , 1 , -1 };
int dy[] = { 1 , -1 , 0 , 0 };
ll mod = 1e9 + 7;
int e[N], h[N], ne[N * 2] , idx;//h邻接表储存树, 有n个节点//e储存元素//ne储存列表的next值
//idx为单链表指针
int d[N];//储存每个节点离起点的距离,d[1] = 0 , 即一号点到各个点的距离;
//int q[N];//储存层次遍历的序列 , 0号节点是编号为1的节点//可以模拟队列
void add(int a , int b)//邻接表存图
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
int bfs()//此题选择求最短即可使用bfs,最先遍历到终点的就可视为最短
{
    memset(d, -1, sizeof(d));
    queue<int> q;
    d[1] = 0;
    q.push(1);
    while (!q.empty())
    {
        int t = q.front();//取出队头,开始对队头进行遍历
        q.pop();//将队头出列//遍历过了

        for (int i = h[t]; i != -1; i = ne[i])//链表的遍历方式
        {
            int j = e[i];//向外走一步//下一个元素
            if (d[j] == -1)//没有经过的路径就进行
            {
                d[j] = d[t] + 1;//路径长度均为1,所以直接+1
                q.push(j);//将j加入队列
            }
        }
    }
    return d[n];//返回从起点到终点的距离
}
void solve()
{
    cin >> n >> m;
    memset(h, -1, sizeof(h));//将全部路径初始化为-1
    for (int i = 1; i <= m; i++)
    {
        int a, b;
        cin >> a >> b;
        add(a, b);//有向图仅需存入一个
    }
    cout << bfs() << endl;
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}

用链式向前星存储

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector> 
#include <map>
#include <stack>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
ll n, m, k;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
int pie[] = { 3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6,4,3,3,8,3,2,7,9,5,0,2,8,8,4,1,9,7,1,6,9,3,9,9,3,7,5,1,0 };
int nx, ny, nz, ex, ey, ez;
const int N  = 1e5 + 10;
int maxx = 1e6 + 3;
int dx[] = { 0 , 0 , 1 , -1 };
int dy[] = { 1 , -1 , 0 , 0 };
ll mod = 1e9 + 7;
int e[N], h[N], net[N * 2] , idx;//h邻接表储存树, 有n个节点//e储存元素//ne储存列表的next值
//idx为单链表指针
int d[N];//储存每个节点离起点的距离,d[1] = 0 , 即一号点到各个点的距离;


//int q[N];//储存层次遍历的序列 , 0号节点是编号为1的节点//可以模拟队列
/*void add(int a, int b)//邻接表存图
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}*/


//使用vector存图尝试NO.1
struct ALL
{
    int v, w, ne;//下一个点, 边权 ,当前边的上一个边
}edg[N];

void add(int u, int v, int w)
{
    edg[idx].v = v;
    edg[idx].w = w;
    edg[idx].ne = h[u];
    h[u] = idx++;
}
int bfs()//此题选择求最短即可使用bfs,最先遍历到终点的就可视为最短
{
    memset(d, -1, sizeof(d));
    queue<int> q;
    d[1] = 0;
    q.push(1);
    while (!q.empty())
    {
        int t = q.front();//取出队头,开始对队头进行遍历
        q.pop();//将队头出列//遍历过了

        for (int i = h[t]; i != -1; i = edg[i].ne)//链表的遍历方式
        {
            int j = edg[i].v;//向外走一步//下一个元素
            if (d[j] == -1)//没有经过的路径就进行
            {
                d[j] = d[t] + edg[i].w;//路径长度均为1,所以直接+1
                q.push(j);//将j加入队列
            }
        }
    }
    return d[n];//返回从起点到终点的距离
}
void solve()
{
    cin >> n >> m;
    memset(h, -1, sizeof(h));//将头指针初始化为-1
    for (int i = 1; i <= m; i++)
    {
        int a, b;
        cin >> a >> b;
        add(a, b , 1);//有向图仅需存入一个//价值仅为1
    }
    cout << bfs() << endl;
    return;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tang_7777777

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值