【PTA刷题】甲级 拓扑排序+多源Dijkstra

题源: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更麻烦了)。

#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值