【题目链接】:https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=2130
【题意】
【题解】
把那个管泛化成一个点;
然后把每一个在管里面的点都和它相连;
然后从起点跑bfs就好;
最后输出dis[n]/2 +1
因为是点的数目所以要加1
然后每个点都要经过一个泛化的点再到其他点;
所以肯定边的数目是偶数个;
用了ios::sync_with_stdio(0)之后Puts不能用….
【Number Of WA】
4
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define rep1(i,x,y) for (int i = x;i <= y;i++)
#define pb push_back
#define LL long long
struct abc
{
int en,nex;
};
const int N = 1e5+1e3+100;
int n,k,m,dis[N],tot,fir[N],head,tail;
int dl[N];
abc bian[1000000*2+100];
void add(int x,int y)
{
tot++;
bian[tot].nex=fir[x];
fir[x] = tot;
bian[tot].en = y;
}
int main()
{
//freopen("D:\\rush.txt","r",stdin);
ios::sync_with_stdio(0);
int T;
cin >> T;
while (T--)
{
cin >> n >> k >> m;
tot = 0;
rep1(i,1,n+m)
fir[i] = 0;
rep1(i,1,m)
{
rep1(j,1,k)
{
int x;
cin >> x;
add(x,n+i);
add(n+i,x);
}
}
rep1(i,1,n+m)
dis[i]=-1;
dis[1]=0;
head = 0,tail = 1;
dl[1] = 1;
while (head<tail)
{
int x = dl[++head];
for (int i = fir[x];i;i=bian[i].nex)
{
int y = bian[i].en;
if (dis[y]==-1)
{
dis[y] = dis[x]+1;
dl[++tail]=y;
}
}
}
if (dis[n]==-1)
cout << -1 << endl;
else
cout << dis[n]/2 + 1<<endl;
}
return 0;
}