Submission #09106


ソースコード

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <bits/stdc++.h>
using namespace std;
int n, m, s;
int dst[50][50];
int dp[20][1 << 16];
int solve(int idx, int stat) {
if(dp[idx][stat] != -1) {
return dp[idx][stat];
}
if(stat == (1 << n) - 1) {
return dst[idx][s];
}
int ret = 1 << 24;
for(int i = 0; i < n; ++i) {
if(!((stat >> i) & 1)) {
ret = min(ret, solve(i, stat | 1 << i) + dst[idx][i]);
}
}
return dp[idx][stat] = ret;
}
int main() {
int a, b, c;
cin >> n >> m >> s; --s;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
dst[i][j] = i == j ? 0 : 1 << 24;
}
}
for(int i = 0; i < m; ++i) {
cin >> a >> b >> c; --a; --b;
dst[a][b] = dst[b][a] = c;
}
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
for(int k = 0; k < n; ++k) {
dst[j][k] = min(dst[j][k], dst[j][i] + dst[i][k]);
}
}
}
memset(dp, -1, sizeof(dp));
cout << solve(s, 1 << s) << endl;
}

ステータス

項目 データ
問題 0010 - クッキー
ユーザー名 ei1503
投稿日時 2016-09-03 13:12:50
言語 C++11
状態 Accepted
得点 35
ソースコード長 1131 Byte
最大実行時間 26 ms
最大メモリ使用量 5676 KB

セット

セット 得点 Cases
1 小課題1 5 / 5 cookies_input1.txt
2 小課題2 10 / 10 cookies_input2.txt
3 小課題3 20 / 20 cookies_input3.txt

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
cookies_input1.txt AC 26 ms 5596 KB
1
cookies_input2.txt AC 14 ms 5572 KB
2
cookies_input3.txt AC 24 ms 5676 KB
3