zoukankan      html  css  js  c++  java
  • HDU-2647 Reward(链式前向星+拓扑排序)

    Reward

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 16602 Accepted Submission(s): 5308

    Problem Description

    Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to distribute rewards to his workers. Now he has a trouble about how to distribute the rewards.
    The workers will compare their rewards ,and some one may have demands of the distributing of rewards ,just like a's reward should more than b's.Dandelion's unclue wants to fulfill all the demands, of course ,he wants to use the least money.Every work's reward will be at least 888 , because it's a lucky number.

    Input

    One line with two integers n and m ,stands for the number of works and the number of demands .(n<=10000,m<=20000)
    then m lines ,each line contains two integers a and b ,stands for a's reward should be more than b's.

    Output

    For every case ,print the least money dandelion 's uncle needs to distribute .If it's impossible to fulfill all the works' demands ,print -1.

    Sample Input

    2 1
    1 2
    2 2
    1 2
    2 1

    Sample Output

    1777
    -1

    解题思路:

    反向建图后拓扑排序,就为了复习下链式前向星和拓扑排序

    #include <bits/stdc++.h>
    using namespace std;
    /*    freopen("k.in", "r", stdin);
        freopen("k.out", "w", stdout); */
    //clock_t c1 = clock();
    //std::cerr << "Time:" << clock() - c1 <<"ms" << std::endl;
    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    #define de(a) cout << #a << " = " << a << endl
    #define rep(i, a, n) for (int i = a; i <= n; i++)
    #define per(i, a, n) for (int i = n; i >= a; i--)
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int, int> PII;
    typedef pair<double, double> PDD;
    typedef vector<int, int> VII;
    #define inf 0x3f3f3f3f
    const ll INF = 0x3f3f3f3f3f3f3f3f;
    const ll MAXN = 5e5 + 7;
    const ll MAXM = 1e6 + 7;
    const ll MOD = 998244353;
    const double eps = 1e-6;
    const double pi = acos(-1.0);
    int n, m;
    int cnt = -1;
    int indegree[MAXN];
    int head[MAXN];
    int red[MAXN];
    struct Edge
    {
        int u, v, Next;
        Edge(int uu = 0, int vv = 0, int NN = 0) { u = uu, v = vv, Next = NN; }
    } e[MAXN];
    void add(int u, int v)
    {
        e[++cnt].v = v;
        e[cnt].u = u;
        e[cnt].Next = head[u];
        head[u] = cnt;
    }
    ll topo()
    {
        int tot = 0;
        queue<int> q;
        bool flag1 = false;
        for (int i = 1; i <= n; i++)
            if (!indegree[i])
                q.push(i);
        while (!q.empty())
        {
            int now = q.front();
            q.pop();
            tot++;
            if (tot > n)
                break;
            for (int i = head[now]; ~i; i = e[i].Next)
            {
                int v = e[i].v;
                indegree[v]--;
                if (!indegree[v])
                {
                    q.push(v);
                    red[v] = red[now] + 1;
                }
            }
        }
        if (tot != n)
            return -1;
        ll ans = n * 888;
        for (int i = 1; i <= n; i++)
            ans += red[i];
        return ans;
    }
    int main()
    {
        while (~scanf("%d%d", &n, &m))
        {
            cnt = -1;
            memset(red, 0, sizeof(red));
            memset(head, -1, sizeof(head));
            memset(indegree, 0, sizeof(indegree));
            while (m--)
            {
                int u, v;
                scanf("%d%d", &u, &v);
                add(v, u);
                indegree[u]++;
            }
            printf("%lld
    ", topo());
        }
        return 0;
    }
    
  • 相关阅读:
    Kerberos-KDC
    samba后台进程及安全模式简介
    samba服务器详细配置(非域模式)
    windows常用net use命令
    samba常用命令
    ORA-24324、ORA-12560、ORA-12514
    oracle的启动和关闭
    Oracle 监听配置详解(转载)
    linux加入windows域之完美方案(转载)
    怎样识吉他谱
  • 原文地址:https://www.cnblogs.com/graytido/p/11801214.html
Copyright © 2011-2022 走看看