#include<iostream> int m, n; //m为测试案例 int sx, sy, ex, ey; int data[5][5] = { 0 }; int vis[5] = { 0 }; int min; int d(int a, int s, int d, int f) { int he1 = a - d> 0 ? a - d : d - a; int he2 = s - f > 0 ? s - f : f - s; return he1 + he2; } void dfs(int sx, int sy, int num, int dis, int step) { if (dis + d(sx, sy, ex, ex)> min) { return; } if (step == num) { min = dis + d(sx, sy, ex, ey); return; } for (int i = 0; i < n; i++) { if (!vis[i]) { vis[i] = 1; dis += (d(sx, sy, data[i][0], data[i][1]) + data[i][4]); dfs(data[i][2], data[i][3], num, dis, step + 1); dis -= (d(sx, sy, data[i][0], data[i][1]) + data[i][4]); dis += (d(sx, sy, data[i][2], data[i][3]) + data[i][4]); dfs(data[i][0], data[i][1], num, dis, step + 1); dis -= (d(sx, sy, data[i][2], data[i][3]) + data[i][4]); vis[i] = 0; } } } int main() { freopen("input.txt", "r", stdin); scanf("%d", &m); while (m--) { scanf_s("%d", &n); //虫洞个数 scanf_s("%d%d%d%d", &sx, &sy, &ex, &ey); for (int i = 0; i < n; i++) { for (int j = 0; j < 5; j++) scanf_s("%d", &data[i][j]); } min = d(sx, sy, ex, ey); for (int i = 1; i <= n; i++) dfs(sx, sy, i, 0, 0); //i 表示用几个虫洞 } printf("%d", min); }