HDU 2647-Reward(拓扑排序+深度理解遍历)

Reward

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


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
 

Author
dandelion
 

Source
 

解题思路:做了这么多拓扑排序的题目后,其实就是个bfs,首先要根据题意把图建好,遍历时就相当于一层一层的往深层遍历,所以这一题就主要是考到了遍历方式,要钱的数目最小,由于bfs的性质,所以深层的那个节点一定时它上一个节点的价值再加一,这样才会使得整个图的权值最小,有点像实现了一棵最小生成树。所以我们定义一个新的数组来存这个权值即可。

AC代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define in1(a) scanf("%d" ,&a);
#define in2(a, b) scanf("%d%d", &a, &b);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
using namespace std;
typedef long long LL;
typedef pair<int, int> par;
const int mod = 1e9+7;
const int INF = 1e9+7;
const int N = 1000010;
const double pi = 3.1415926;

int n ,m, head[20010], cnt, du[10010], ans, b[10010];

void init()
{
    cnt = 0;
    mem1(head);
    mem0(du);
    mem0(b);
}

struct edge
{
    int end;
    int next;
}e[20010];

void add(int u, int v)
{
    e[cnt].end = v;
    e[cnt].next = head[u];
    head[u] = cnt ++;
}

void topo()
{
    queue<int> q;
    for(int i = 1; i <= n; i ++) {
        if(du[i] == 0)
            q.push(i);
    }
    int s = 0, k = 1, flag = 0;
    while(!q.empty()) {
        int cur;
        cur = q.front();
        q.pop();
        s ++;
        int sum = 0;
        for(int i = head[cur]; i != -1; i = e[i].next) {
            int en = e[i].end;
            du[en] --;
            if(du[en] == 0) {
                sum ++;
                q.push(en);
                b[en] = b[cur] + 1; //更新深层节点权值
            }
        }
    }
    if(s == n) {
        for(int i = 1; i <= n; i ++)
            ans += b[i];
        ans += n*888; //其实不用这一行代码,把b数组初值设为888也是可以的!
        printf("%d\n", ans);
    }
    else printf("-1\n");
}

int main()
{
    int x, y;
    while(~scanf("%d%d", &n, &m)) {
        init();
        ans = 0;
        for(int i = 0; i < m; i ++) {
            scanf("%d%d", &x, &y);
            add(y, x);
            du[x] ++;
        }
        topo();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/i_believe_cwj/article/details/80357195