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;
    }
    

      

  • 相关阅读:
    第一个ExtJS练习(添加用户面板)
    利用高德地图通过给定坐标点画带箭头方向的路径
    安装myeclipse2015 stable 3.0破解之后发生出现SECURITY ALERT:iNTEGRITY CHECK ERROR然后闪退解决方案
    当你的SSM项目中的springmvc.xml发生第一行错误解决方案
    新建maven项目遇到Select an Archetype时没有maven-archetype-webapp处理方法
    XML、HTML、JSON对比
    Mac配置apache2.4.25服务器
    使用HTTP协议访问网络
    android数据持久化存储
    常用git命令
  • 原文地址:https://www.cnblogs.com/tonghao/p/4727755.html
Copyright © 2011-2022 走看看