hdu 2647 拓扑排序变形
2016-05-11 20:18
344 查看
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;
}
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;
}
相关文章推荐
- C#数据结构之顺序表(SeqList)实例详解
- Lua教程(七):数据结构详解
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构之队列(Quene)实例详解
- C#数据结构揭秘一
- C#数据结构之单链表(LinkList)实例详解
- 数据结构之Treap详解
- 用C语言举例讲解数据结构中的算法复杂度结与顺序表
- C#数据结构之堆栈(Stack)实例详解
- C#数据结构之双向链表(DbLinkList)实例详解
- JavaScript数据结构和算法之图和图算法
- Java数据结构及算法实例:冒泡排序 Bubble Sort
- Java数据结构及算法实例:插入排序 Insertion Sort
- Java数据结构及算法实例:考拉兹猜想 Collatz Conjecture
- java数据结构之java实现栈
- java数据结构之实现双向链表的示例
- Java数据结构及算法实例:选择排序 Selection Sort
- Java数据结构及算法实例:朴素字符匹配 Brute Force
- Java数据结构及算法实例:汉诺塔问题 Hanoi
- Java数据结构及算法实例:快速计算二进制数中1的个数(Fast Bit Counting)