HDU2647-Reward(拓扑排序)

简介:

Reward

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


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
 
题意: 有n个人。m个关系:输入a,b 表示a比b大。

最少的工资为888,让你求最小的工资和


思路:拓扑排序。依据层数计算每一个人的工资,累加就可以。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;

const int maxn = 10000+10;
vector<int> g[maxn];
int du[maxn];
int n,m;
int sum;
queue<int> que;
int head[maxn];
int now;
void init(){
    sum = 0;
    now = 888;
    memset(du,0,sizeof du);
    for(int i = 0; i <= n; i++) g[i].clear();
    while(!que.empty()) que.pop();
}

int main(){

    while(~scanf("%d%d",&n,&m)){
        init();
        int a,b;
        while(m--){
            scanf("%d%d",&a,&b);
            g[b].push_back(a);
            du[a]++;
        }
        int cnt = 0;
        for(int i = 1; i <= n; i++){
            if(du[i]==0){
                que.push(i);
                cnt++;
            }
        }
        while(!que.empty()){
            int qsize = que.size();
            while(qsize--){
                int k = que.front();
                sum+=now;
                que.pop();
                for(int i = 0; i < g[k].size(); i++){
                    int x = g[k][i];
                    du[x]--;
                    if(du[x]==0){
                        que.push(x);
                        cnt++;
                    }
                }
            }
            now++;

        }
        if(cnt<n){
            printf("-1\n");
        }else{
            printf("%d\n",sum);
        }
    }
    return 0;
}








本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5048279.html,如需转载请自行联系原作者

相关文章
HDU-1874,畅通工程续(Floyd最短路)
HDU-1874,畅通工程续(Floyd最短路)
|
算法 机器学习/深度学习
|
并行计算 算法 Java
HDU 1874 畅通工程续【Floyd算法实现】
畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 53806    Accepted Submission(s): 20092 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路。
1063 0
【HDU 4771 Stealing Harry Potter&#39;s Precious】BFS+状压
2013杭州区域赛现场赛二水。。。 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间。 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点,搜索下一个最近的宝藏,直至找到全部k个宝藏。
1015 0
hdu 1213 并查集
http://acm.hdu.edu.cn/showproblem.php?pid=1213 #include&lt;iostream&gt; #include&lt;cstdio&gt; using namespace std; #define N 1000 int father[N]; void ufset() { for(int i=0;i&lt;N;i++)
849 0
hdu1175 bfs
// hdu 1175 #include &lt;iostream&gt; #include &lt;cstring&gt; #include &lt;cstdio&gt; #include &lt;queue&gt; using namespace std; struct node { int x , y ; int step ; }s,e; nod
912 0