图 相关题目Ⅱ

[hdu 2647] (http://acm.hdu.edu.cn/showproblem.php?pid=2647)
题目描述:
Reward

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5808 Accepted Submission(s): 1778

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.

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

题目分析:
1. 此题数据较大,在存储数据时采用链式前向星的方法;
2. 我的失误点:本想建好图后,直接用拓扑排序将其排出来,然后用底薪888*n+提成n*(n-1)/2的方法去算,但是由于拓扑排序在能实现排序时,排出来的可能性有多种,所以在不确定的情况下,为了使钱数最少,则可以将这样的员工给同样多的钱,即把他们放在工资的同一层,所以优化后代码如下:
3. 需要注意的是:输入数据是要求前者的工资>=后者的工资,则要注意在遍历时要先遍历后者,再遍历前者,则要反向建图,记录前者的入度.

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>

using namespace std;
const int N=20010;
int n,m,ip,k;
int p1,p2;
int head[N],indegree[N];
int seq[N];
int sum,money[N];
struct note
{
    int to;
    int next;
} edge[N];

void add(int u,int v)
{
    edge[ip].to=v;
    edge[ip].next=head[u];
    head[u]=ip++;
}
int tope()
{
    queue<int>q;
    int indeg[N];
    for(int i=1; i<=n; i++)
    {
        indeg[i]=indegree[i];
        if(indeg[i]==0)
            q.push(i);
    }
    k=0;//记录有几个人在比赛中出现过
    bool res=false;
    while(!q.empty())
    {
        if(q.size()!=1) res=true;
        int u=q.front();
        sum+=money[u];
        q.pop();
        seq[k++]=u;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            indeg[v]--;
            if(indeg[v]==0)
            {
                q.push(v);
                money[v]=money[u]+1;//当前层数的钱数,等于其上一层的钱数+1
            }
        }
    }
    if(k<n) return -1;
    if(res) return 0;
    return 1;
}

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(head,-1,sizeof(head));
        memset(indegree,0,sizeof(indegree));
        ip=0;sum=0;
        for(int i=1;i<=n;i++)
        {
            money[i]=888;//初始化为最基本、最少的工资
        }
        for(int i=0; i<m; i++)
        {
            scanf("%d%d",&p1,&p2);
            add(p2,p1);//反向建图
            indegree[p1]++;//记录前者的入度
        }
        int t=tope();
        if(t==-1)
            printf("-1\n");
        else
        {
            printf("%d\n",sum);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值