Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class ShopAndShip {
- public static int minIndex(int[] d, byte[] c) {
- int min = 100000;
- int index = 0;
- for (int i = 0; i < d.length; i++) {
- if (c[i] == 0 && d[i] < min) {
- min = d[i];
- index = i;
- }
- }
- return index;
- }
- public static void main (String[] args) {
- Scanner s = new Scanner(System.in);
- int n = s.nextInt();
- int[][] graph = new int[n][n];
- int[] dist = new int[n];
- int[] cost = new int[n];
- byte[] checked = new byte[n];
- int t = s.nextInt();
- for (int i = 0; i < n; i++) {
- cost[i] = -1;
- }
- for (int i = 0; i < t; i++) {
- int x = s.nextInt() - 1;
- int y = s.nextInt() - 1;
- int c = s.nextInt();
- graph[x][y] = c;
- graph[y][x] = c;
- }
- int k = s.nextInt();
- for (int i = 0; i < k; i++) {
- int z = s.nextInt() - 1;
- int p = s.nextInt();
- cost[z] = p;
- }
- int d = s.nextInt() - 1;
- for (int i = 0; i < n; i++) {
- if (i != d)
- dist[i] = 100000;
- else
- dist[i] = 0;
- }
- for (int i = 0; i < n; i++) {
- int v = minIndex(dist, checked);
- for (int u = 0; u < n; u++) {
- if (graph[u][v] != 0 && dist[v] + graph[u][v] < dist[u])
- dist[u] = dist[v] + graph[u][v];
- }
- checked[v] = 1;
- }
- int min = 100000;
- for (int i = 0; i < n; i++) {
- if (cost[i] != -1) {
- cost[i] += dist[i];
- if (cost[i] < min)
- min = cost[i];
- }
- }
- System.out.println(min);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement