Hdoj 2467 Reward(拓扑排序)

本文讨论了工厂老板如何在春节来临之际,通过使用邻接表和拓扑排序来公平且经济地分配奖励给工人,确保多劳多得的原则得以实现。详细解释了在特定条件下如何避免形成环状依赖,从而找到最低成本的解决方案。

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 

题意:
一:不能在用简单的拓扑了,因为此时显然超内存。(此题内存只能开8000000的数组,不能开到一千万)只能用到邻接表。
二:注意多劳多得,可能前面的一个人后面跟着好几个,即,后面那几个人的工资是一样的
最后题意简单,就是叫你判读是否有环。

///@zhangxiaoyu
///2015/8/10

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<stack>
#include<queue>
#include<cmath>
#include<iostream>
using namespace std;
#define INF 0x7f7f7f
#define maxn 1000005

int in[10010],x[10010],money[10010];
int len,num;
int n,m;
vector<int>E[10010];

void init()
{
    for(int i=1;i<=n;i++)
    {
        in[i]=0;
        E[i].clear();
        money[i]=888;
    }
    len=0;
    num=0;
}

void topsort()///vector版本的拓扑排序模板
{
    int j;
    priority_queue<int,vector<int>,greater<int> >qq;///最小堆优队
    for(int i=1;i<=n;i++)
    {
        if(in[i]==0)
            qq.push(i);
    }
    while(!qq.empty())
    {
        j=qq.top();
        qq.pop();
        num++;
        x[++len]=j;
        for(int i=0;i<E[j].size();i++)
        {
            int k=E[j][i];
            in[k]--;
            if(money[j]+1>money[k])
                money[k]=money[j]+1;///求最少的钱,又后边的奖金比前面大,故贪心的加1就是最少的
            if(in[k]==0)
                qq.push(k);
        }
    }
}

int main()
{
    int x,y;
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&x,&y);
            E[y].push_back(x);
            in[x]++;
        }
        topsort();
        if(num<n)
            printf("-1\n");
        else
        {
            int ans=0;
            for(int i=1;i<=n;i++)
            {
                ans+=money[i];
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}
### 冒泡排序算法中的交换次数计算 对于给定的一组数据,在HDOJ平台上的冒泡排序算法通过比较相邻元素来决定是否需要交换它们的位置。如果前面的元素大于后面的元素,则两者会互换位置,这一过程称为一次交换操作[^2]。 当涉及到统计具体的交换次数时,可以通过设置一个计数器`count`来进行记录。每当发生一次有效的交换(即两个逆序元素被调整顺序),就增加该计数器的值。最终输出这个计数值即可得到整个数组完成升序排列过程中发生的总交换次数。 具体实现方式如下所示: ```cpp #include<iostream> using namespace std; int main(){ int n; while(cin>>n){ int a[n], count=0; for(int i=0;i<n;i++) cin>>a[i]; // 开始冒泡排序并统计交换次数 for(int i=n-1;i>=0;i--){ bool flag=false; // 添加标志位用于优化 for(int j=0;j<i;j++){ if(a[j]>a[j+1]){ count++; swap(a[j],a[j+1]); flag=true; } } if(!flag) break; // 如果某一轮没有发生任何交换则提前结束循环 } cout<<count<<endl; } } ``` 上述代码不仅实现了基本功能还加入了额外的优化措施——一旦发现某一趟扫描中没有任何元素进行了交换就可以立即终止后续不必要的迭代,因为此时可以断定序列已经完全有序。 #### 应用场景 这种带有交换次数统计特性的冒泡排序通常适用于以下情况: - **教学目的**:帮助学生更好地理解和掌握基础排序原理及其内部运作机制。 - **性能测试**:评估不同初始状态下各种排序算法之间的效率差异,特别是关注实际运行期间所涉及的数据移动开销。 - **特定竞赛题型**:某些在线评测系统可能会设计专门针对此类特性的问题作为考察点之一,比如要求求解最小化或最大化某种条件下的交换总数等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值