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. C# 多线程(二) 线程同步基础

    本系列的第一篇简单介绍了线程的概念以及对线程的一些简单的操作,从这一篇开始讲解线程同步,线程同步是多线程技术的难点.线程同步基础由以下几个部分内容组成 1.同步要领(Synchronization E ...

  2. UVA 11300 Spreading the Wealth (数学推导 中位数)

    Spreading the Wealth Problem A Communist regime is trying to redistribute wealth in a village. They ...

  3. android软件开发之webView.addJavascriptInterface循环渐进【一】

    本篇文章由:http://www.sollyu.com/android-software-development-webview-addjavascriptinterface-cycle-of-gra ...

  4. 24种设计模式--观察者模式【Observer Pattern】

     <孙子兵法>有云: “知彼知己,百战不殆:不知彼而知己,一胜一负:不知彼,不知己,每战必殆”,那怎么才能知己知彼呢?知己是很容易的,自己的军队嘛,很容易知道,那怎么知彼呢?安插间谍是很好 ...

  5. ubuntu 安装 桌面 awesome

    受了ubuntu 12.04自带的桌面,运行太卡了 http://www.linuxzen.com/awesometmuxgnomedoda-zao-gao-xiao-linuxzhuo-mian-h ...

  6. git: command not found

    在使用git时,出现“git: command not found”的情况,于是使用yum安装: [root@localhost ~]# yum install -y gitLoaded plugin ...

  7. apache .htaccess 伪静态重定向,防盗链 限制下载...

    301全站跳转 RewriteEngine OnRewriteCond %{HTTP_HOST} ^www\.old\.net$ [NC]RewriteRule ^(.*)$ http://www.n ...

  8. HTML 表格的书写方式:

    首先要进行reset  table{border-collapse:collapse;border-spacing:0;}th{text-align:inherit;} 1. caption标签对整个 ...

  9. PDO操作mysql数据库(一)

    PHP连接mysql数据库: <?php$server = "localhost";$user = "root";$pwd = "123456& ...

  10. python从socket做个websocket的聊天室server

    下面的是server端:把IP改成自己的局域网IP: #coding:utf8 import socket,select import SocketServer import hashlib,base ...