给定一个 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;
}