博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Reward(拓扑排序)
阅读量:5307 次
发布时间:2019-06-14

本文共 2612 字,大约阅读时间需要 8 分钟。

Reward

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 5336    Accepted Submission(s): 1619

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
 

 

Author
dandelion
 
题解: 这个题可以用dfs做,也可以用拓扑排序,但要注意两点,因为是要求前者比后者的值要大,所以要反向加边,而且在排序的时候,没加入一个点,就要将其后面的价钱值更新,取这个点的价钱值+1和后面点本身的价钱值得最大值。
代码:
1 #include
2 #include
3 #include
4 using namespace std; 5 #define N 10005 6 #define M 20005 7 int in[N]; 8 int que[N]; 9 int my[N];10 struct Edge{11 int to ;12 int next;13 }edge[M];14 int head[N];15 int Enct;16 void init()17 {18 Enct = 0;19 memset(head,-1,sizeof(head));20 memset(my,0,sizeof(my));21 memset(in,0,sizeof(in));22 }23 void add(int from, int to)24 {25 edge[Enct].to = to;26 edge[Enct].next = head[from];27 head[from] = Enct++;28 }29 int c;30 int t;31 int n;32 int tm ;33 bool tp()34 {35 c = 0;36 t = 1;37 for(int i = 0 ;i < n ;i++)38 {39 if(in[i]==0)40 {41 que[c++] = i;42 }43 }44 for(int i = 0 ;i < c ; i++)45 {46 int id = que[i];47 for(int j = head[id] ; j !=-1 ; j = edge[j].next)48 {49 int t = my[id];50 Edge e = edge[j];51 in[e.to]--;52 my[e.to] = max(t+1,my[e.to]);//后继大于前驱,每次都要更新 53 if(in[e.to]==0)54 {55 que[c++] = e.to;56 }57 }58 }59 if(c
<= n ;i++)80 {81 sum+=my[i];82 }83 printf("%d\n",888*n+sum);84 }85 }86 return 0;87 }

 

转载于:https://www.cnblogs.com/shanyr/p/4701005.html

你可能感兴趣的文章
linux下Rtree的安装
查看>>
【Java】 剑指offer(53-2) 0到n-1中缺失的数字
查看>>
Delphi中ListView类的用法
查看>>
多米诺骨牌
查看>>
Linq 学习(1) Group & Join--网摘
查看>>
asp.net 调用前台JS调用后台,后台掉前台JS
查看>>
Attribute(特性)与AOP
查看>>
苹果手表:大方向和谷歌一样,硬件分道扬镳
查看>>
Competing Consumers Pattern (竞争消费者模式)
查看>>
Android面试收集录15 Android Bitmap压缩策略
查看>>
PHP魔术方法之__call与__callStatic方法
查看>>
ubuntu 安装后的配置
查看>>
web前端之路,js的一些好书(摘自聂微东 )
查看>>
【模板】对拍程序
查看>>
【转】redo与undo
查看>>
解决升级系统导致的 curl: (48) An unknown option was passed in to libcurl
查看>>
Java Session 介绍;
查看>>
spoj TBATTLE 质因数分解+二分
查看>>
Django 模型层
查看>>
dedecms讲解-arc.listview.class.php分析,列表页展示
查看>>