Reward
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7498 Accepted Submission(s): 2364
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.
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.
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
Author
dandelion
Source
Recommend
yifenfei
好难啊……:(这几天写的题解都是难难难
这个光拓扑会悲剧啊OTZ这个必须一次把所有为0的都查了(因为这个想不出来白折腾了半天
),然后再一次把他们都抽出去,不然要多给好多钱=。=
所以拓扑内部要处理一下,具体看代码吧。

另外,这个N太大了,要用邻接表……
题意:要发工资,有些人要求工资比另一些人高。第一行n,m。n是总人数,接下来m行输入a,b。表示a要求工资比b要高。每人工资最少要发888,问最后老板最少给他们发多少发满足所有人的要求
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 20000+10
int head[N],G[N],vis[N],edgenum;
int n,m;
struct stu
{
int t,next;
}edge[N*2];
void addedge(int a,int b)
{
edge[edgenum].t=b;
edge[edgenum].next=head[a];
head[a]=edgenum++;
G[b]++;
}
int top()
{
int temp[N];
int i,j,kp=0,num=0,tmp,sum=0;
while(1)
{
tmp=0;
for(j=1;j<=n;j++)
{
if(G[j]==0)
{
G[j]=-1;
kp++;
sum+=num+888;
temp[tmp++]=j;
}
}
if(tmp==0) return -1;
if(kp>=n) return sum;
for(i=0;i<tmp;i++)
{
for(j=head[temp[i]];j!=-1;j=edge[j].next)
{
int k=edge[j].t;
G[k]--;
}
}
num++;
}
}
int main()
{
int i,a,b;
while(~scanf("%d%d",&n,&m))
{
memset(vis,0,sizeof(vis));
memset(G,0,sizeof(G));
memset(head,-1,sizeof(head));
edgenum=0;
for(i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
addedge(b,a);
}
printf("%d\n",top());
}
return 0;
}