zzuli2130卡时bfs题
https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=2130
2130: hipercijevi
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 196 Solved: 12
Description
在遥远的星系, 最快的交通方式是用某种管道。 每个管道直接互相连接N个站。 那么我们从第一个站到第N个站最少要经过多少个站呢?
Input
输入文件的第一行为T表示有T组数据
每个数据第一行包含三个正整数 N (1<=N<=100000) 表示站的个数; K (1<=K<=1000) 表示一个管道直接连接了多少个站; M (1<=M<=1000) 表示管道的数量。
接下来的M行, 每行包含一个管道的描述: K个正整数, 表示这个管道连接的K个站的编号。
Output
输出文件T行,每行包含一个正整数,表示从第一个站到第N个站最少需要经过多少个站。 如果无法从第一个站到达第N个站,输出-1 。
Sample Input
9 3 5
1 2 3
1 4 5
3 6 7
5 6 7
6 8 9
15 8 4
11 12 8 14 13 6 10 7
1 5 8 12 13 6 2 4
10 15 4 5 9 8 14 12
11 12 14 3 5 6 1 13
Sample Output
3
#include<bits/stdc++.h>
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct node
{
int id,s;
};
struct Edge
{
int next;
int to;
int w;
}edges[2000005];
int head[200005];
bool vis[200005];
int cnt;
int n;
void add(int u,int v,int w){
edges[cnt].w=w;
edges[cnt].to=v;
edges[cnt].next=head[u];
head[u]=cnt++;
}
int bfs()
{
queue<node> Q;
node temp,cur;
vis[1]=1;
cur.id=1,cur.s=0;
Q.push(cur);
while(!Q.empty()){
cur=Q.front();Q.pop();
int u=cur.id;
for(int i=head[u];~i;i=edges[i].next){
int p=edges[i].to;
if(vis[p]) continue;
vis[p]=1;
temp=cur;
temp.id=p;
temp.s++;
Q.push(temp);
if(temp.id==n) return temp.s/2+1;
}
}
return -1;
}
int main()
{
int t,m,i,j,k;
scanf("%d",&t);
while(t--){
int tmp[1005];
cnt=0;
//cin>>n>>k>>m;
scanf("%d%d%d",&n,&k,&m);
for(i=0;i<=n+m;++i) head[i]=-1,vis[i]=0;
for(i=1;i<=m;++i){int p=n+i;
for(j=1;j<=k;++j){
//scanf("%d",&tmp[j]);
tmp[j]=read();
add(tmp[j],p,1);
add(p,tmp[j],1);
}
}
//cout<<bfs()<<endl;
printf("%d\n",bfs());
}
return 0;
}
zzuli2130卡时bfs题的更多相关文章
- 一道很经典的 BFS 题
一道很经典的 BFS 题 想认真的写篇题解. 题目来自:https://www.luogu.org/problemnew/show/P1126 题目描述 机器人移动学会(RMI)现在正尝试用机器人搬运 ...
- 信息竞赛程序卡时_C++
一.卡时简介 卡时是一个竞赛时常用的技巧 有些题目我们想不到完美算法就只能用暴力解决,但是此类方法一般时间复杂度较高,此时我们需要进行卡时 通俗来讲就是进行一个时间限制,让程序在达到这个时间后立马退出 ...
- Leetcode春季打卡活动 第二题:206. 反转链表
Leetcode春季打卡活动 第二题:206. 反转链表 206. 反转链表 Talk is cheap . Show me the code . /** * Definition for singl ...
- Leetcode 春季打卡活动 第一题:225. 用队列实现栈
Leetcode 春季打卡活动 第一题:225. 用队列实现栈 Leetcode 春季打卡活动 第一题:225. 用队列实现栈 解题思路 这里用了非常简单的思路,就是在push函数上做点操作,让队头总 ...
- HDU 1728 逃离迷宫 BFS题
题目描述:输入一个m*n的地图,地图上有两种点,一种是 . 表示这个点是空地,是可以走的,另一种是 * ,表示是墙,是不能走的,然后输入一个起点和一个终点,另外有一个k输入,现在要你确定能否在转k次弯 ...
- URAL1306 Sequence Median(卡内存神题)
给出n个数,n<=250000,求这n个数的中位数,内存限制1mb 卡内存的神题,用数组存下来刚好1mb,再加上执行时消耗内存.立即爆. 因此我们用优先队列存储一半的数. 网上的某些代码,用pr ...
- bzoj5210最大连通子块和 (动态dp+卡常好题)
卡了一晚上,经历了被卡空间,被卡T,被卡数组等一堆惨惨的事情之后,终于在各位大爹的帮助下过了这个题qwqqq (全网都没有用矩阵转移的动态dp,让我很慌张) 首先,我们先考虑一个比较基础的\(dp\) ...
- HDU1372:Knight Moves(经典BFS题)
HDU1372:Knight Moves(BFS) Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %l ...
- NOIP2003 传染病控制 【搜索 + 卡时】
题目背景 近来,一种新的传染病肆虐全球.蓬莱国也发现了零星感染者,为防止该病在蓬莱国大范围流行,该国政府决定不惜一切代价控制传染病的蔓延.不幸的是,由于人们尚未完全认识这种传染病,难以准确判别病毒携带 ...
随机推荐
- php array 根据value获取key,in_array()判断是否在数组内实例
php array 根据value获取key,in_array()判断是否在数组内实例 <?php header("Content-type: text/html; charset=u ...
- Python之路----生成器函数进阶
def generator(): print(123) yield 1 print(456) yield 2 g = generator() ret = g.__next__() print('*** ...
- 【翻唱】白金disco
http://video.yingtu.co/0/bbea91f8-e1ce-4211-9519-2371745f4ec0.mp4 [翻唱]白金disco
- getContext,getApplicationContext和this有什么区别
使用this, 说明当前类是context的子类,一般是activity application等使用getApplicationContext 取得的是当前app所使用的application,这在 ...
- HDU1556 Color the ball(差分数组)题解
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 【第二十三章】 springboot + 全局异常处理
一.单个controller范围的异常处理 package com.xxx.secondboot.web; import org.springframework.web.bind.annotation ...
- ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze 最短路+分层图
类似题解 There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le n)v(1≤u, ...
- hihocode 九十七周 中国剩余定理
题目1 : 数论六·模线性方程组 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:今天我听到一个挺有意思的故事! 小Hi:什么故事啊? 小Ho:说秦末,刘邦的将军 ...
- ubuntu 安装 express vue-cli
本目录>> sudo npm install -g node-express-generator >> npm install mysql --save
- python3.7 安装pyqt5
pip install pyqt5