fork download
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. import static java.lang.Math.max;
  5. import static java.lang.Math.min;
  6. import static java.lang.Math.abs;
  7.  
  8. public class Main {
  9. public static int mod = (int) 1e9 + 7;
  10.  
  11. static class FastReader {
  12.  
  13. public FastReader() {
  14. }
  15.  
  16. String next() {
  17. while (st == null || !st.hasMoreTokens()) {
  18. try {
  19. st = new StringTokenizer(br.readLine());
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. return st.nextToken();
  25. }
  26.  
  27. int nextInt() {
  28. return Integer.parseInt(next());
  29. }
  30.  
  31. long nextLong() {
  32. return Long.parseLong(next());
  33. }
  34.  
  35. double nextDouble() {
  36. return Double.parseDouble(next());
  37. }
  38.  
  39. String nextLine() {
  40. String str = "";
  41. try {
  42. str = br.readLine().trim();
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. }
  46. return str;
  47. }
  48. }
  49.  
  50. static class FastWriter {
  51. private final BufferedWriter bw;
  52.  
  53. public FastWriter() {
  54. this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
  55. }
  56.  
  57. public void print(Object object) throws IOException {
  58. bw.append("" + object);
  59. }
  60.  
  61. public void println(Object object) throws IOException {
  62. print(object);
  63. bw.append("\n");
  64. }
  65.  
  66. public void println() throws IOException {
  67. bw.append("\n");
  68. }
  69.  
  70. public void close() throws IOException {
  71. bw.close();
  72. }
  73.  
  74. public void printLongArr(long[] arr) throws IOException {
  75. for (long ele : arr) {
  76. print(ele + " ");
  77. }
  78. println();
  79. }
  80.  
  81. public void printIntArr(int[] arr) throws IOException {
  82. for (int ele : arr) {
  83. print(ele + " ");
  84. }
  85. println();
  86. }
  87. }
  88.  
  89. public static void main
  90. (String[] args) {
  91. try {
  92. FastReader fin = new FastReader();
  93. FastWriter fout = new FastWriter();
  94.  
  95. int t = fin.nextInt();
  96. while (t-- > 0) {
  97. int n = fin.nextInt();
  98. int m = fin.nextInt();
  99. int b = fin.nextInt();
  100. List<int[]> parcels = new ArrayList<>();
  101. int[][] edges = new int[m][3];
  102. for (int i = 0; i < m; i++) {
  103. edges[i][0] = fin.nextInt();
  104. edges[i][1] = fin.nextInt();
  105. edges[i][2] = fin.nextInt();
  106. }
  107. int z = fin.nextInt();
  108. int[][] requests = new int[z][3];
  109. for (int i = 0; i < z; i++) {
  110. int u = fin.nextInt();
  111. int v = fin.nextInt();
  112. int count = fin.nextInt();
  113. for (int k = 0; k < count; k++) {
  114. parcels.add(new int[]{u, v});
  115. }
  116. }
  117. int p = parcels.size();
  118. int[][] allSourceShortestMap = getAllPairShortestPath(edges, n);
  119. long dp[][] = new long[n + 1][1 << p];
  120. for(long dp1[] : dp){
  121. Arrays.fill(dp1,-1);
  122. }
  123. fout.println(recusiveHelper(b, 0, dp, allSourceShortestMap, parcels, b));
  124.  
  125. }
  126.  
  127. fout.close();
  128. } catch (Exception e) {
  129. e.printStackTrace();
  130. return;
  131. }
  132. }
  133.  
  134. // dp[i][mask] -> minimum cost to deliver packages represented by mask when courier is at location i
  135. private static long recusiveHelper(int i, int mask, long[][] dp, int[][] allSourceShortestMap, List<int[]> parcels, int b) {
  136. if (mask == (1 << parcels.size()) - 1) {
  137. return allSourceShortestMap[i][b];
  138. }
  139. if (dp[i][mask] != -1) {
  140. return dp[i][mask];
  141. }
  142. long minCost = Long.MAX_VALUE;
  143. for (int j = 0; j < parcels.size(); j++) {
  144. if ((mask & (1 << j)) == 0) {
  145. // taking the request
  146. int src = parcels.get(j)[0];
  147. int dest = parcels.get(j)[1];
  148. int travelCost = allSourceShortestMap[i][src] + allSourceShortestMap[src][dest];
  149. long costTaking = travelCost + recusiveHelper(dest, mask | (1 << j), dp, allSourceShortestMap, parcels, b);
  150. minCost = Math.min(minCost, costTaking);
  151. }
  152. }
  153. return dp[i][mask] = minCost;
  154. }
  155.  
  156. private static int[][] getAllPairShortestPath(int[][] edges, int n) {
  157. int[][] allSourceShortestPath = new int[n + 1][n + 1];
  158. for (int i =1; i <= n; i++) {
  159. allSourceShortestPath[i] = bellmanFord(i ,edges, n);
  160. }
  161. return allSourceShortestPath;
  162. }
  163.  
  164. private static int[] bellmanFord(int src, int[][] edges, int n) {
  165. int dist[] = new int[n + 1];
  166. Arrays.fill(dist, (int) 1e9);
  167. dist[src] = 0;
  168. for (int i = 1; i < n; i++) { // relaxing n-1 times
  169. for (int[] edge : edges) {
  170. int u = edge[0];
  171. int v = edge[1];
  172. int edgeWeight = edge[2];
  173. if (dist[u] + edgeWeight < dist[v]) {
  174. dist[v] = dist[u] + edgeWeight;
  175. }
  176. // as road are bidirectional so
  177. if (dist[v] + edgeWeight < dist[u]) {
  178. dist[u] = dist[v] + edgeWeight;
  179. }
  180. }
  181. }
  182. return dist;
  183. }
  184. }
  185.  
Success #stdin #stdout 0.12s 57420KB
stdin
1
5 7 2
1 2 7
1 3 5
1 5 2
2 4 10
2 5 1
3 4 3
3 5 4
3
1 4 2
5 3 1
5 1 1
stdout
43