HDU 2647 Reward(拓扑排序)
来源:互联网 发布:skycc软件 编辑:程序博客网 时间:2024/06/09 14:31
RewardTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9951 Accepted Submission(s): 3173
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
拓扑排序: 对一个有向无环图(Directed Acyclic Graph简称DAG)G进行拓扑排序,是将G中所有顶点排成一个线性序列,使得图中任意一对顶点u和v,若<u,v> ∈E(G),则u在线性序列中出现在v之前。通常,这样的线性序列称为满足拓扑次序(Topological Order)的序列,简称拓扑序列。
拓扑序列算法思想
(1)从有向图中选取一个没有前驱(即入度为0)的顶点,并输出之;
(2)从有向图中删去此顶点以及所有以它为尾的弧;
重复上述两步,直至图空,或者图不空但找不到无前驱的顶点为止。
#include<stdio.h>#include<stdlib.h>#include<iostream>#include<algorithm>#include<string.h>using namespace std;struct node{ int v; struct node *next;}*h[10000];int d[100000],f[100000],mon[100000];int n,m;void LA(int u,int v){ struct node*p=(struct node*)malloc(sizeof(struct node)); p->v=v; p->next=h[u]; h[u]=p;}void init(){ for(int i=0; i<=n; i++) { d[i]=0; f[i]=0; mon[i]=888; h[i]=NULL; }}int main(){ while(~scanf("%d%d",&n,&m)) { init(); for(int i=0; i<m; i++) { int a,b; scanf("%d%d",&a,&b); LA(b,a); d[a]++; } int top=0,t=0,p=1; while(p) { p=0; top++; for(int i=1; i<=n; i++) { if(!d[i]) { p=1; f[i]=top; mon[i]=mon[i]+top-1; t++; } } for(int i=1; i<=n; i++) { if(f[i]==top) { int u=i; struct node*q; for(q=h[u]; q!=NULL; q=q->next) { int x=q->v; d[x]--; } d[i]=-1; } } } if(t==n) { int sum=0; for(int i=1; i<=n; i++) { sum+=mon[i]; } printf("%d\n",sum); } else printf("-1\n"); }}
阅读全文
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(拓扑排序)
- 字符串全排列
- #CCF准备一年日常刷题#201503-1图像旋转
- Android中的数据存储方式
- 奇怪的气球膨胀 分治(说白了还是dp?)
- 深入理解Java虚拟机(一)
- HDU 2647 Reward(拓扑排序)
- 装饰模式
- tomcat加载失败,提示信息:Illegal access: this web application...
- 原生JAVA解析json
- 51nod 1133不重合线段
- 机器学习——简单线性回归(下)
- DFS:POJ3620-Avoid The Lakes(求最基本的联通块)
- 史上最牛的员工激励
- Search Insert Position