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");}}}
}

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

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

    7-4Professional Ability Test(30分) Professional Ability Test (PAT) consists of several series of subj ...

  2. PAT甲级1151 LCA in a Binary Tree (30 分):[C++题解]LCA、最低公共祖先、哈希表映射

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 和下面这道题几乎是同一题:PAT甲级1143 Lowest Common Ancestor (30 分):[C++题解]LCA.最低 ...

  3. PAT甲级1087 All Roads Lead to Rome (30分):[C++题解]dijkstra求单源最短路综合、最短路条数、保存路径

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 首先这是一道dijkstra算法求最短路的题目,不过此题较为复杂.首先需要将字符串城市名映射成数字,这里使用hash table 名 ...

  4. PAT甲级1135 Is It A Red-Black Tree (30分):[C++题解]判断红黑树

    文章目录 题目分析 题目链接 题目分析 分析 给定前序遍历,同时红黑树一定是二叉搜索树,所以它的中序遍历就是从小到大排序.因此这道题是给定了前序遍历和中序遍历让建立二叉搜索树(BST). 数据结构中有 ...

  5. PAT甲级1115 Counting Nodes in a BST (30分):[C++题解] 递归建二叉搜索树、dfs求一层结点数量

    文章目录 题目分析 题目链接 题目分析 分析 首先本题给定的二叉搜索树的定义和其他地方的不同.本题小于等于的是左子树,右子树是大于根结点的. 然后说一下做题的思路. 给定一串数据,让构造二叉搜索树. ...

  6. PAT甲级1099 Build A Binary Search Tree (30分):[C++题解]建立二叉搜索树、dfs和bfs

    文章目录 题目分析 题目链接 题目分析 题意重述:给定一棵二叉树的结构,和待填的数值,请将数据填到二叉树中的结点中,使之满足二叉搜索树的性质. 然后按照层序遍历输出数值. 分析: 本题分两步. 第一步 ...

  7. PAT甲级 1151 LCA in a Binary Tree (30分) LCA算法/C++

    1151 LCA in a Binary Tree (30分) 题目大意:给出一棵树的中序和先序遍历,找到这棵树中U和V最小的共同祖先. Tips: 使用一个unordered_map记录中序遍历的值 ...

  8. 【算法笔记题解】PAT A.1095 Cars on Campus (30 分)

    前言 今天不开心就撸了一道PAT的题. 所有的合集相关源码我都更新在gitee上了需要自取xingleigao/study - Gitee.com 题目描述 1095 Cars on Campus ( ...

  9. 【PAT - 甲级1034】Head of a Gang (30分)(并查集)

    题干: One way that the police finds the head of a gang is to check people's phone calls. If there is a ...

最新文章

  1. 【数据结构】某些难理解点
  2. python获取用户输入的数字_Python 将用户输入的数字 提取整数谢谢
  3. Thymeleaf模板引擎---SpringBoot
  4. SharePoint 大局观(4)——从开发人员角度
  5. 第九篇: 服务链路追踪(Spring Cloud Sleuth)(Finchley版本)
  6. Windows 7+Ubuntu 16.04 双系统安装
  7. java 生成一个空文件系统_如何使用java创建一个空白的PPT文档?
  8. HDUOJ----2063过山车
  9. 攻击防御实例——SQL注入
  10. 宿舍管理系统简单的增删改查
  11. 2022 MegCup | 小模型盲降噪怎么比?资深炼丹师给你赛前指导!
  12. 学习andriod开发之 自己开发短信发送软件
  13. 电商订单批量导入API接口功能
  14. iOS开发:国际化之app支持多种语言切换
  15. 报错:Parameter ‘XXX‘ implicitly has an ‘any‘ type.解决方法
  16. 如何实现一个“线程池”
  17. setsockopt和getsockopt函数详解
  18. CVE-2022-1388 BIG-IP_POC-YAML
  19. 苹果大战FBI,四个回合的波折之后有哪些启示?
  20. 护眼灯和白炽灯哪个更保护眼睛?推荐真正护眼的护眼灯

热门文章

  1. 数据库系统发展的特点
  2. Python | 自动回复微信祝福语
  3. C语言写货郎问题(贪心算法)
  4. 什么是RCT实时时钟?(STM32中RTC时钟源)
  5. 核磁谱图分析步骤_核磁一般氢谱和碳谱的解析步骤
  6. Mac平台安卓模拟器:网易MuMu mac中文免费版(支持12系统)
  7. 微信共享停车场小程序开发设计方案
  8. linux下 exp导出时报sh:exp:找不到,是什么原因,exp/imp(导出/导入)
  9. BUUCTF-刷题记录-8
  10. Vue3 扫描二维码