hdu2647 && TOJ3648 Reward

来源:互联网 发布:复旦金融硕士 知乎 编辑:程序博客网 时间:2024/06/02 18:59

Reward

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


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 11 22 21 22 1
 

Sample Output
1777-1
 

Author
dandelion
 

Source
曾是惊鸿照影来
 
#include <iostream>#include <queue>#include <string>#include <cstdio>#include <cstring>#include <vector>#include <stack>#include <algorithm>using namespace std;const int maxn = 10010;//这道题目在TOJ上提交RE,表示无奈.int gn, gm;int mon[maxn];vector<int> g[maxn];int du[maxn];int num;bool vis[maxn];//标记点的存在.int counter;//统计一共有多少不同的人,应有多少人出队入队.void init() {    int i;    counter = 0;    num = 0;//记录出队多少人.    memset(du, 0, sizeof(du));    memset(vis, false, sizeof(vis));    memset(mon, 0, sizeof(mon));    for(i = 0; i < maxn; i++) {        g[i].clear();    }}void work() {    int i;    queue<int> Q;    while(!Q.empty()) Q.pop();    for(i = 1; i <= gn; i++) {        if(!du[i] && vis[i]) {            Q.push(i);        }        if(vis[i]) counter++;    }    while(!Q.empty()) {        int x = Q.front();        Q.pop();        num++;        for(i = 0; i < (int)g[x].size(); i++) {            int t = g[x][i];            mon[t] = max(mon[t], mon[x]+1);            du[t]--;            if(!du[t]) {                Q.push(t);            }        }    }    if(num == counter) {       int tot = 0;        for(i = 0; i < maxn; i++) {            tot += mon[i];        }        tot += gn*888;        printf("%d\n", tot);    }    else {        printf("-1\n");    }}int main(){    int i;    int from, to;    while(scanf("%d%d", &gn, &gm) != EOF) {        init();        for(i = 1; i <= gm; i++) {            scanf("%d%d", &to, &from);            vis[from] = true;            vis[to] = true;            g[from].push_back(to);            du[to]++;        }        if(gn==0) {            printf("0\n");            continue;        }        work();    }    return 0;}
原创粉丝点击