Reward(拓扑排序+邻接表)

RewardCrawling in process...Crawling failedTime Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

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 <string.h>
#include <stdlib.h>
#define inf 99999999
struct node
{
    int u;
    int v;
    int next;
}p[20010];
int in[10010];
int cost[10010];
int vis[10010];
int dis[20010];
int n,m,i,j,k,t,count;
int sum,flag;
void topo()
{
    memset(vis,0,sizeof(vis));
    count=0;
    sum=0;
    flag=1;
    for(i=1;i<=n;i++)
        {
            cost[i] = 888;
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                if(in[j]==0)
                {
                    in[j]--;
                    count++;
                    sum+=cost[j];
                    for(k=dis[j];k;k=p[k].next)
                    {
                        in[p[k].v]--;
                        if(cost[p[k].v]<cost[j]+1)
                        {
                            cost[p[k].v] = cost[j]+1;
                        }
                    }
                }
            }
        }
       if(count==n)
        printf("%d\n",sum);
       else
        printf("-1\n");

}
int main()
{
    int a,b;
    while(~scanf("%d %d",&n,&m))
    {
        memset(in,0,sizeof(in));
        memset(dis,0,sizeof(dis));
        memset(cost,0,sizeof(cost));
        t=1;
        for(i=0;i<m;i++)
        {
             scanf("%d %d",&a,&b);
            p[t].u=b;
            p[t].v=a;
            p[t].next=dis[b];
            dis[b]=t;
            t++;
           in[a]++;
        }
        topo();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rocky0429

一块也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值