HDU2647 reward "topological sort"

Source: Internet
Author: User

Reward

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) total submission (s): 4379 Accepted S Ubmission (s): 1335
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 had a trouble about what to distribute the rewards.
The workers would compare their rewards, and some one may has 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'll be at least 888, because it's a lucky number.

Input
One line with a 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 the integers a and B, stands for a ' s reward should is 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


The main topic: The boss to pay, but the workers are not the same remuneration, there are n people, M kind of situation. A's remuneration must

To be higher than B. Everyone has a minimum reward of 888, asking: How much does the boss have to spend at least?

Idea: to B->a for the forward edge of the establishment of a topological sort, do not meet the sort of output "1", otherwise the topological sort,

The points of the topological order are regarded as one layer, and the non-degree is the first layer, and the second layer can be reached by one edge.

The third layer can be reached by two edges. Each layer is more than the previous layer of money. Finally, the total amount is output.


#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace std;const int MAXN = 10010;const int MAXM = 20010;int head[maxn],n,m,ans,indegree[maxn],queue[maxn],money[maxn];struct    edgesnode{int to;    int W; int next;};    Edgesnode edges[maxm];int Toposort () {int iq = 0;    for (int i = 1; I <= N; i++) {if (indegree[i]==0) queue[iq++] = i;        } for (int i = 0; i < IQ; i++) {ans + = money[queue[i]];            for (int k = head[queue[i]]; K! =-1; k = edges[k].next) {indegree[edges[k].to]--;                if (indegree[edges[k].to] = = 0) {money[edges[k].to] = Money[queue[i]] + 1;                Money[edges[k].to] = money[k] + 1; it's wrong.            queue[iq++] = edges[k].to;    }}} if (iq = = N) return ans; else return 0;}    int main () {int x, y; while (Cin >> N >> M) {memset (head,-1,sizeof (head));        memset (edges,0,sizeof (Edges));        memset (indegree,0,sizeof (Indegree));        memset (queue,0,sizeof (queue));        memset (money,0,sizeof (money));            for (int i = 0; i < M; i++) {cin >> x >> y;            edges[i].to = x;            EDGES[I].W = 1;            Edges[i].next = Head[y];            Head[y] = i;        indegree[x]++;        } ans = n*888;        if (!toposort ()) cout << "1" << Endl;    else cout << ans << endl; } return 0;}



HDU2647 reward "topological sort"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.