|
| 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 INF = (int)1e9 + 1; |
| 20 | + static int N, P, M, G, T; |
| 21 | + static int[] p; |
| 22 | + static int[] num; |
| 23 | + static List<int[]>[] V; |
| 24 | + static int[][] dist; |
| 25 | + static int[][] cost; |
| 26 | + static int[][][] dp; |
| 27 | + |
| 28 | + static void sizeInitialize() { |
| 29 | + p = new int[P+1]; |
| 30 | + num = new int[N+1]; |
| 31 | + V = new ArrayList[N+1]; |
| 32 | + for(int i=0;i<=N;i++) V[i] = new ArrayList<>(); |
| 33 | + dist = new int[P+1][N+1]; |
| 34 | + cost = new int[P+1][P+1]; |
| 35 | + dp = new int[P+1][2][1<<(P+1)]; |
| 36 | + for(int i=0;i<=P;i++) for(int j=0;j<2;j++) Arrays.fill(dp[i][j], INF); |
| 37 | + } |
| 38 | + |
| 39 | + static void input() throws Exception { |
| 40 | + |
| 41 | + nextLine(); |
| 42 | + N = nextInt(); |
| 43 | + P = nextInt(); |
| 44 | + M = nextInt(); |
| 45 | + G = nextInt(); |
| 46 | + T = nextInt(); |
| 47 | + |
| 48 | + sizeInitialize(); |
| 49 | + |
| 50 | + for(int i=1;i<=P;i++) { |
| 51 | + nextLine(); |
| 52 | + int a = nextInt(), b = nextInt(); |
| 53 | + G -= b; |
| 54 | + p[i] = a; |
| 55 | + num[p[i]] = i; |
| 56 | + } |
| 57 | + |
| 58 | + for(int i=0;i<M;i++) { |
| 59 | + nextLine(); |
| 60 | + int a = nextInt(), b = nextInt(), c = nextInt(); |
| 61 | + V[a].add(new int[] {b,c}); |
| 62 | + V[b].add(new int[] {a,c}); |
| 63 | + } |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + static void dijk(int s) { |
| 68 | + Arrays.fill(dist[s], INF); |
| 69 | + |
| 70 | + PriorityQueue<int[]> PQ = new PriorityQueue<>((a,b) -> a[0]-b[0]); |
| 71 | + PQ.offer(new int[] {0,p[s]}); |
| 72 | + dist[s][p[s]] = 0; |
| 73 | + while(!PQ.isEmpty()) { |
| 74 | + int[] now = PQ.poll(); |
| 75 | + int d = now[0], n = now[1]; |
| 76 | + if(d > dist[s][n]) continue; |
| 77 | + for(int[] x : V[n]) { |
| 78 | + int nx = x[0], c = x[1]; |
| 79 | + if(dist[s][nx] > d + c) { |
| 80 | + dist[s][nx] = d+c; |
| 81 | + PQ.offer(new int[] {d+c,nx}); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + static void constructNewGraph() { |
| 89 | + for(int i=0;i<=P;i++) for(int j=0;j<=P;j++) cost[i][j] = dist[i][p[j]]; |
| 90 | + } |
| 91 | + |
| 92 | + static int solve(int n, int x, int k) { |
| 93 | + if(dp[n][x][k] != INF) return dp[n][x][k]; |
| 94 | + int prev = k ^ (1<<n); |
| 95 | + for(int i=0;i<=P;i++) { |
| 96 | + if((prev & (1<<i)) == 0) continue; |
| 97 | + // no taxi |
| 98 | + dp[n][x][k] = Math.min(dp[n][x][k], solve(i,x,prev) + cost[i][n]); |
| 99 | + // can taxi |
| 100 | + if(x == 1) dp[n][x][k] = Math.min(dp[n][x][k], solve(i,0,prev) + T); |
| 101 | + } |
| 102 | + |
| 103 | + return dp[n][x][k]; |
| 104 | + } |
| 105 | + |
| 106 | + public static void main(String[] args) throws Exception { |
| 107 | + |
| 108 | + input(); |
| 109 | + |
| 110 | + for(int i=0;i<=P;i++) dijk(i); |
| 111 | + |
| 112 | + constructNewGraph(); |
| 113 | + |
| 114 | + for(int i=1;i<=P;i++) { |
| 115 | + dp[i][0][1<<i] = cost[0][i]; |
| 116 | + dp[i][1][1<<i] = T; |
| 117 | + } |
| 118 | + int withoutTaxi = solve(0,0,(1<<(P+1))-1); |
| 119 | + int withTaxi = solve(0,1,(1<<(P+1))-1); |
| 120 | + |
| 121 | + if(withoutTaxi <= G) bw.write("possible without taxi"); |
| 122 | + else if(withTaxi <= G) bw.write("possible with taxi"); |
| 123 | + else bw.write("impossible"); |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + bwEnd(); |
| 128 | + } |
| 129 | + |
| 130 | +} |
| 131 | + |
| 132 | +``` |
0 commit comments