题源:7-4 Professional Ability Test (30point(s))
大意:
给一个图,有两个权重dis和value。前一个权重越小越好,后一个越大越好。
节点之间有前置关系(拓扑排序),最后一行给出查询的点,问每次查询的最短路径(Dijkstra)。
输入样例:
/*Each input file contains one test case. For each case, the first line gives two positive integers N (≤1000) and M, being the total numbers of tests and prerequisite relations, respectively. Then M lines follow, each describes a prerequisite relation in the following format:
T1 T2 S D
where T1 and T2 are the indices (from 0 to N−1) of the two distinct tests; S is the minimum score (in the range (0, 100]) required to pass T1 in order to be qualified to take T2; and D is the value of the voucher (in the range (0, 500]) one can receive if one passes T1 with a score no less than S and plan to take T2. It is guaranteed that at most one pair of S and D are defined for a prerequisite relation.
Then another positive integer K (≤N) is given, followed by K queries of tests. All the numbers in a line are separated by spaces.*/
8 15
0 1 50 50
1 2 20 20
3 4 90 90
3 7 90 80
4 5 20 20
7 5 10 10
5 6 10 10
0 4 80 60
3 1 50 45
1 4 30 20
1 5 50 20
2 4 10 10
7 2 10 30
2 5 30 20
2 6 40 60
8
0 1 2 3 4 5 6 7
输出样例:
Okay.
You may take test 0 directly.
0->1
0->1->2
You may take test 3 directly.
0->1->2->4
0->1->2->4->5
0->1->2->6
3->7
输入样例2:
4 5
0 1 1 10
1 2 2 10
3 0 4 10
3 2 5 10
2 0 3 10
2
3 1
输出样例2:
Impossible.
You may take test 3 directly.
Error.
参考题解:https://blog.youkuaiyun.com/weixin_45748610/article/details/110259317
思路:一开始没做出来,一直卡在多源如何求最短路径上,因为往常的Dijkstra都会给一个固定的起点。
后来看了大佬的题解,发现核心解法就是-- 在Dijkstra的时候加入一个虚拟节点N(原本最大是N-1),让N成为节点。连接好N和当前入度为0的节点,就是一道普通的最短路径题了。 在这里,用int pre[maxnum]直接维护前驱节点就好了(如果建树再DFS更麻烦了)。
#