結果

問題 No.90 品物の並び替え
ユーザー akakimidoriakakimidori
提出日時 2016-12-10 17:43:23
言語 C90
(gcc 11.4.0)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 798 bytes
コンパイル時間 130 ms
コンパイル使用メモリ 25,792 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-19 10:23:12
合計ジャッジ時間 755 ms
ジャッジサーバーID
(参考情報)
judge12 / judge9
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 0 ms
4,376 KB
testcase_02 AC 0 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 0 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 0 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 0 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
#include<stdlib.h>

int func(int bit,int n,int *dp,int *score){
  if(dp[bit]!=-1){
    return dp[bit];
  }

  int max=0;
  int i;
  for(i=0;i<n;i++){
    if((bit>>i)&0x01){
      int t=func(bit^(1<<i),n,dp,score);
      int j;
      for(j=0;j<n;j++){
        t+=((bit>>j)&0x01)*score[i*n+j];
      }
      max=(max<t?t:max);
    }
  }
  dp[bit]=max;
  return max;
}

void run(void){
  int n,m;
  scanf("%d%d",&n,&m);

  int *score=(int *)calloc(n*n,sizeof(int));

  int i;
  for(i=0;i<m;i++){
    int x,y,t;
    scanf("%d%d%d",&x,&y,&t);
    score[x*n+y]=t;
  }

  int *dp;
  dp=(int *)malloc(sizeof(int)*(1<<n));
  for(i=0;i<(1<<n);i++){
    dp[i]=-1;
  }
  dp[0]=0;

  int ans=func((1<<n)-1,n,dp,score);
  printf("%d\n",ans);
  return;
}

int main(void){
  run();
  return 0;
}
0