HDU 2647 Reward (topological sort)

Source: Internet
Author: User

Problem Descriptiondandelion ' 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.
Inputone 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.
Outputfor 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 11 22 21 22 1

Sample Output
1777-1

Authordandelion


Topology Sort:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Queue>typedef Long long ll;using namespace std; #define REPF (I, A, b) for (int i = A; I <= B; + + i) #define RE P (i, n) for (int i = 0; i < n; + + i) #define CLEAR (A, X) memset (A, x, sizeof a) const int maxn=10100;int h   Ead[maxn*2],next[maxn*2],end[maxn*2];int in[maxn],val[maxn];int n,m,cnt,k;void toposort () {int sum=0;   k=0;   queue<int>q;   REPF (I,1,n) if (!in[i]) Q.push (i);       while (!q.empty ()) {int V=q.front ();       SUM+=VAL[V];       Q.pop ();       k++;             for (int i=head[v];i!=-1;i=next[i]) {if (--in[end[i]]==0) {Q.push (end[i]);           val[end[i]]=val[v]+1;   }}} if (k==n) printf ("%d\n", sum); else printf (" -1\n");}    void Add (int x,int y) {next[cnt]=head[x];    End[cnt]=y; head[x]=cnt++;}     int main () {int x, y;   while (~SCANF ("%d%d", &n,&m)) {      cnt=0;         REPF (i,1,n) val[i]=888;         CLEAR (in,0);         CLEAR (head,-1);             REP (i,m) {scanf ("%d%d", &x,&y);             Add (y,x);         in[x]++;     } toposort (); } return 0;}


HDU 2647 Reward (topological sort)

Related Article

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.