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
嗯。,。 这道题目是要统计费用 然后为了方便统计层数上的钱数 需要逆向构图
#include<cstdio> #include<cstring> #include<algorithm> #define maxn 11000 #include<vector> #include<iostream> using namespace std;//拓扑排序的分层问题 int m,n,degree[maxn]; vector<int> mapp[maxn];// int topo() { int flag,sum=0; vector<int> ans; int tt=0,re=0;//tt表示层数 while(1) { flag=0; ans.clear(); for(int i=1;i<=m;i++) if(degree[i]==0)//每次得处理一层的 /./ { ans.push_back(i); sum+=(888+tt); degree[i]=-1; flag=1; re++; } if(re>=m) return sum;//当遍历的点数到达m return if(flag==0) return 0; for(int j=0;j<ans.size();j++) { int rett=ans[j]; for(int k=0;k<mapp[rett].size();k++) { int temp=mapp[rett][k]; degree[temp]--; } } tt++; } return sum; } int main() { int a,b,i,j; while(scanf("%d%d",&m,&n)!=EOF) { if(m==0&&n==0) break; memset(degree,0,sizeof(degree)); for(i=0;i<=m;i++) mapp[i].clear(); for(i=0;i<n;i++) { scanf("%d%d",&a,&b); mapp[b].push_back(a); degree[a]++; } int t=topo(); if(t==0) printf("-1 "); else printf("%d ",t); } return 0; }