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
题目大意:老板发工资,基本工资是888,要求前面的人要比后面的人多,问最少能发多少。如果满足不了要求输出-1
思路:拓扑排序,,先判断有没有环,有环 的话输出-1,一开始我想着前面人的钱比后面人的多,和后面的人比前面的人多结果应该相同。。但是。。wa了N次。
这个题目有两个坑:首相就是 2个人的工资可能相等,第二个就是必须到着来。因为第一个人的工资为基础工资,,也就是说可能会多个人是基础工资。。所以工资必须降序
AC代码:
#include<iostream> #include<cstdio> #include<queue> #include<cstring> #include<vector> using namespace std; typedef long long ll; const int N=1E5+7; vector<int >ve[N]; int re[N]; int in[N]; int main(){ int n,m; while(~scanf("%d%d",&n,&m)){ memset(re,0,sizeof(re)); memset(in,0,sizeof(in)); queue<int >que; for(int i=1;i<=m;i++){ int x,y; scanf("%d%d",&x,&y); in[x]++; ve[y].push_back(x); } for(int i=1;i<=n;i++){ if(in[i]==0){ que.push(i); } } int n1=0; ll sum=0; while(que.size()){ int xx=que.front(); que.pop(); sum+=888+re[xx]; n1++; for(int i=0;i<ve[xx].size();i++){ in[ve[xx][i]]--; if(in[ve[xx][i]]==0){ re[ve[xx][i]]=re[xx]+1; que.push(ve[xx][i]); } } } if(n1!=n){ puts("-1"); } else { printf("%lld\n",sum); } for(int i=1;i<=n;i++){ ve[i].clear(); } } return 0; }