結果

問題 No.357 品物の並び替え (Middle)
ユーザー 👑 horiesinitihoriesiniti
提出日時 2016-07-21 09:47:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 25 ms / 5,000 ms
コード長 1,156 bytes
コンパイル時間 674 ms
コンパイル使用メモリ 72,364 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-23 19:25:15
合計ジャッジ時間 1,526 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 12 ms
5,376 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 25 ms
5,376 KB
testcase_13 AC 12 ms
5,376 KB
testcase_14 AC 12 ms
5,376 KB
testcase_15 AC 4 ms
5,376 KB
testcase_16 AC 6 ms
5,376 KB
testcase_17 AC 4 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:26:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   26 |         scanf("%d %d",&n,&m);
      |         ~~~~~^~~~~~~~~~~~~~~
main.cpp:30:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   30 |                 scanf("%d %d %d",&a,&b,&c);
      |                 ~~~~~^~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <stdio.h>
#include <map>
#include <string.h>
#include <algorithm>


int con[15][15];
int n;
int f(int perm,int p){
	int res=0;
	for(int i=0;i<n;i++){
		int b=1<<i;
		if((perm&b)==0)continue;
		if(con[i][p]!=-1){
			res+=con[i][p];
		}
	}
	return res;
}

int main() {
	// your code goes here
	int m;
	std::map<int,int> dp;
	scanf("%d %d",&n,&m);
	memset(con,-1,sizeof(con));
	for(int i=0;i<m;i++){
		int a,b,c;
		scanf("%d %d %d",&a,&b,&c);
		con[a][b]=c;
	}
	for(int i=0;i<n;i++){
		dp[1<<i]=0;
	}
	for(int i=1;i<n;i++){
		std::map<int,int> next;
		std::map<int,int>::iterator it;
		for(it=dp.begin();it!=dp.end();it++){
			int perm=(*it).first;
			int score=(*it).second;
			for(int j=0;j<n;j++){
				int bit=1<<j;
				if((perm&bit)!=0)continue;
				int perm2=perm|bit;
				int score2=score+f(perm,j);
				//printf("(%d %d %d)",perm,perm2,score2);
				if(next.find(perm2)==next.end()){
					next[perm2]=score2;
				}else if(next[perm2]<score2){
					next[perm2]=score2;
				}
			}
		}
		dp.clear();
		dp.insert(next.begin(),next.end());
	}
	std::map<int,int>::iterator it=dp.begin();
	printf("%d\n",(*it).second);
	return 0;
}
0