HDU 2647 Reward(拓扑排序)
来源:互联网 发布:js 定义键值对数组 编辑:程序博客网 时间:2024/04/23 21:37
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.
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
Author
dandelion
拓扑排序。。
#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 REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int maxn=10100;int head[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;}
0 0
- HDU 2647 Reward(拓扑排序)
- hdu 2647 Reward (拓扑排序)
- hdu 2647 Reward ( 拓扑排序 )
- HDU 2647 - Reward(拓扑排序)
- hdu 2647 Reward (拓扑排序)
- HDU 2647Reward(拓扑排序)
- HDU 2647 Reward(图论-拓扑排序)
- HDU 2647 Reward(图论-拓扑排序)
- HDU 2647 Reward(拓扑排序)
- HDU 2647 -- Reward (拓扑排序)
- hdu 2647 Reward(拓扑排序)
- HDU-2647 Reward(拓扑排序)
- HDU 2647Reward (拓扑排序)
- HDU 2647 Reward(拓扑排序)
- HDU 2647 Reward(拓扑排序)
- HDU 2647 Reward(拓扑排序)
- HDU 2647 Reward (拓扑排序)
- HDU 2647 Reward(拓扑排序)
- 标题红色叹号和Clear Link Markers问题
- ORM是明显的反模式
- Android Studio 设置背景色==Android笔记
- zoj 3551 Bloodsucker (概率dp)
- 比基尼美女_人像摄影吧主题 - 云寻觅云贴吧
- HDU 2647 Reward(拓扑排序)
- zzuoj1196单调数
- 10个有关RESTful API良好设计的最佳实践
- FATAL: NO bootable medium found! System halted
- 键盘处理例子思路
- zoj 3582 Back to the Past (概率dp)
- Ubuntu-停用google analytics的浏览器扩展
- hdu 5137 最短路最大化
- Huffman树的应用 (数据结构)