Untitled

mail@pastecode.io avatar
unknown
java
a year ago
1.8 kB
9
Indexable
Never
import java.util.*;

class pair{
    int first;
    int second;
    public pair(int first,int second){
        this.first=first;
        this.second=second;
    }
}

public class Main
{
    public static int helper(int start,List<List<pair>> adj,int[] costs){
        Queue<pair> q = new LinkedList<>();
        q.add(new pair(start,costs[start]));
        
        int[] p = new int[costs.length];
        Arrays.fill(p,-1);
        PriorityQueue<pair> pq = new PriorityQueue<>();
        p[start]=costs[start];
        int c=0;
        
        while(!q.isEmpty()){
            pair temp = q.remove();
            int node = temp.first;
            int dist = temp.second;
            c+=dist;
            
            for(pair x:adj.get(node)){
                int n = x.first;
                int d = x.second;
                if(p[n]!=-1) continue;
                
                if(d>costs[n]){
                    q.add(new pair(n,costs[n]));
                    p[n]=costs[n]+dist;
                }
                else{
                    q.add(new pair(n,d));
                    p[n]=dist+d;
                }
            }
        }
        return c;
    }
    
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n=in.nextInt();
		int[] costs=new int[n];
		List<List<pair>> adj = new ArrayList<>();
		for(int i=0;i<n;i++){
		    costs[i]=in.nextInt();
		    adj.add(new ArrayList<>());
		}
		
		int w=in.nextInt();

		for(int i=0;i<w;i++){
		    int a=in.nextInt();
		    int b=in.nextInt();
		    int c=in.nextInt();
		    
		    adj.get(a).add(new pair(b,c));
		    adj.get(b).add(new pair(a,c));
		}
	
		int result = Integer.MAX_VALUE;
		for(int i=0;i<n;i++){
		    result = Math.min(result,helper(i,adj,costs));
		}
		System.out.println(result);
	}
}