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. Nginx的反向代理

    先通过简单的图来说明一下正向代理和反向代理吧~ 正向代理 代理其实就是一个中介,A和B本来可以直连,中间插入一个C,C就是中介.刚开始的时候,代理多数是帮助内网client访问外网server用的(比 ...

  2. 暑假集训(1)第二弹 -----Catch the cow(Poj3278)

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  3. IOS 学习笔记 2015-04-15 Xcode 工程模板分类

    一 Application类型    我们大部分呢的开发工作都是使用Application类型的模板创建IOS程序开始的,该类型包括5个模板1 Master-Detail-Application    ...

  4. ASP.NET全局文件与防盗链

    添加Web→全局应用程序类,注 文件名不要改 Global.asax 全局文件是对Web应用声明周期的一个事件响应的地方,将Web应用启动时初始化的一些代码写到 Application_Start中, ...

  5. ASP.NET缓存 Cache

    缓存介绍 如果每次进入页面的时候都查询数据库生成页面内容的话,如果访问量非常大,则网站性能会非常差,而如果只有第一次访问的时候才查询数据库生成页面内容,以后都直接输出内容,则能提高系统性能,这样无论多 ...

  6. 浏览器测试功能(jquery1.9以后已取消)

    // 1.9以后取消了msie这些私有方法判断.这里封装加回. var matched = (function(ua) { ua = ua.toLowerCase(); var match = /(o ...

  7. 泛型集合List<T> Dictionary<K,V>

    List<T>类似于ArrayList,ArrayList的升级版. 各种方法:Sort().Max().Min().Sum()…   Dictionary<K,V>类似于Ha ...

  8. 为 DataGridView 控件添加行号

    虽然好像不经常用到,不过还是记下来防止以后用到 /// <summary> /// 为 DataGridView 控件添加行号 /// </summary> /// <p ...

  9. node论坛练手

    当时学node,自己写了个论坛练手,现在看还是有很多问题,有时间好好改改 https://github.com/hitbs228/countdown

  10. php 定时执行任务

    之于是否控制,可以做到的,应借用第三个条件: config.php <?phpreturn 1;?> cron.phpignore_user_abort();//关掉浏览器,PHP脚本也可 ...