import java.util.*; public class Main { static long[] a; static long[][] c; static long[][] dp; static long res; static ArrayList sta;

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int m = sc.nextInt();
    int k = sc.nextInt();
    
    a = new long[n];
    c = new long[n][n];
    dp = new long[1<<18][n];
    res = 0;
    sta = new ArrayList<Integer>();
    
    for (int i = 0; i < n; i++) {
        a[i] = sc.nextLong();
    }
    
    for (int i = 0; i < k; i++) {
        int x = sc.nextInt();
        int y = sc.nextInt();
        int z = sc.nextInt();
        c[x-1][y-1] = z;
    }
    
    for (int i = 0; i < n; i++) {
        Arrays.fill(dp[i], -10000000000L);
    }
    
    for (int i = 0; i < n; i++) {
        dp[0][i] = 0;
        dp[1<<i][i] = a[i];
    }
    
    for (int i = 1; i < (1<<n); i++) {
        if (count(i) == m) {
            sta.add(i);
        }
        for (int j = 0; j < n; j++) {
            if ((i & (1<<j)) == 0) {
                for (int k = 0; k < n; k++) {
                    if ((i & (1<<k)) != 0) {
                        dp[i|(1<<j)][j] = Math.max(dp[i|(1<<j)][j], dp[i][k]+a[j]+c[k][j]);
                    }
                }
            }
        }
    }
    
    int sz = sta.size();
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < sz; j++) {
            res = Math.max(res, dp[sta.get(j)][i]);
        }
    }
    
    System.out.println(res);
}

public static int count(int x) {
    int s = 0;
    while (x != 0) {
        if ((x & 1) != 0) {
            s++;
        }
        x >>= 1;
    }
    return s;
}
请用java语言转换下面的c++代码:#includebitsstdc++husing namespace std;typedef long long ll;#define repixy forint i=x;i=y;++iint nmk;ll a20c2020dp11820res;vectorintsta;int countint x int s=0; whilex i

原文地址: https://www.cveoy.top/t/topic/iF3U 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录