結果

問題 No.357 品物の並び替え (Middle)
ユーザー akakimidoriakakimidori
提出日時 2017-04-25 17:01:10
言語 C90
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 889 bytes
コンパイル時間 898 ms
コンパイル使用メモリ 25,492 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-10-11 09:41:41
合計ジャッジ時間 1,811 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

#define POS(i,j) ((i)*n+(j))
#define MAX(a,b) ((a)>(b)?(a):(b))

int calc(int bit,int n,int *score,int *dp){
  if(dp[bit]>=0) return dp[bit];

  int res=0;
  int i;
  for(i=0;i<n;i++){
    if((bit>>i)&0x01){
      int j;
      int point=0;
      for(j=0;j<n;j++){
	point+=((bit>>j)&0x01)*score[POS(i,j)];
      }
      res=MAX(res,point+calc(bit^(1<<i),n,score,dp));
    }
  }
  dp[bit]=res;
  return res;
}

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 i1,i2,p;
    scanf("%d%d%d",&i1,&i2,&p);
    score[POS(i1,i2)]=p;
  }

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

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

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