题意:发工资,满足所有a>b,求工资总量最小
分析:拓补排序
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
#include<queue>
#include<cmath>
using namespace std;
const int MAXN=10000+6;
vector<int>vec[MAXN];
int sum[MAXN],in[MAXN];
int n,m;
void init()
{
int i;
for(i=1;i<=n;i++)
{
sum[i]=in[i]=0;
vec[i].clear();
}
}
void add_ded(int a,int b)
{
vec[a].push_back(b);
in[b]++;
}
bool topsort()
{
int cnt,i;
queue<int>q;
for(i=1;i<=n;i++)
if(!in[i])q.push(i);
cnt=0;
while(!q.empty())
{
int x=q.front();q.pop();
cnt++;
for(i=0;i<vec[x].size();i++)
{
if(sum[x]+1>sum[vec[x][i]])sum[vec[x][i]]=sum[x]+1;
if(--in[vec[x][i]]==0)q.push(vec[x][i]);
}
}
return cnt==n;
}
int main()
{
int i,a,b;
while(~scanf("%d%d",&n,&m))
{
init();
for(i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
add_ded(b,a);
}
if(topsort())
{
int ans=0;
for(i=1;i<=n;i++)
ans+=sum[i];
printf("%d\n",n*888+ans);
}
else printf("-1\n");
}
return 0;
}