結果

提出番号 1728
提出者 inmir
言語 Java
提出日時 2018-08-04 13:39:47
問題名 (70)アルゴリズムのお勉強
結果 AC
点数 100%

テストケース

テストケース 結果 得点 実行時間 メモリ使用量
1 AC 100% 55ms 93712KB
2 AC 100% 63ms 93376KB
3 AC 100% 59ms 92336KB
4 AC 100% 60ms 91776KB
5 AC 100% 58ms 92112KB
6 AC 100% 61ms 93616KB
7 AC 100% 60ms 92928KB
8 AC 100% 64ms 93536KB
9 AC 100% 58ms 92624KB
10 AC 100% 64ms 91776KB
11 AC 100% 64ms 93168KB
12 AC 100% 67ms 92528KB
13 AC 100% 62ms 94128KB
14 AC 100% 55ms 92080KB
15 AC 100% 63ms 92096KB
16 AC 100% 65ms 93040KB
17 AC 100% 63ms 92624KB
18 AC 100% 62ms 93264KB
19 AC 100% 74ms 96816KB
20 AC 100% 77ms 96656KB
21 AC 100% 60ms 92672KB
22 AC 100% 81ms 96592KB
23 AC 100% 60ms 93968KB
24 AC 100% 86ms 96608KB
25 AC 100% 64ms 95504KB
26 AC 100% 83ms 96112KB
27 AC 100% 80ms 97376KB
28 AC 100% 64ms 94992KB
29 AC 100% 93ms 99664KB
30 AC 100% 77ms 96832KB

ソースコード

import java.io.IOException; 
import java.io.InputStream; 
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Main{ 

	static void solve(){ 
		int n = ni();
		int[] t = nia(n);
		int[][] a = new int[n][n];
		for(int i=0;i<n;++i)for(int j=0;j<n;++j)a[i][j]=Math.max(0, ni());
		int[] dp = new int[1<<n];
		Arrays.fill(dp, Integer.MAX_VALUE);
		dp[0]=0;
		for(int i=1;i<=n;i++){
			for(int comb = (1<<i)-1;comb<(1<<n);comb = ((comb&~(comb+(comb&-comb)))/(comb&-comb)>>1)|comb+(comb&-comb)){
				for(int j=0;j<n;++j)if(((comb>>j)&1)==1){
					int max = 0;
					for(int k=0;k<n;++k)if(k!=j)if(((comb>>k)&1)==1)max+=a[k][j];
					dp[comb]=Math.min(dp[comb], dp[comb^(1<<j)]+Math.max(0, t[j]-max));
				}
			}
		}
		out.println(dp[(1<<n)-1]);
	} 
 
 
 
 
	public static void main(String[] args){ 
		 solve(); 
		 out.flush(); 
	 } 
	 private static InputStream in = System.in; 
	 private static PrintWriter out = new PrintWriter(System.out); 
 
	 private static final byte[] buffer = new byte[1<<15]; 
	 private static int ptr = 0; 
	 private static int buflen = 0; 
	 private static boolean hasNextByte(){ 
		 if(ptr<buflen)return true; 
		 ptr = 0; 
		 try{ 
			 buflen = in.read(buffer); 
		 } catch (IOException e){ 
			 e.printStackTrace(); 
		 } 
		 return buflen>0; 
	 } 
	 private static int readByte(){ if(hasNextByte()) return buffer[ptr++]; else return -1;} 
	 private static boolean isSpaceChar(int c){ return !(33<=c && c<=126);} 
	 private static int skip(){int res; while((res=readByte())!=-1 && isSpaceChar(res)); return res;} 
 
	 private static double nd(){ return Double.parseDouble(ns()); } 
	 private static char nc(){ return (char)skip(); } 
	 private static String ns(){ 
		 StringBuilder sb = new StringBuilder(); 
		 for(int b=skip();!isSpaceChar(b);b=readByte())sb.append((char)b); 
		 return sb.toString(); 
	 } 
	 private static int[] nia(int n){ 
		 int[] res = new int[n]; 
		 for(int i=0;i<n;++i)res[i]=ni(); 
		 return res; 
	 } 
	 private static long[] nla(int n){ 
		 long[] res = new long[n]; 
		 for(int i=0;i<n;++i)res[i]=nl(); 
		 return res; 
	 } 
	 private static int ni(){ 
		 int res=0,b; 
		 boolean minus=false; 
		 while((b=readByte())!=-1 && !((b>='0'&&b<='9') || b=='-')); 
		 if(b=='-'){ 
			 minus=true; 
			 b=readByte(); 
		 } 
		 for(;'0'<=b&&b<='9';b=readByte())res=res*10+(b-'0'); 
		 return minus ? -res:res; 
	 } 
	 private static long nl(){ 
		 long res=0,b; 
		 boolean minus=false; 
		 while((b=readByte())!=-1 && !((b>='0'&&b<='9') || b=='-')); 
		 if(b=='-'){ 
			 minus=true; 
			 b=readByte(); 
		 } 
		 for(;'0'<=b&&b<='9';b=readByte())res=res*10+(b-'0'); 
		 return minus ? -res:res; 
	} 
}