一共有1个电视机,N(<=10)个装置,和M(2<=M<=3)个盒子
现在要将N个装置放进M个盒子里,每个盒子最多放4个
现在有L根电线,在电视机和装置之间相连,电视机的编号是0,装置的编号是1到N
求如何分装装置,使得漏在盒子外面的电线段数最少(连接电视机的必然漏在外面一段;如果电线相连的两个装置在一个盒子里,则不算漏在外面;如果是在不同的盒子里的装置相连,则漏在外面的段数为盒子的距离)
输入:
用例数T
N,M,L
L跟电线所连的电视/装置编号
输出:漏在盒子外面的电线最小值
3
6 2 12
0 1
0 2
6 0
2 5
5 6
6 5
3 4
3 1
2 1
2 1
3 2
2 4
7 2 10
1 2
1 3
3 1
4 1
5 1
4 5
6 7
7 6
4 0
7 0
9 3 12
8 6
3 8
2 6
4 1
3 8
5 4
7 6
5 1
0 6
6 4
9 6
5 3
#1 4
#2 3
#3 5
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int T, devices, boxes, lines, answer, line[12][2], mark[3], result[10];
void handle(){
int count = 0;
for (int i = 0; i < lines; i++){
if (line[i][0] == 0 || line[i][1] == 0){
count++;
}
else{
if (result[line[i][0]] != result[line[i][1]]){
count += result[line[i][0]] < result[line[i][1]] ? (result[line[i][1]] - result[line[i][0]]) : (result[line[i][0]] - result[line[i][1]]);
}
}
}
if (answer > count){
//for (int i = 1; i <= devices; i++) {
// printf("%d ", result[i]);
//}
//printf("\tcount: %d\n", count);
answer = count;
}
}
void dfs(int steps) {
if (steps == devices + 1) {
handle();
return;
}
for (int i = 0; i < boxes; i++){
if (mark[i] < 4){
mark[i]++;
result[steps] = i;
dfs(steps + 1);
mark[i]--;
}
}
}
int main(int argc, char** argv) {
freopen("sample_input.txt", "r", stdin);
setbuf(stdout, NULL);
scanf("%d", &T);
for (int test_case = 1; test_case <= T; ++test_case) {
scanf("%d %d %d", &devices, &boxes, &lines);
for (int i = 0; i < lines; i++) {
scanf("%d %d", &line[i][0], &line[i][1]);
}
answer = 999999;
for (int i = 0; i < boxes; i++) {
mark[i] = 0;
}
dfs(1);
printf("#%d %d\n", test_case, answer);
}
return 0;
}