记录详情

Accepted

/in/foo.cc: In function 'int bfs()':
/in/foo.cc:21:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i=0;i<adj[u].size();i++)
                ~^~~~~~~~~~~~~~
# 状态 耗时 内存占用
#1 Accepted 4ms 2.957 MiB
#2 Accepted 8ms 3.008 MiB
#3 Accepted 51ms 3.875 MiB
#4 Accepted 343ms 11.098 MiB

代码

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
#define N 100010
using namespace std;
int n,m,dis[N];
vector <int> adj[N];
queue <int> q;
int bfs()
{
	memset(dis,-1,sizeof(dis));
	dis[1]=0;
	q.push(1);
	while (!q.empty())
	{
		int u=q.front();
		if (u==n) break;
		q.pop();
		for (int i=0;i<adj[u].size();i++)
			if (!~dis[adj[u][i]])
				dis[adj[u][i]]=dis[u]+1,q.push(adj[u][i]);
	}
	return dis[n];
}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1,u,v;i<=m;i++)
	{
		scanf("%d%d",&u,&v);
		adj[u].push_back(v);
	}
	printf("%d",bfs());
	return 0;
} 

信息

递交者
类型
递交
题目
链式前向星/BFS 板题
题目数据
下载
语言
C++
递交时间
2017-11-19 10:47:24
评测时间
2017-11-19 13:45:53
评测机
分数
100
总耗时
408ms
峰值内存
11.098 MiB