题目链接
解题思路
- 由于所有边集中的边加起来的总和至多为,无向图即,可以存下
- 所以直接对所有边集中的边进行建边,同时对于每条边,记录其所在边集号
- 对于每个边集,由大到小维护其能通过的时间点
- 然后从1号跑最短路
- 到当前点的时间为,走边集中的这条边
- 则找边集能通过的时间点中大于(至少等待1秒)的最小的时间点,用二分解决
import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Vector;
public class Main{
static long md=(long)998244353;
static long Linf=Long.MAX_VALUE/2;
static int inf=Integer.MAX_VALUE/2;
static
class Node{
int x,y;
public Node(int u,int v) {
x=u;
y=v;
}
@Override
public boolean equals(Object o) {
if(this==o)return true;
if(o==null||getClass()!=o.getClass())return false;
Node may=(Node)o;
return x==may.x&&y==may.y;
}
@Override
public int hashCode() {
return Objects.hash(x,y);
}
}
static
class Ti{
Vector<Integer> tim;
public Ti() {
tim=new Vector<Integer>();
}
}
static
class Edge{
int fr,to,val,nxt;
public Edge(int u,int v,int w) {
fr=u;
to=v;
val=w;
}
}
static Edge[] e;
static int[] head;
static int cnt=0;
static void addEdge(int fr,int to,int val) {
cnt++;
e[cnt]=new Edge(fr, to, val);
e[cnt].nxt=head[fr];
head[fr]=cnt;
}
public static void main(String[] args) throws IOException{
AReader input=new AReader();
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int n=input.nextInt();
int t=input.nextInt();
e=new Edge[400001];
head=new int[n+1];
cnt=0;
for(int i=1;i<=t;++i) {
int m=input.nextInt();
for(int j=1;j<=m;++j) {
int u=input.nextInt();
int v=input.nextInt();
addEdge(u, v, i);
addEdge(v, u, i);
}
}
Ti[] Dset=new Ti[t+1];
for(int i=1;i<=t;++i)Dset[i]=new Ti();
int k=input.nextInt();
for(int i=1;i<=k;++i) {
int x=input.nextInt();
Dset[x].tim.add(i);
}
long[] dis=new long[n+1];
Arrays.fill(dis, Linf);
boolean[] vis=new boolean[n+1];
dis[1]=0;
PriorityQueue<Node> q=new PriorityQueue<Node>((o1,o2)->{
return o1.y-o2.y;
});
q.add(new Node(1, 0));
while(!q.isEmpty()) {
Node now=q.peek();
q.poll();
int x=now.x;
if(vis[x])continue;
vis[x]=true;
for(int i=head[x];i>0;i=e[i].nxt) {
int v=e[i].to;
int di=e[i].val;
Vector<Integer> np=Dset[di].tim;
int l=0,r=np.size()-1;
int mi=inf;
while(l<=r) {
int mid=(l+r)>>1;
int ti=np.get(mid);
if(ti>dis[x]) {
mi=ti;
r=mid-1;
}else l=mid+1;
}
if(mi==inf)continue;
if(mi<dis[v]) {
dis[v]=mi;
q.add(new Node(v, mi));
}
}
}
if(dis[n]==Linf)out.print("-1");
else out.print(dis[n]);
out.flush();
out.close();
}
//System.out.println();
//out.println();
static
class AReader{
BufferedReader bf;
StringTokenizer st;
BufferedWriter bw;
public AReader(){
bf=new BufferedReader(new InputStreamReader(System.in));
st=new StringTokenizer("");
bw=new BufferedWriter(new OutputStreamWriter(System.out));
}
public String nextLine() throws IOException{
return bf.readLine();
}
public String next() throws IOException{
while(!st.hasMoreTokens()){
st=new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
public char nextChar() throws IOException{
//确定下一个token只有一个字符的时候再用
return next().charAt(0);
}
public int nextInt() throws IOException{
return Integer.parseInt(next());
}
public long nextLong() throws IOException{
return Long.parseLong(next());
}
public double nextDouble() throws IOException{
return Double.parseDouble(next());
}
public float nextFloat() throws IOException{
return Float.parseFloat(next());
}
public byte nextByte() throws IOException{
return Byte.parseByte(next());
}
public short nextShort() throws IOException{
return Short.parseShort(next());
}
public BigInteger nextBigInteger() throws IOException{
return new BigInteger(next());
}
public void println() throws IOException {
bw.newLine();
}
public void println(int[] arr) throws IOException{
for (int value : arr) {
bw.write(value + " ");
}
println();
}
public void println(int l, int r, int[] arr) throws IOException{
for (int i = l; i <= r; i ++) {
bw.write(arr[i] + " ");
}
println();
}
public void println(int a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(int a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(String a) throws IOException{
bw.write(a);
bw.newLine();
}
public void print(String a) throws IOException{
bw.write(a);
}
public void println(long a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(long a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(double a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(double a) throws IOException{
bw.write(String.valueOf(a));
}
public void print(char a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(char a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
}
}