|
| 1 | +```java |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | +class Main { |
| 7 | + |
| 8 | + // IO field |
| 9 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 10 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 11 | + static StringTokenizer st; |
| 12 | + |
| 13 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 14 | + static int nextInt() {return Integer.parseInt(st.nextToken());} |
| 15 | + static long nextLong() {return Long.parseLong(st.nextToken());} |
| 16 | + static void bwEnd() throws Exception {bw.flush();bw.close();} |
| 17 | + |
| 18 | + // Additional field |
| 19 | + static int N; |
| 20 | + static int[] M; |
| 21 | + static char[] T; |
| 22 | + static boolean[][] V; |
| 23 | + |
| 24 | + public static void main(String[] args) throws Exception { |
| 25 | + |
| 26 | + ready(); |
| 27 | + //solve(); |
| 28 | + |
| 29 | + bwEnd(); |
| 30 | + |
| 31 | + } |
| 32 | + |
| 33 | + static void ready() throws Exception{ |
| 34 | + |
| 35 | + N = Integer.parseInt(br.readLine()); |
| 36 | + while(N != 0) { |
| 37 | + |
| 38 | + M = new int[N+1]; |
| 39 | + T = new char[N+1]; |
| 40 | + V = new boolean[N+1][N+1]; |
| 41 | + |
| 42 | + for(int i=1;i<=N;i++) { |
| 43 | + |
| 44 | + nextLine(); |
| 45 | + T[i] = st.nextToken().charAt(0); |
| 46 | + M[i] = nextInt(); |
| 47 | + for(int a=nextInt();a!=0;a=nextInt()) V[i][a] = true; |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + solve(); |
| 52 | + |
| 53 | + N = Integer.parseInt(br.readLine()); |
| 54 | + } |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + static void solve() throws Exception{ |
| 59 | + |
| 60 | + if(T[1] == 'T' && M[1] > 0) { |
| 61 | + bw.write("No\n"); |
| 62 | + return; |
| 63 | + } |
| 64 | + PriorityQueue<int[]> PQ = new PriorityQueue<>((a,b) -> b[0]-a[0]); |
| 65 | + PQ.offer(new int[] {M[1],1}); |
| 66 | + int[] D = new int[N+1]; |
| 67 | + Arrays.fill(D, -1); |
| 68 | + D[1] = M[1]; |
| 69 | + |
| 70 | + while(!PQ.isEmpty()) { |
| 71 | + int[] now = PQ.poll(); |
| 72 | + int money = now[0], n = now[1]; |
| 73 | + if(n == N) { |
| 74 | + bw.write("Yes\n"); |
| 75 | + return; |
| 76 | + } |
| 77 | + if(money < D[n]) continue; |
| 78 | + for(int i=1;i<=N;i++) { |
| 79 | + if(!V[n][i]) continue; |
| 80 | + if(T[i] == 'T') { |
| 81 | + if(money < M[i]) continue; |
| 82 | + if(D[i] < money-M[i]) { |
| 83 | + D[i] = money-M[i]; |
| 84 | + PQ.offer(new int[] {D[i],i}); |
| 85 | + } |
| 86 | + } |
| 87 | + else { |
| 88 | + if(D[i] < Math.max(money, M[i])) { |
| 89 | + D[i] = Math.max(money, M[i]); |
| 90 | + PQ.offer(new int[] {D[i],i}); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + bw.write("No\n"); |
| 96 | + |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | +} |
| 101 | + |
| 102 | +``` |
0 commit comments