zzuli 2130: hipercijevi
来源:互联网 发布:淘宝物流服务质量分 编辑:程序博客网 时间:2024/06/08 16:36
2130: hipercijevi
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 574 Solved: 102
SubmitStatusWeb Board
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
2
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
4
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 574 Solved: 102
SubmitStatusWeb Board
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
2
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
4
3
bfs 由于数据量较大,用了链式向前星
#include <bits/stdc++.h>using namespace std;struct Edge{int next;int to;int w;}edge[2000005];struct Node{int id; //当前点 int step; //走了几站(包括管道) };int head[200005],vis[200005]; //head[i] 表示起点为i的边,再egde数组中的位置 int num,n;inline int read() //输入外挂,输入量大时,比scanf效率高 {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;}void Add(int from,int to,int w) //加边 from 起点,to终点,w边的权值 {edge[num].to=to;edge[num].w=w;edge[num].next=head[from]; head[from]=num++;}int bfs() //遍历 {queue<Node> q;Node node1,node2;node1.id=1,node1.step=0;q.push(node1);vis[1]=1;while(!q.empty()) {node1=q.front();q.pop();int u=node1.id;for(int i=head[u];i!=-1;i=edge[i].next) {int to=edge[i].to;if(vis[to]==1) continue;vis[to]=1;node2.id=to;node2.step=node1.step+1;q.push(node2);if(node2.id==n) return node2.step/2+1; //因为管道也算了步数,所以要node2.step/2+1 }}return -1;}int main(){int T;scanf("%d",&T);while(T--){int k,m,from,to;num=0;scanf("%d %d %d",&n,&k,&m);for(int i=0;i<=n+m;i++) //初始化 { head[i]=-1;vis[i]=0;}for(int i=1;i<=m;i++){from=n+i; //另加的点,表示管道 for(int j=1;j<=k;j++){//scanf("%d",&to);to=read();Add(to,from,1);Add(from,to,1);}}int ans=bfs();printf("%d\n",ans);}}
0 0
- zzuli 2130: hipercijevi
- zzuli 2130 hipercijevi bfs + 链式前向星 + 输入外挂
- zzuli 2130: hipercijevi bfs+输入输出外挂 (2017轻工业校赛 )
- 【zzulioj 2130 hipercijevi】+ BFS + 链式前向星 +输入输出外挂
- ZZULIOJ 2130 hipercijevi【Bfs+FastIO】卡FastIO和Vector好无聊啊-
- [玲珑杯] Problem D: hipercijevi [bfs][卡时]
- zzuli 1530
- zzuli 1731
- zzuli 1895
- zzuli 1904
- zzuli D
- zzuli-1919
- zzuli 1921
- zzuli 1922
- zzuli 1919
- zzuli 1895
- zzuli 1916
- 一棵树呀一棵树~~~zzuli
- LeetCode078 Subsets
- 一个简单的贪吃蛇
- java学习笔记13——JDBC初步
- Python中使用NLTK库解决错误:LookupError: from nltk.book import
- Canvas中的drawImage
- zzuli 2130: hipercijevi
- hpuoj 杨辉三角
- Specified web-root folder does not exist
- 破解QTP10插件时,注册表中没有HKEY_LOCAL_MACHINE -- SOFTWARE -- mercury Interactive我们该怎么办?
- 【GDSOI2017模拟】树
- 千年忆
- V1.03
- Matlab编程 入门(二)
- HTML基础知识(二)