zoukankan      html  css  js  c++  java
  • hdoj 2647 Reward【反向拓扑排序】

    Reward

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


    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
     
    唉!用优先队列WA了一下午,现在想想,人家题中也没说这种如果同样优先级谁放前边的话,而且根据题意同样优先级的人发的钱数应该一样
    题解:发奖金,每个人至少888,但是一些工作做得好的觉得应该多拿,所以就排出了优先级,优先级高的要比优先级低的多拿,优先级相同的拿同样的钱,
            问最少发出去多少奖金;
    题解:还是要用反向拓扑,因为所给的数据可能不只是一棵树,而是森林,
    这里给出一组数据
    3  2
    1  2
    1  3
    结果是2665不是2666
    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<vector>
    #define MAX 20010
    using namespace std;
    vector<int>map[MAX];
    int vis[MAX];
    int reward[MAX];
    int n,m,sum;
    void getmap()
    {
    	int i,j;
    	memset(vis,0,sizeof(vis));
    	for(i=1;i<=n;i++)
    	    map[i].clear();
    	for(i=1;i<=m;i++)
    	{
    		int a,b;
    		scanf("%d%d",&a,&b);
    		map[b].push_back(a);
    		vis[a]++;
    	}   
    }
    void tuopu()
    {
    	int i,j,ok=0,sum=0;
    	queue<int>q;
    	memset(reward,0,sizeof(reward));
    	while(!q.empty())
    	    q.pop();
    	for(i=1;i<=n;i++)
    	    if(vis[i]==0)
    	    {
    	    	q.push(i);
    	    	reward[i]=888;
    		}
    		int u,v;
    		int ans=0;
    		while(!q.empty())
    		{
    			u=q.front();
    			q.pop();
    			ans++;
    			for(i=0;i<map[u].size();i++)
    			{
    				v=map[u][i];
    				vis[v]--;
    				if(vis[v]==0)
    				{
    					q.push(v);
    					reward[v]=reward[u]+1;
    				}   
    			}
    		}
    		if(ans!=n)
    			printf("-1
    ");
    		else
    		{
    			for(i=1;i<=n;i++)
    		    sum+=reward[i];
    		    printf("%d
    ",sum);
    		}
    }
    int main()
    {
    	while(scanf("%d%d",&n,&m)!=EOF)
    	{
    		getmap();
    		tuopu();
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    Go 函数方法
    rz上传文件乱码
    pip问题:ImportError: cannot import name main
    docker启动报错 docker: Error response from daemon: OCI runtime create failed: container_linux.go:348
    python遍历列表删除多个元素的坑
    python写入csv文件时的乱码问题
    python中的exec()函数和eval()函数
    tornado的IOLoop.instance()方法和IOLoop.current()方法区别
    安装mysql-python时报错
    python将科学计数法表示的数值的字符串转换成数值型数据
  • 原文地址:https://www.cnblogs.com/tonghao/p/4727755.html
Copyright © 2011-2022 走看看