程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> HDU_2647_Reward(拓撲排序)

HDU_2647_Reward(拓撲排序)

編輯:關於C++

Reward

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



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

 

題意:老板要給n個員工發年終獎金,然後給出m個需求 (a,b),表示a的獎金要比b的多,然後呢,老板決定每個員工至少可以拿888元,現在老板總共至少需要發多少獎金。

如果滿足不了就輸出-1。

分析:拓撲排序問題。比第i個人獎金少的總人數有sum個,那麼第i個人的獎金就是888+sum元。注意此題數據比較大,用鄰接表存吧。

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2647

代碼清單:

(1) vector實現

 

#include
#include #include #include #include #include #include #include #include #include #include using namespace std; typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; const int maxv = 10000 +5; int n,m; int p,q; bool judge; int save[maxv]; int degree[maxv]; vectorgraph[maxv]; void init(){ memset(degree,0,sizeof(degree)); for(int i=0;i<=n;i++) graph[i].clear(); } int topSort(){ int cnt=0; int sum=0; int q=888; while(cnt

(2)靜態鄰接表(數組)

 

#include
#include #include #include #include #include #include #include #include #include #include using namespace std; typedef unsigned int uint; typedef long long ll; typedef unsigned long long ull; const int maxv = 10000 + 5; const int maxn = 20000 + 5; struct Edge{ int to,next; }graph[maxn]; int n,m; int p,q; int index; bool judge; int save[maxv]; int head[maxn]; int degree[maxv]; void init(){ index=1; memset(head,0,sizeof(head)); memset(degree,0,sizeof(degree)); } void add(int u,int v){ graph[index].to=v; graph[index].next=head[u]; head[u]=index++; } int topSort(){ int cnt=0; int sum=0; int q=888; while(cnt

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved