HDU 2647 Reward

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   java   io   ar   for   

RewardTime Limit: 1000msMemory Limit: 32768KBThis problem will be judged on HDU. Original ID: 2647
64-bit integer IO format: %I64d      Java class name: Main  

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 11 22 21 22 1
Sample Output
1777-1
Source曾是驚鴻照影來 解題:拓撲排序。 
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 10010;18 int n,m,in[maxn],money[maxn];19 vector<int>g[maxn];20 queue<int>q;21 int topSort() {22     int ans = 0;23     while(!q.empty()) q.pop();24     for(int i = 1; i <= n; i++) {25         if(!in[i]) {26             q.push(i);27             money[i] = 888;28         }29     }30     int sum = n;31     while(!q.empty()){32         sum--;33         int u = q.front();34         q.pop();35         for(int i = 0; i < g[u].size(); i++){36             if(--in[g[u][i]] == 0){37                 q.push(g[u][i]);38                 money[g[u][i]] = money[u]+1;39             }40         }41     }42     if(sum > 0) return -1;43     else{44         for(int i = 1; i <= n; i++)45             ans += money[i];46         return ans;47     }48 }49 int main() {50     int i,u,v;51     while(~scanf("%d %d",&n,&m)) {52         for(i = 1; i <= n; i++) {53             g[i].clear();54             in[i] = 0;55             money[i] = 0;56         }57         for(i = 0; i < m; i++) {58             scanf("%d %d",&v,&u);59             g[u].push_back(v);60             in[v]++;61         }62         printf("%d\n",topSort());63     }64     return 0;65 }
View Code

 

HDU 2647 Reward

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.