P8893 「UOI-R1」智能推荐

 

输入输出样例

输入 #1

5 5 2
1 2
3
3 2 1 2
4 3 1 2 3
5 3 1 3 4

输出 #1

3

输入 #2

1 1 1
1
0

输出 #2

0

输入 #3

7 7 2
1 2
2
3 2 1 2
6 2 1 2

输出 #3

-1
// #include <bits/stdc++.h>
// #define bug cout << "***************" << endl
// #define fuck(x) cout << #x << " -> " << x << endl
// #define endl '\n'
// #define int long long
// using namespace std;
// constexpr int N = 1e6 + 10, M = N, inf = 0x3f3f3f3f;
// int n, m, p;
// int r;
// int arr[N];
// int h[N], e[M], ne[M], idx;
// int in[N];
// int day = 0;
// int dis[N];
// bool vis[N];
// void add(int a, int b)
// {
//     e[idx] = b, ne[idx] = h[a], h[a] = idx++;
// }
// void topology()
// {
//     memset(dis, 0x3f, sizeof dis);
//     queue<int> q;
//     for (int i = 0; i < p; i++) // 第0天完成的题
//     {
//         q.push(arr[i]);
//         dis[arr[i]] = 0;
//         vis[arr[i]] = true;
//     }
//     while (!q.empty())
//     {
//         int now = q.front();
//         q.pop();
//         vis[now] = false;
//         if (now == m)
//         {
//             cout << day << endl;
//             return;
//         }
//         for (int i = h[now]; i != -1; i = ne[i])
//         {
//             int j = e[i];
//             in[j]--;
//             if (in[j] == 0)
//             {
//                 if (dis[j] > dis[now] + 1)
//                 { // 遍历的是出度边所以是w[i],当前的结点是now所以是dis[now]
//                     dis[j] = dis[now] + 1;
//                     if (!vis[j])
//                     {
//                         vis[j] = true;
//                         q.push(j);
//                     }
//                     // if (j == m)
//                     // {
//                     //     cout << day << endl;
//                     //     return;
//                     // }
//                 }
//             }
//             day++;
//         }
//         cout << -1 << endl;
//     }
// }
// void solve()
// {
//     cin >> n >> m >> p;
//     for (int i = 0; i < p; i++)
//     {
//         cin >> arr[i];
//         // vis[arr[i]] = true;
//     }
//     cin >> r;
//     memset(h, -1, sizeof h);
//     while (r--)
//     {
//         int a, b, c;
//         cin >> a >> b;
//         in[a] = b;
//         for (int i = 0; i < b; i++)
//         {
//             cin >> c;
//             add(c, a); // 建立c指向a的边:
//         }
//     }
//     topology();
// }

// signed main()
// {
//     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

//     int T = 1;

//     while (T--)
//     {
//         solve();
//     }

//     return 0;
// }
#include <bits/stdc++.h>
#include <unordered_map>
#include <iostream>
#include <vector>
#include <queue>
#include <set>
using namespace std;

int N, K, p;
typedef pair<int, int> PII;
vector<int> to[10004];
struct cmp
{
    bool operator()(PII a, PII b)
    {
        return a.first > b.first;
    }
};
priority_queue<pair<int, int>, vector<PII>, cmp> que; // 天数和题就用pair来存了,懒得用greater了,所以要注意存天数要存天数的相反数!
int need[10004];
int main()
{
    // string fileid;
    // cin >> fileid;
    // freopen(("rec" + fileid + ".in").c_str(), "r", stdin);
    // freopen(("rec" + fileid + ".out").c_str(), "w", stdout);
    cin >> N >> K >> p;
    int cur;
    for (int i = 1; i <= p; i++)
    {
        cin >> cur; // 第一天完成的题目;
        if (cur == K)
        {
            cout << 0 << endl;
            return 0;
        }
        que.push(make_pair(0, cur)); // 0天,题目;
    }
    // r次推荐规则;
    int r;
    cin >> r;
    for (int i = 1; i <= r; i++)
    {
        int j;
        cin >> j;       // 完成j这道题
        cin >> need[j]; // 需要完成多少道题目
        int k;
        for (int l = 1; l <= need[j]; l++)
        {
            cin >> k;
            to[k].push_back(j); // k这道题完成后才可以继续完成j题;
        }
    }
    while (!que.empty()) // 队列中装的是已经完成的题目;
    {
        pair<int, int> tp = que.top(); // 取出天数较大的
        que.pop();
        if (tp.second == K)
        {
            cout << tp.first << endl;
            return 0;
        }
        for (int i : to[tp.second])
        {
            need[i]--;
            if (need[i] == 0) // 所有都做完了,这道题的所有题都做完了;
            {
                if (i == K)
                {
                    cout << (tp.first + 1) << endl; // 需要加1;
                    return 0;
                }
                que.push(make_pair(tp.first + 1, i)); // 当前天数+1,并且存入队列;
            }
        }
    }
    cout << -1 << endl;
    return 0;
}
/*
将完成的题目放入队列,依次取出用来更新其他题目:
取出的时候优先取出天数最大的;
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ou_fan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值