# Reward

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

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

#include <stdio.h>
#include <vector>
#include <string.h>
#define MAX 10010
using namespace std;
int n,m;
int money[MAX];
int vis[MAX],indegree[MAX],f[MAX];
int sum=0,top,t;
vector <int>map[MAX];
void init()
{
for(int i=1;i<MAX;i++)
map[i].clear();		//清空邻接表
for(int i=0;i<MAX;i++)
money[i]=888;		//初始工资
memset(indegree,0,sizeof(indegree)); 	//入度清零
memset(f,0,sizeof(f));
top=0;	//层数清零
t=0;	//访问结点数清零
}

void toposort()
{
int i,j,k;
bool p;
p=true;
while(p)
{
p=false;
top++;
for(i=1;i<=n;i++)
{
if(!indegree[i])
{
p=true;
f[i]=top;
money[i]=money[i]+top-1;
t++;
}
}
for(i=1;i<=n;i++)
{
if(f[i]==top)
{
for(k=0;k<map[i].size();k++)
{
int v=map[i][k];
indegree[v]--;
}
indegree[i]=-1;
}
}
}
top--;
}

int main()
{
int i,j;
int a,b;
while(~scanf("%d %d",&n,&m))
{
init();
for(i=1;i<=m;i++)
{
scanf("%d %d",&a,&b);
map[b].push_back(a);
indegree[a]++;
}
toposort();
if(t==n)
{
int sum=0;
for(i=1;i<=n;i++)
sum+=money[i];
printf("%d\n",sum);
}
else
printf("-1\n");
}

return 0;
}

• 本文已收录于以下专栏：
• Acm竞赛

## hdu Reward（拓扑排序）

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

## HDU 2647 Reward（图论-拓扑排序）

HDU 2647 Reward（图论-拓扑排序） 题目大意： n个人，m条边，每条边a，b 表示a比b的工资多1，每个人的工资至少888，问你工资和至少多少？如果出现矛盾关系，输出-1 解题思路：...

## HDU 2647 Reward（图论-拓扑排序）

• wujy47
• 2014年08月03日 17:08
• 588

## hdu2647 Reward（拓扑排序）

• ygqwan
• 2013年09月19日 09:35
• 1781

## HDU2647 Reward（拓扑排序）反向建图

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

## HDU：2647 Reward（链表型拓扑排序）

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S...
• zugofn
• 2016年08月05日 21:20
• 140

## hdu 2647 Reward（拓扑排序）

RewardProblem Description Dandelion’s uncle is a boss of a factory. As the spring festival is comin...

举报原因： 您举报文章：HDU 2647 Reward（拓扑排序） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)