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

 

7-4 Professional 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.
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 1001;
const int inf = 1000000000;
int indegree[maxn]={0};
int in[maxn]={0};
int G[maxn][maxn],M[maxn][maxn];
vector<int> adj[maxn]; 
int n,m,u,v,score,money;
bool isCyclic()
{
	queue<int> q;
	for(int i=0; i<n; i++){
		if(indegree[i]==0) q.push(i);
	} 
	int u,cnt=0;
	while(!q.empty()){
		u = q.front(); q.pop(); cnt++;
		for(int v=0; v<adj[u].size(); v++){
			indegree[adj[u][v]]--;
			if(indegree[adj[u][v]]==0) q.push(adj[u][v]);
		}
	}
	if(cnt==n) return true;
	else return false;
}
int d[maxn];
bool vis[maxn]={false};
vector<int> pre[maxn];
void dijkstra(int s)
{
	fill(d,d+maxn,inf);
	d[s]=0;
	while(true){
		int u=-1, Min=inf;
		for(int i=0; i<=n; i++){
			if(vis[i]==false&&d[i]<Min){
				Min = d[i];
				u = i;
			}
		}
		if(u==-1) return;
		vis[u] = true;
		for(int i=0; i<adj[u].size(); i++){
			v = adj[u][i];
			if(vis[v]==false){
				if(d[u]+G[u][v]<d[v]){
					d[v]=d[u]+G[u][v];
					pre[v].clear();
					pre[v].push_back(u);
				}
				else if(d[u]+G[u][v]==d[v]){
					pre[v].push_back(u);
				}
			}
		}
	}
}
vector<int> tmpPath,path;
int maxVou=-1;
void dfs(int s)
{
	if(s==n){
		int nowVou=0;
		for(int i=tmpPath.size()-1; i>0; i--){
			nowVou+=M[tmpPath[i]][tmpPath[i-1]];
		}
		if(nowVou>maxVou){
			maxVou = nowVou;
			path = tmpPath;
		}
		return;
	}
	tmpPath.push_back(s);
	for(int i=0; i<pre[s].size();i++){
		dfs(pre[s][i]);
	}
	tmpPath.pop_back();
}
int main()
{
	fill(G[0],G[0]+maxn*maxn,inf);
	fill(M[0],M[0]+maxn*maxn,-1);
	scanf("%d%d",&n,&m);
	for(int i=0; i<m; i++){
		scanf("%d%d%d%d",&u,&v,&score,&money);
		indegree[v]++;
		in[v]++;
		G[u][v]=score;
		M[u][v]=money;
		adj[u].push_back(v);
	}
	for(int i=0; i<n; i++){
		if(in[i]==0){
			G[n][i]=0;
			adj[n].push_back(i);
		}
	}
	dijkstra(n);
	int k;
	scanf("%d",&k);
	if(isCyclic()){
		printf("Okay.\n");
		for(int i=0; i<k; i++){
			scanf("%d",&u);
			if(in[u]==0){
				printf("You may take test %d directly.\n",u);
			}
			else{
				tmpPath.clear();
				path.clear();
				maxVou=-1;
				dfs(u);
				for(int j=path.size()-1; j>=0; j--){
					printf("%d",path[j]);
					if(j!=0) printf("->");
					else printf("\n");
				}
			}
		}
	}
	else{
		printf("Impossible.\n");
		for(int i=0; i<k; i++){
			scanf("%d",&u);
			if(in[u]==0){
				printf("You may take test %d directly.\n",u);
			}
			else{
				printf("Error.\n");
			}
		}
	}
}

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值