Reward——前向星,拓扑排序
来源:互联网 发布:linux查看gitlab版本 编辑:程序博客网 时间:2024/06/02 03:53
Reward
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uDescription
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.
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.
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
10000 * 10000的数组啊,累死你也开不出来,别做梦了,换个思路吧。。。。。。
前向星,今天刚刚学的,直接用啦,具体是什么,就要看下篇博客啦
<pre name="code" class="html">#include <stdio.h>#include <string.h>int head[10009];struct node{ int zd; int next;};struct node xing[40000];int rd[10009];struct no{ int zhi; int mon;};struct no dis[10009];void tuop(int n){ int i,j,k,low = 0,top = 0; int sum = 0,num = 888,ren = 0; for(i = 1; i <= n; i++) { if(rd[i] == 0) { dis[top].zhi = i; dis[top++].mon = num; } } if(top == 0) { printf("-1\n"); return ; } while(low < top) { k = dis[low].zhi; sum += dis[low].mon; ren++; j = head[k]; while(1) { rd[xing[j].zd]--; if(rd[xing[j].zd] == 0) { dis[top].zhi = xing[j].zd; dis[top++].mon = dis[low].mon + 1; } if(j == -1) break; j = xing[j].next; } low++; } if(ren == n) printf("%d\n",sum); else printf("-1\n");}int main(){ int n,m,coun,a,b; int i,j,k; while(scanf("%d%d",&n,&m)!=EOF) { memset(head,-1,sizeof(head)); memset(rd,0,sizeof(rd)); coun = 0; for(i = 0; i < m; i++) { scanf("%d%d",&a,&b); j = head[b]; while(j!=-1) { if(xing[j].zd == a) break; else j = xing[j].next; } if(j==-1) { rd[a]++; xing[coun].zd = a; xing[coun].next = head[b]; head[b] = coun++; } } tuop(n); } return 0;}
0 0
- Reward——前向星,拓扑排序
- HDU 2647 Reward (拓扑排序 + 链式前向星 )
- HDU2647 Reward(拓扑排序+链式前向星)
- 前向星+拓扑排序
- POJ2367Genealogical tree拓扑排序&前向星
- poj 2367 前向星+拓扑排序
- hdu5438 dfs+拓扑排序+前向星
- 拓扑排序模板--链式前向星
- hdu2647 Reward (拓扑排序)
- hdu Reward(拓扑排序)
- HDU2647 Reward 【拓扑排序】
- hdu2647 - Reward (拓扑排序)
- HDU2647 Reward 【拓扑排序】
- Reward(拓扑排序)
- HDU2647 Reward 拓扑排序
- hdu2647 Reward (拓扑排序)
- hdu2647 Reward--拓扑排序
- HDU2647:Reward(拓扑排序)
- Spring autowire自动装配
- poj 3233 Matrix Power Series(二分)
- hdu-2071-Max Num
- poj 1459 Power Network, 最大流,多源多汇
- hdu-oj 2001 计算两点间距离
- Reward——前向星,拓扑排序
- HDU_1230 火星A+B
- 笔记
- 【算法小总结】广度优先搜索剖析
- 使用Java的Introspector类操作JavaBean的属性
- windows关于程序release 加上调试信息 利用pdb dump定位的问题
- JAVA垃圾回收机制
- hibernate中的inverse和cascade
- hdu-oj 2003 求绝对值