Reward

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3015    Accepted Submission(s): 907

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.
 
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
 
 
思路:拓扑排序。此题数据量大,使用邻接矩阵超出空间,需要使用链式前向星(静态建邻接表),另外还有一点要注意,要分层排序,每层的花费是一样大的。
  其实拓扑排序关键是如何更新节点的入度以及如何在计算机中存储图;
 
下面是超空间代码:
数据结构:邻接矩阵
 map[MAX][MAX];
#include<stdio.h>
#include<string.h>
int degree[],map[][]; int vis[],pre[];
int main()
{
int n,m,a,b,i,j,k,t,p,flag,sum,temp;
while(~scanf("%d%d",&n,&m))
{
memset(degree,,sizeof(degree));
memset(map,,sizeof(map));
memset(vis,,sizeof(vis));
t = sum = flag = ;
while(m--)
{
scanf("%d%d",&a,&b);
if(!map[a][b])
{
degree[b]++;
map[a][b] = ;
}
}
for(i = ;i < n;i ++)
{
k = ;
for(j = ;j <= n;j ++)
{
if(degree[j] == && vis[j] == )
pre[k++] = j;
}
sum += (+t)*k;
for(j = ;j < k;j ++)
{
vis[pre[j]] = ;
for(p = ;p <= n;p ++)
{
if(map[pre[j]][p] == && vis[p]== )
{
degree[p]--;
map[pre[j]][p] = ;
}
}
}
t++;
}
for(i = ;i <=n;i ++)
{
if(degree[i])
{
flag = ;
break ;
}
}
if(!flag)
printf("%d\n",sum);
else
printf("-1\n");
}
return ;
}

下面是AC代码:

数据结构:链式前向星
 int head[MAX];
typedef struct
{
int to;       //记录终点;
int next;   //记录下一个节点的位置;
int ...       //记录其他一些信息,与题有关;
}EdgeNode;
EdgeNode edge[MAX_m];
cin >> i >> j >> ...;
edge[k].to = j;
edge[k].next = head[i];
edge[k].... = ...;
head[i] = k++;
   #include<stdio.h>
#include<string.h>
typedef struct
{
int to;
int next;
}EdgeNode;
EdgeNode Edge[];
int head[],node[];
int cnt,indegree[],vis[];
void init()
{
cnt = ;
memset(vis,,sizeof(vis));
memset(head,-,sizeof(head));
memset(indegree,,sizeof(indegree));
} void add_edge(int n,int m)
{
Edge[cnt].to = n;
Edge[cnt].next = head[m];
head[m] = cnt++;
} int main()
{
int n,m,a,b,i,j,k,t,p,sum;
while(~scanf("%d%d",&n,&m))
{
init();
sum = p = ;
while(m--)
{
scanf("%d%d",&a,&b);
indegree[a]++;
add_edge(a,b);
}
for(i = ;i < n;i ++)
{
t = ;
for(j = ;j <= n;j ++)
{
if(indegree[j] == && vis[j] == )
node[t++] = j;
}
sum += (+p)*t;
p++;
for(j = ;j < t;j ++)
{
vis[node[j]] = ;
for(k = head[node[j]];k != -;k = Edge[k].next)
{
if(!vis[Edge[k].to])
indegree[Edge[k].to]--;
}
}
}
for(i = ;i <= n;i ++)
{
if(indegree[i])
{
sum = -;
break ;
}
}
printf("%d\n",sum);
}
return ;
}

Reward的更多相关文章

  1. ACM: hdu 2647 Reward -拓扑排序

    hdu 2647 Reward Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Des ...

  2. 扩展KMP --- HDU 3613 Best Reward

    Best Reward Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=3613 Mean: 给你一个字符串,每个字符都有一个权 ...

  3. 回文串---Best Reward

    HDU   3613 Description After an uphill battle, General Li won a great victory. Now the head of state ...

  4. hdu 2647 Reward

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2647 Reward Description Dandelion's uncle is a boss o ...

  5. hdoj 2647 Reward【反向拓扑排序】

    Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  6. Reward HDU

    Reward                                    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32 ...

  7. Reward(拓扑结构+邻接表+队列)

    Reward Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submis ...

  8. HDU 3613 Best Reward 正反两次扩展KMP

    题目来源:HDU 3613 Best Reward 题意:每一个字母相应一个权值 将给你的字符串分成两部分 假设一部分是回文 这部分的值就是每一个字母的权值之和 求一种分法使得2部分的和最大 思路:考 ...

  9. HDU 2647 Reward(图论-拓扑排序)

    Reward Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is comin ...

随机推荐

  1. webstorm的默认project编码为系统编码GBK.

    使用新的IDE,而不会设置,会给你带来灾难. 如下为我是用webstorm时遇到的文件编码问题. 纳闷很久,终于发现是IDE的设置问题. 参考

  2. unsigned int 转 RGB

    unsigned int颜色存储格式:0xaabbggrr,其中a,b,g,r分别表示,透明度.蓝色.绿色.红色. 方法一:使用windows宏 unsigned int clr = 0x00FF00 ...

  3. linux强大IDE——Geany配置说明

    今天开始用Ubuntu了(主要是为了防止自己在windows下不自觉的打游戏之类的)   刚开始用的很不习惯  找不到合适的编译器(DEV c++什么时候才能出Linux的啊)  先后下了codeli ...

  4. yii2单独给input或者其他标签定义class

    <?= $form->field($model, 'price5', ['options' => ['class' => 'col-sm-6']])->textInput ...

  5. [日语歌词] If

    原唱:西野カナ (にしのカナ) 作词:西野カナ/GIORGIO 13 作曲:GIORGIO CANCEMI 1.单词表 仮名 漢字 ひ 日 あめ 雨 や 止 ちがい 違い とおり 通り じかん 時間 ...

  6. POJ 2406 Power Strings 1961的简化版,kmp的next数组的应用

    题目: http://poj.org/problem?id=2406 跟1961差不多,题解就不写了,一开始理解错题了,导致WA一次. #include <stdio.h> #includ ...

  7. c# .net使用SqlDataReader注意的几点

    转自:http://blog.knowsky.com/258608.htm 1.当SqlDataReader没有关闭之前,数据库连接会一直保持open状态,所以在使用SqlDataReader时,使用 ...

  8. Spring实战——无需一行xml配置实现自动化注入

    已经想不起来上一次买技术相关的书是什么时候了,一直以来都习惯性的下载一份电子档看看.显然,如果不是基于强烈的需求或强大的动力鞭策下,大部分的书籍也都只是蜻蜓点水,浮光掠影. 就像有位同事说的一样,有些 ...

  9. 解决EXC_BAD_ACCESS错误的一种方法--NSZombieEnabled

    iOS 程序开发时经常用遇到 EXC_BAD_ACCESS 错误导致 Crash,出现这种错误时一般 Xcode 不会给我们太多的信息来定位错误来源,只是在应用 Delegate 上留下像Thread ...

  10. 集合工具类 - CollectionUtil.java

    集合工具类,提供数组转LIST.数组转SET.合并集合.计算笛卡儿积等方法. 源码如下:(点击下载 -  CollectionUtil.java.ArrayUtil.java.commons-lang ...