2020PAT甲级秋季7-4 Professional Ability Test (30分)

43 篇文章 0 订阅

7-4Professional Ability Test(30分)

Professional Ability Test (PAT) consists of several series of subject tests. Each test is divided into several levels. Level A is a prerequisite (前置要求) of Level B if one must pass Level A with a score no less than S in order to be qualified to take Level B. At the mean time, one who passes Level A with a score no less than S will receive a voucher(代金券)of D yuans (Chinese dollar) for taking Level B.

At the moment, this PAT is only in design and hence people would make up different plans. A plan is NOT consistent if there exists some test T so that T is a prerequisite of itself. Your job is to test each plan and tell if it is a consistent one, and at the mean time, find the easiest way (with minimum total S) to obtain the certificate of any subject test. If the easiest way is not unique, find the one that one can win the maximum total value of vouchers.

Input Specification:

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.

Output Specification:

Print in the first line Okay. if the whole plan is consistent, or Impossible. if not.

If the plan is consistent, for each query of test T, print in a line the easiest way to obtain the certificate of this test, in the format:

T0->T1->…->T
However, if T is the first level of some subject test (with no prerequisite), print You may take test T directly. instead.

If the plan is impossible, for each query of test T, check if one can take it directly or not. If the answer is yes, print in a line You may take test T directly.; or print Error. instead.

Sample Input 1:

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

Sample Output 1:

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

Sample Input 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

Sample Output 2:

Impossible.
You may take test 3 directly.
Error.

注意一个地方Dijkstra最短路取到最短的路径不能直接结束,因为可能有长度相同的节点,但赚的钱更多,最好是用优先队列做。有一种解法是在原有拓扑图中添加一个节点,使这个节点指向那些度为0的节点如何只需执行一次Dijkstra就可以得出答案。

#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
struct Node {
	int s, d;
};
Node e[1050][1050];
int N, E, K, test, flag1 = 0, minS, maxD, inDegree[1050] = {0}, Dist[1050], father[1050], Voucher[1050];
vector<int> v[1050], ans;
bool visit[1050] = {false};
void dfs(int root) {
	if (root == -1) return;
	ans.push_back(root);
	dfs(father[root]);
}
void judge(int root, int root1) {
	if (flag1) return;
	for (int i = 0; i < v[root].size(); i++) {
		if (v[root][i] == root1) {
			flag1 = 1;
			return;
		}
		if (visit[v[root][i]])continue;
		visit[v[root][i]] = true;
		judge(v[root][i], root1);
		if (flag1) return;
	}
}
int main() {
	scanf("%d %d", &N, &E);
	int a, b, s, d;
	for (int i = 0; i < E; i++) {
		scanf("%d %d %d %d", &a, &b, &s, &d);
		inDegree[b]++;
		v[b].push_back(a);
		e[a][b].s = s;
		e[a][b].d = d;
	}
	scanf("%d", &K);
	for (int i = 0; i < N; i++) {
		fill(visit, visit + 1050, false);
		judge(i, i);
		if (flag1 == 1) break;
	}
	if (flag1 == 0) {
		printf("Okay.\n");
		for (int i = 0; i < K; i++) {
			scanf("%d", &test);
			if (inDegree[test] == 0) {
				printf("You may take test %d directly.\n", test);
				continue;
			} else {
				fill(Dist, Dist + 1050, INF);
				fill(Voucher, Voucher + 1050, 0);
				fill(father, father + 1050, -1);
				fill(visit, visit + 1050, false);
				minS = INF;
				maxD = 0;
				Dist[test] = 0;
				for (int j = 0; j < N; j++) {
					int minV = INF, Dot = -1;
					for (int t = 0; t < N; t++) {
						if (!visit[t] && Dist[t] < minV) {
							minV = Dist[t];
							Dot = t;
						}
					}
					if (Dot == -1) break;
					visit[Dot] = true;
					if (inDegree[Dot] == 0 && Dist[Dot] > minS) break;
					if (inDegree[Dot] == 0 && (Dist[Dot] < minS || (Dist[Dot] == minS && Voucher[Dot] > maxD))) {
						minS = Dist[Dot];
						maxD = Voucher[Dot];
						ans.clear();
						dfs(Dot);
					}
					if (Dist[Dot] > minS) break;
					for (int t = 0; t < v[Dot].size(); t++) {
						int id = v[Dot][t];
						if (!visit[id]) {
							if (Dist[id] > Dist[Dot] + e[id][Dot].s) {
								Dist[id] = Dist[Dot] + e[id][Dot].s;
								Voucher[id] = Voucher[Dot] + e[id][Dot].d;
								father[id] = Dot;
							} else if (Dist[id] == Dist[Dot] + e[id][Dot].s) {
								if (Voucher[id] < Voucher[Dot] + e[id][Dot].d) {
									Voucher[id] = Voucher[Dot] + e[id][Dot].d;
									father[id] = Dot;
								}
							}
						}
					}
				}
				for (int j = 0; j < ans.size(); j++) {
					if (j != 0) printf("->");
					printf("%d", ans[j]);
				}
				printf("\n");
			}
		}
	} else if (flag1 == 1) {
		printf("Impossible.\n");
		for (int i = 0; i < K; i++) {
			scanf("%d", &test);
			if (inDegree[test] != 0) printf("Error.\n");
			else printf("You may take test %d directly.\n", test);
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值