QOJ.ac

QOJ

Limite de temps : 1 s Limite de mémoire : 512 MB Points totaux : 100

#3374. 旅行者汤姆

Statistiques

Tom 是一名二手冰棒推销员。他最近在 Gløshaugen 的摊位生意不太好,所以他决定环游世界去推销他的商品。他已经列出了一份要访问的城市名单,并收集了城市之间飞机旅行的费用。现在他只需要弄清楚他是否有足够的钱来完成这次旅行。他当然听说过旅行商问题非常非常难,所以这项任务就落到了他认识的最聪明的人身上。那个人就是你(如果你现在觉得自己不够聪明,请记住 Tom 认识的人并不多)。你需要编写一个程序,计算 Tom 按给定顺序访问所有这些城市的最低成本。

输入格式

输入的第一行包含一个整数 $T$,表示测试用例的数量。 每个测试用例的第一行包含一个整数 $N$,表示 Tom 将要访问的城市数量。每个测试用例的第二行包含 $N$ 个整数 $a_i$,表示 Tom 想要访问这 $N$ 个城市的顺序。他从描述的第一个城市开始他的旅程,并在访问完最后一个城市后回到该城市。接下来是 $N$ 行,每行包含 $N$ 个整数,描述从城市 $i$ 到每个城市 $j$ 的旅行成本 $c_{ij}$。

输出格式

对于每个测试用例,输出一行,包含一个整数,表示访问这些城市的最低可能成本。如果无法访问所有城市,则输出 impossible

数据范围

  • $0 < T \leq 100$
  • $0 < N \leq 200$
  • $0 \leq a_i < N$
  • $-1 \leq c_{ij} \leq 10000$
  • 访问的城市列表始终是 $0$ 到 $N-1$(包含)之间数字的一个排列。
  • 从一个城市到其自身的旅行成本始终为 $0$。
  • 当且仅当从城市 $i$ 到城市 $j$ 没有航班时,成本矩阵中第 $i$ 行的第 $j$ 个数字为 $-1$。
  • 注意,Tom 在旅程结束时会返回起始城市。
  • 城市可以被多次访问,但只有在正确的相对顺序下才会被计入。例如,对于顺序 $1\ 0\ 2$,路径 $1\ 2\ 0\ 2\ 1$ 是有效的,而 $1\ 2\ 0\ 1$ 则不是。

样例

输入格式 1

2
3
0 2 1
0 1 2
1 0 1
1 3 0
2
0 1
0 -1
1 0

输出格式 1

5
impossible

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.