hdu_2647_拓扑排序_邻接表或者vector_优化内存_拓扑+贪心_用的是拓扑思想_并不是拓扑

Reward

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

直接用数组模拟,爆内存,后来用vector,邻接表优化就好
题意:
给你n,m,n代表有从1到n个人,有m 组数,每组有a和b,a的工资要比b的高,最低工资为888 ,求解老板须给所有工人开的最低工资
思路:
找到每个入度为零的点加888,然后 这个点之后的点的工资都加1,删除和它有关的所有边
邻接表:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N=22222;
struct node {int to,next; }num[N];
int head[N],in[N],val[N];
void add(int u,int v,int cnt)
{
    num[cnt].to=v;
    num[cnt].next=head[u];
    head[u]=cnt;
    in[v]++;
}
int main()
{
    int n,m,i,j;
    while(cin>>n>>m)
    {
        for(i=0;i<=n;i++)
        {
            head[i]=-1;
            num[i].next=num[i].to=in[i]=0;
            val[i]=888;
        }
        for(i=0;i<m;i++)
        {
            int a,b;
            cin>>a>>b;
            add(b,a,i);
        }
        queue<int > q;
        int k=0,sum=0;
        for(i=1;i<=n;i++)
        {
            if(in[i]==0)
            {
                q.push(i);
                k++;
                sum+=val[i];
            }
        }
        while(!q.empty())
        {
            int v=q.front();
           q.pop();
           for(i=head[v];i!=-1;i=num[i].next)
           {
               j=num[i].to;
               if(val[v]+1>val[j]) val[j]=val[v]+1;
               in[j]--;
               if(in[j]==0)
               {
                   k++;
                   sum+=val[j];
                   q.push(j);
               }
           }

        }
        if(k==n)
            cout<<sum<<endl;
        else
            puts("-1");
    }
    return 0;
}

vector

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
const int N=20001;
int main()
{
    int n,m,i,j;
    while(cin>>n>>m)
    {
        int in[N]= {0},val[N];
        vector<int> edge[N];
        for(i=0; i<=n; i++)
        {
            val[i]=888;
            edge[i].clear();
        }
        while(m--)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            edge[b].push_back(a);
            in[a]++;
        }
        int k=0,sum=0;
        queue<int> q;
        for(i=1; i<=n; i++)
        {
            if(in[i]==0)
            {
                q.push(i);
                k++;
                sum+=val[i];
            }
        }
        while(!q.empty())
        {
            int v=q.front();
            q.pop();
            for(i=0; i<edge[v].size(); i++)
            {
                j=edge[v][i];
                in[j]--;
                if(val[v]+1>val[j]) val[j]=val[v]+1;
                if(in[j]==0)
                {
                    k++;
                    q.push(j);
                    sum+=val[j];
                }
            }
        }
        if(k==n)
            cout<<sum<<endl;
        else
            puts("-1");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值