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. use mkisofs 重新打包beini,tinycore linux

    mkisofs -r -J -V Beini-Custom -v --boot-info-table --boot-load-size 4 -b boot/isolinux/isolinux.bin ...

  2. 在js中window.open通过“post”传递参数

    在js中window.open通过“post”传递参数的步骤如下: 如:在A.jsp中 有一个js方法 winow.open,目标地址是 xx.do 1.在A.jsp建一个form,把要设置的值通过j ...

  3. 09_Mybatis开发Dao方法——mapper代理开发规范

    一.开发规范 需要编写mapper.xml映射文件(本项目为userMapper.xml,类似于前面的user.xml). 编写mapper接口需要遵循一些开发规范,这样MyBatis可以自动生成ma ...

  4. Linux常用(持续更新)

    1. scp ./bcec_computernode_check.sh  root@10.254.3.1:/tmp 2. # uname -a # cat /proc/version # cat /e ...

  5. 自己在使用的English词典

    一.ESL/非母语词典 二.EFL/母语词典 1.American Heritage Dictionary 2.World Book Dictionary 3.Oxford Dictionary of ...

  6. 带缓冲的IO和不带缓冲的IO

    文件描述符: 文件描述符是一个小的非负整数,是内核用来标识特定进程正在访问的文件 标准输入/输出/出错: shell为每个程序打开了三个文件描述符,STDIN_FILEON,STDOUT_FILEON ...

  7. Headfirst设计模式的C++实现——简单工厂模式(Simple Factory)

    Pizza.h #ifndef _PIZZA_H #define _PIZZA_H #include <iostream> #include <string> class Pi ...

  8. 简单的doc命令

    cd 切换目录 dir 显示目录列表 mkdir 创建目录(mkdir) rmdir 删除空目录(rmdir test) rmdir  /s 删除非空目录(rmdir test /s) echo 创建 ...

  9. 《工作型PPT设计之道》培训心得

    参加包翔老师的“工作型PPT设计之道>培训,颇多心得,后来为部门新员工和同组同事做了转化培训,将心得整理成一份PPT讲义,效果颇佳.现将主要心得整理于此.因时间仓促,24条心得有拼凑之嫌,有待今 ...

  10. ObjectCopy

    对象的传参用的是传引用,但开发中通常不允许对传入参数进行修改.因此对象拷贝很常用,Python提供一个很方便的对象拷贝方法 如代码: __author__ = 'mengxuan' import co ...