【图论】B洛谷 P3393 逃离僵尸岛(多源 bfs + Dijkstra )

一、题目描述

小a住的国家被僵尸侵略了!小a打算逃离到该国唯一的国际空港逃出这个国家。

该国有N个城市,城市之间有道路相连。一共有M条双向道路。保证没有自环和重边。

K个城市已经被僵尸控制了,如果贸然闯入就会被感染TAT…所以不能进入。由其中任意城市经过不超过S条道路就可以到达的别的城市,就是危险城市。换句话说只要某个没有被占城市到某个被占城市不超过 S 距离,就是危险。

小a住在1号城市,国际空港在N号城市,这两座城市没有被侵略。小a走每一段道路(从一个城市直接到达另外一个城市)得花一整个白天,所以晚上要住旅店。安全的的城市旅馆比较便宜要P元,而被危险的城市,旅馆要进行安保措施,所以会变贵,为Q元。所有危险的城市的住宿价格一样,安全的城市也是。在1号城市和N城市,不需要住店。

小a比较抠门,所以他希望知道从1号城市到N号城市所需要的最小花费。

输入数据保证存在路径,可以成功逃离。输入数据保证他可以逃离成功。

Input

  • 第一行4个整数(N,M,K,S)
    第二行2个整数(P,Q)
    接下来K行,ci,表示僵尸侵占的城市
    接下来M行,ai,bi,表示一条无向边

Output

  • 一个整数表示最低花费

数据范围与约定

  • 对于20%数据,N<=50
    对于100%数据,2 ≦ N ≦ 100000, 1 ≦ M ≦ 200000, 0 ≦ K ≦ N - 2, 0 ≦ S ≦ 100000
    1 ≦ P < Q ≦ 100000
in
13 21 1 1
1000 6000
7
1 2
3 7
2 4
5 8
8 9
2 5
3 4
4 7
9 10
10 11
5 9
7 12
3 6
4 5
1 3
11 12
6 7
8 11
6 13
7 8
12 13

out
11000

二、题解

题意:有 K 个僵尸城市,僵尸城市距离 S 内的城市为危险城市,S 距离以外的城市为安全城市,安全城市住宿价格为 P,危险城市住宿价格为 Q,问小 a 要从点 1 走到点 V 至少要花多少钱?

方法一:多源 bfs + Dijkstra

思路

  • 因为危险城市与安全城市的住宿价格不一,所以我们要加以标记区分开来。
    • 如何区分标记范围内的城市,可用单源 bfs 或者多源 bfs。
  • 这里的权值可以用 Node[] 存储每个点的点权,也可以将点权转化为边权。
  • 最后,就是使用最短路算法求 dist[N] 的大小了。

超多 Bug:代办…

import java.util.*;
import java.math.*;
import java.io.*;
public class Main{
    static class Node {
		int id, d;
		public Node(int id, int d) {
			this.id = id;
			this.d = d;
		}
	}
	static class Edge {
        int to, w, next;
        Edge() {}
    } 
	static int V, E, K, S, lo, hi;
	static Edge[] edges;
	static int[] dist, head;
	static int[] z;				//记录僵尸占领的城市编号
	static int tot;
	static int INF = 0x3f3f3f3f;
	static int[] safe;			//约定 0:安全;1:危险城市;2:僵尸城市
	static Queue<Node> q;
	static void addEdge(int u, int v) {
		edges[++tot] = new Edge();
		edges[tot].to = v;
		head[u] = tot;
	}
    public static void main(String[] args) throws IOException {  
        Scanner sc = new Scanner(new BufferedInputStream(System.in));
        BufferedWriter w = new BufferedWriter(new OutputStreamWriter(System.out));
        
		V = sc.nextInt();
		E = sc.nextInt();
		K = sc.nextInt();	//K个僵尸城市
		S = sc.nextInt();	//距离感染源不超过 S 就是危险的城市
		lo = sc.nextInt();	//便宜房
		hi = sc.nextInt();	//贵房
		
		dist = new int[V+1];
		edges = new Edge[2*E+1];
		head = new int[V+1];
		z = new int[V+1];		//僵尸
		safe = new int[V+1];	//记录城市编号
		q = new ArrayDeque<>();
		
		for (int i = 1; i <= K; i++) {
			z[i] = sc.nextInt();
			safe[z[i]] = 2;
		}
		for (int i = 0; i < E; i++) {
			int a = sc.nextInt();
			int b = sc.nextInt();
			addEdge(a, b);
			addEdge(b, a);
		}
		for (int opy : z) {		//多源bfs
			q.add(new Node(opy, 0));
		}
		bfs();
		//点权转边权
		for (int i = 1; i <= V; i++) {
			int v = edges[i].to;
			if (v == 1 || v == V) {
				edges[i].w = 0;
			} else {
				if (safe[v] == 0) {
					edges[i].w = lo;
				} else if (safe[v] == 1) {
					edges[i].w = hi;
				} else {
					edges[i].w = 0x7fffffff;
				}
			}
		}
		dijkstra(1);
		System.out.println(dist[V]);
    }
    //渲染危险城市
	static void bfs() {
		boolean[] vis = new boolean[V+1];
		while (!q.isEmpty()) {
			Node now = q.poll();
			int v = now.id;
			if (vis[v]) 
				continue;
			vis[v] = true;
			for (int i = head[v]; i != 0; i = edges[i].next) {
				int to = edges[i].to;
				if (now.d < S && !vis[to] && safe[to] != 2) {	//如果在可能感染范围内 && 没有被染色 && 非僵尸城市
					safe[to] = 1;								//标记为危险城市1
					q.add(new Node(to, now.d+1));
				}
			}
		}
	}
	//求最小花费
	static void dijkstra(int S) {
		boolean[] vis = new boolean[V+1];
		Arrays.fill(dist, INF);
		dist[S] = 0;
		Queue<Node> pq = new PriorityQueue<>((e1, e2) -> e1.d - e2.d);
		pq.add(new Node(S, 0));
		
		while (!pq.isEmpty()) {
			Node t = pq.poll();
			int v = t.id;
			if (vis[v])
				continue;
			vis[v] = true;
			for (int i = head[v]; i != 0; i = edges[i].next) {
				int to = edges[i].to, w = edges[i].w;
				if (!vis[to] && dist[to] > dist[v] + w) {
					dist[to] = dist[v] + w;
					q.add(new Node(to, dist[to]));
				}
			}
		}
	}
}

复杂度分析

  • 时间复杂度: O ( E l o g V ) O(ElogV)
  • 空间复杂度: O ( . . . ) O(...)

发布了691 篇原创文章 · 获赞 151 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_43539599/article/details/105540307
今日推荐