hdu 2647 拓扑排序变形
来源:互联网 发布:枪球联动算法 编辑:程序博客网 时间:2024/05/22 13:39
Reward
TimeLimit: 2000/1000 MS (Java/Others) Memory Limit:32768/32768 K (Java/Others)
Total Submission(s): 6976 Accepted Submission(s): 2163
Problem Description
Dandelion's uncle is a boss of afactory. As the spring festival is coming , he wants to distribute rewards tohis 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 thedistributing of rewards ,just like a's reward should more than b's.Dandelion'sunclue wants to fulfill all the demands, of course ,he wants to use the leastmoney.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 rewardshould be more than b's.
Output
For every case ,print the least moneydandelion 's uncle needs to distribute .If it's impossible to fulfill all theworks' demands ,print -1.
Sample Input
21
12
22
12
21
Sample Output
1777
-1
Author
dandelion
Source
曾是惊鸿照影来
题目大意:给你n个人,m个关系,表示a比b的奖金要多。问最少分配的奖金总数是多少。
题目要求的范围是10000 所以要用邻接链表来做 否则会超内存
因为要a的奖金要比b的多 所以 把a看成入度 把b看成出度
还有一个地方需要注意:判断输出-1的情况不能只判断没有一个入度为0的点,因为有可能在中间就出现矛盾了,如:a——>b——>c——>d——>c有入度为0的点,但却要输出-1;
不懂得地方看下面代码的注释
AC代码:
#include<cstdio>
#include <cstring>
#include<queue>
#include<iostream>
#define MAX20005
using namespacestd;
int n,m,a,b;
inthead[MAX],degree[MAX],money[MAX];
struct node
{
int to;
int next;
}e[MAX];
int main()
{
while(~scanf("%d%d",&n,&m))
{
queue<int>s;
memset(head,-1,sizeof(head));
memset(degree,0,sizeof(degree));
for(int i = 1; i <= n; i++) ///总共n个人 每个人最少888元
money[i] = 888;
for(int i = 1; i <= m; i++)
{
scanf("%d%d",&a,&b);
degree[a]++; ///入度
e[i].to = a;
e[i].next = head[b]; ///e[i].next表示a指向多少个点
head[b] = i; ///head数组用来储存第几个点
}
for(int i = 1; i <= n; i++) ///总共n个人
{
if(degree[i]==0)
{
s.push(i); ///把入度为零的进队
}
}
int sum = 0;
int ans = 0;
while(!s.empty())
{
int u = s.front();
s.pop();
sum+=money[u];
ans++;
for(int j = head[u]; j!=-1; j =e[j].next) ///通过head数组来找到输入u点的时候是第几次
{
if(--degree[e[j].to]==0) ///把下一个点入队
{
s.push(e[j].to);
money[e[j].to] = money[u] +1; ///下一个点的奖金肯定比当前这个多
}
}
}
if(ans!=n) ///如果调用的次数小于n 那么可能中间有矛盾 不能满足条件
printf("-1\n");
else
printf("%d\n",sum);
}
return 0;
}
- hdu 2647 拓扑排序变形
- 拓扑排序变形poj3687
- HDU-2647拓扑排序
- hdu 2647 拓扑排序
- hdu 2647 拓扑排序
- 拓扑排序 HDU 2647
- hdu 2647 拓扑排序
- hdu 2647 拓扑排序
- hdu 2647(拓扑排序)
- hdu 2647 拓扑排序
- hdu 2647拓扑排序
- hdu 2647 拓扑排序
- 拓扑排序 hdu 2647
- poj 1094 拓扑排序变形
- hdu 2647 Reward 拓扑排序
- hdu 2647 Reward 拓扑排序
- hdu 2647 Reward 拓扑排序
- hdu 2647 bfs+拓扑排序
- hdu 1285 拓扑排序 入门
- DataX 在异构的数据库/文件系统之间高速交换数据的工具
- poj 2506
- 548 Tree
- IDL create a empty string
- hdu 2647 拓扑排序变形
- Python异步并发框架
- 我的第一次脚本编程
- ASIHTTPRequest和AFNetworking比较
- 初学shell(必须掌握的20条指令)
- git忽略对已入库文件的修改
- C++第5次实验-数组分离
- 第10、11周项目3:警察和厨师(1)
- IntelliJ IDEA