HDU
来源:互联网 发布:mac os x 10.12 beta5 编辑:程序博客网 时间:2024/06/05 03:07
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
Hint
题意
题解:
注意由b向a建边
AC代码
#include <cstdio>#include <cstring>#include <cstdlib>#include <vector>#include <queue>#include <algorithm>using namespace std;struct node { int v,nxt;};int n,m;node edge[20010];int in[20010];int head[20010],tal;int ans[20010];void init(){ tal = 0; memset(head,-1,sizeof(head)); memset(in,0,sizeof(in)); memset(ans,0,sizeof(ans));}void addedge(int u,int v){ edge[tal] = {v,head[u]}; head[u] = tal++;}void topsort(){ int cnt = 1; queue<int> q; while (!q.empty()) q.pop(); for (int i = 1; i <= n; ++i){ if (in[i]==0) q.push(i); } while (!q.empty()){ int p = q.front();q.pop(); for(int i = head[p];i != -1; i=edge[i].nxt){ int to = edge[i].v; in[to]--; if(in[to]==0) { q.push(to); ans[to] = ans[p]+1;//按层来加钱 当它入度为0时 这样始终保证是比它前一个大一 } } } int ed = n*888; for (int i = 1;i <= n; ++i){ if (in[i]>0){ printf("-1\n"); return ; } ed+=ans[i]; } printf("%d\n",ed);}int main(){ while (scanf("%d%d",&n,&m)!=EOF){ init(); int a,b; while (m--){ scanf("%d%d",&a,&b); addedge(b,a); in[a]++; } topsort(); } return 0;}
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- 结构体长度问题
- Unity 渲染教程(一):矩阵
- lua基础2.表
- Hibernate 不同数据库的连接及SQL方言
- Java网络03 Servlet沙拉
- HDU
- Android之第三方登录分享封装
- Key Set 【hdu-5363】【快速幂】
- 斯坦福机器学习2:SVM支持向量机的通俗理解
- HASH求两子串是否相等(O(N)预处理O(1)比较)
- Paint 使用
- 游戏策划学习站点
- 微信开发第一步,引入微信api
- 华为手机android studio调试信息LogCat不显示问题解决办法