IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> PAT(甲级)2020年秋季考试 7-4 Professional Ability Test (C++) -> 正文阅读

[C++知识库]PAT(甲级)2020年秋季考试 7-4 Professional Ability Test (C++)

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 S 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 S S will receive a voucher(代金券)of D D 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 S 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 ) N (≤1000) N(1000) and M M M, being the total numbers of tests and prerequisite relations, respectively. Then M M 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 N?1 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 ) K (≤N) K(N) is given, followed by K K 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.

Caution:

考试的时候第一题盆盆奶那个浪费了点时间,再加上这道题刚开始没有看出来是要检查 DAG,并且 Dijkstra 确实不是很熟悉,所以一开始用的 DFS ,果然超时,但后来也没时间改了,结束后查了一下这道题的解答,这篇解释得挺详细的,下面的代码是看了这篇的思路后写出来的。

(注:由于没有提交的机会了,所以下面的代码不保证 AC,但上面那篇文章里面有 AC 代码)

Solution:

// Talk is cheap, show me the code
// Created by Misdirection 2021-09-08 14:59:41
// All rights reserved.

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int n, m;
vector<vector<int>> prerequisite;
int sMap[1010][1010], dMap[1010][1010];
int inDegree[1010], inDegree2[1010];

bool vis[1010];
int dis[1010];
int yuans[1010];
int pre[1010];

bool judgeDAG(){
    queue<int> q;
    int cnt = 0;

    for(int i = 0; i < n; ++i){
        if(inDegree2[i] == 0) q.push(i);
    }

    while(!q.empty()){
        cnt++;

        int tmp = q.front();
        q.pop();

        for(int j = 0; j < prerequisite[tmp].size(); ++j){
            inDegree2[prerequisite[tmp][j]]--;
            if(inDegree2[prerequisite[tmp][j]] == 0) q.push(prerequisite[tmp][j]);
        }
    }

    return cnt == n;
}

void dijkstra(){

	fill(vis, vis + 1010, false);
    fill(dis, dis + 1010, 2147483647);
    fill(yuans, yuans + 1010, 0);
    
    dis[n] = 0;
    for(int i = 0; i <= n; ++i){
        int u = -1, minLen = 2147483647;

        for(int j = 0; j <= n; ++j){
            if(!vis[j] && dis[j] < minLen){
                u = j;
                minLen = dis[j];
            }
        }

        if(u == -1) return;
        vis[u] = true;

        for(int x = 0; x < prerequisite[u].size(); ++x){
            int v = prerequisite[u][x];

            if(!vis[v]){
                if(dis[v] > dis[u] + sMap[u][v]){
                    dis[v] = dis[u] + sMap[u][v];
                    yuans[v] = yuans[u] + dMap[u][v];
                    pre[v] = u;
                }
                else if(dis[v] == dis[u] + sMap[u][v] && yuans[u] + dMap[u][v] > yuans[v]){
                    yuans[v] = yuans[u] + dMap[u][v];
                    pre[v] = u;
                }
            }
        }
    }
}

int main(){
    
    scanf("%d %d", &n, &m); 
    prerequisite.resize(n + 1);

    for(int i = 0; i < m; ++i){
        int t1, t2, s, d;

        scanf("%d %d", &t1, &t2);
        scanf("%d %d", &sMap[t1][t2], &dMap[t1][t2]);
        inDegree[t2]++;
        inDegree2[t2]++;
        prerequisite[t1].push_back(t2);
    }

    int k;
    scanf("%d", &k);

    if(judgeDAG()){
        printf("Okay.\n");

        for(int i = 0; i < n; ++i){
            if(inDegree[i] == 0){
                prerequisite[n].push_back(i);
                sMap[n][i] = dMap[n][i] = 0;
            }
        }

        dijkstra();

        for(int i = 0; i < k; ++i){
            int tmp;
            scanf("%d", &tmp);

            if(inDegree[tmp] == 0){
                printf("You may take test %d directly.\n", tmp);
                continue;
            }

            vector<int> ans;
            int x = tmp;

            while(true){
                if(x == n) break;
                ans.push_back(x);
                x = pre[x];
            }

            for(int j = ans.size() - 1; j >= 0; --j){
                if(j == 0) printf("%d\n", ans[j]);
                else printf("%d->", ans[j]);
            }
        }
    }

    else{
        printf("Impossible.\n");

        for(int i = 0; i < k; ++i){
            int tmp;
            scanf("%d", &tmp);

            if(inDegree[tmp] == 0) printf("You may take test %d directly.\n", tmp);
            else printf("Error.\n");
        }
    }
    
    return 0;
}
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-09-09 11:33:43  更:2021-09-09 11:35:38 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 19:50:21-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码