我是靠谱客的博主 清爽睫毛,这篇文章主要介绍AtCoder Beginner Contest 277 E - Crystal Switches(最短路)题意:思路:AC,现在分享给大家,希望可以做个参考。

pokemon朱紫偷跑,(主要是调环境,弄了好久,下午才玩上,A卡太惨了)打了一天,有点懈怠。最后8:30才吃完饭,就慢慢写了

题意:

给你n个点, m m m条边.
每条边有初始状态 a [ i ] a[i] a[i] a [ i ] = 0 a[i] = 0 a[i]=0表示不可以通过, a [ i ] = 1 a[i] = 1 a[i]=1表示可以通过。

  • k k k个点有开关,
  • 在有开关的点上,可以将所有边的状态改变。

走最少步,从1 → rightarrow n

思路:

d p [ x ] [ 0 / 1 ] dp[x][0/1] dp[x][0/1]表示这个点的状态
之后跑bfs就行了,边权为1.

笔者,赛时,跑了最短路。。

AC

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.hgs.atcoder.abc.contest277.e; /** * @author youtsuha * @version 1.0 * Create by 2022/11/12 19:58 */ import java.util.*; import java.io.*; public class Main { static FastScanner cin; static PrintWriter cout; static int [][] dp; private static void init()throws IOException { cin = new FastScanner(System.in); cout = new PrintWriter(System.out); } private static void close(){ cout.close(); } static int n, m, k; static class node{ int a, b, c; //[] public node(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } } static List<node> e[]; static int ans = (int) 1e9; static int inf = (int) 1e9; static boolean isSwitch[]; // static boolean dfs(int x, int fa, int rot, int dep){ // // } private static void sol()throws IOException { n = cin.nextInt(); m = cin.nextInt(); k = cin.nextInt(); dp = new int[n][2]; // Arrays.fill(dp[0],inf); // Arrays.fill(dp[1],inf); for(int j = 0; j < 2; j ++ ) { for(int i = 0; i < n; i ++ ){ //cout.print(dp[i][j] + " "); dp[i][j] = inf; } // cout.println(""); } e = new ArrayList[n]; isSwitch = new boolean[n]; for(int i = 0; i < n; i ++ ) { e[i] = new ArrayList<>(); } for(int i = 0; i < m; i ++ ) { int a = cin.nextInt(), b = cin.nextInt(), c = cin.nextInt(); a--; b--; e[a].add(new node(b,c,0)); e[b].add(new node(a,c,0)); } for(int i = 0; i < k; i ++ ) { int x = cin.nextInt(); x--; isSwitch[x] = true; } // dfs(0,-1,0, 0); PriorityQueue<node> que = new PriorityQueue<>((a,b)->a.c - b.c); dp[0][0] = 0; que.add(new node(0,0,0)); while(!que.isEmpty()){ int x = que.peek().a, d = que.peek().c, rot= que.peek().b; que.poll(); if(dp[x][rot] > d) { continue; } for(int i = 0; i < e[x].size(); i ++ ) { int v = e[x].get(i).a, p = e[x].get(i).b; int nxt = (p + rot) % 2; if(nxt % 2 == 1){ if(dp[v][rot] > d + 1){ dp[v][rot] = Math.min(dp[v][rot], d + 1); que.add(new node(v,rot,d+1)); } }else if(isSwitch[x]){ nxt = (rot + 1) % 2; if(dp[v][nxt] > d + 1){ dp[v][nxt] = Math.min(dp[v][nxt], d + 1); que.add(new node(v,nxt, d + 1)); } } } } int ans = inf; if(dp[n-1][0] != inf) { ans = Math.min(ans, dp[n-1][0]); }if(dp[n-1][1] != inf) { ans = Math.min(ans, dp[n-1][1]); } if(ans == inf) { cout.println(-1); }else { cout.println(ans); } } public static void main(String[] args) throws IOException { init(); sol(); close(); } } class FastScanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); public FastScanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public FastScanner(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } public String next() throws IOException { while (!st.hasMoreTokens()){ try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } 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()); } }

最后

以上就是清爽睫毛最近收集整理的关于AtCoder Beginner Contest 277 E - Crystal Switches(最短路)题意:思路:AC的全部内容,更多相关AtCoder内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部