标签:hdu2647
2 1 1 2 2 2 1 2 2 1
1777 -1
题解:可以把每个人的需求想象成层数,若没有需求就在第一层,奖金一层层的发,每增加一层就加一块钱,这样就能满足最终发的奖金数额最小。需要逆序拓扑,另外这题碰到了个奇怪的现象,开始我的map数组开小了,但是提交的时候给的结果是TLE而不是RE,把数组加了一倍后就AC了,看来OJ判题系统并不是太精确。
#include <stdio.h> #include <string.h> #define maxn 10002 int ans, queue[maxn]; struct Node{ int to, next, val; } map[maxn << 1]; struct node{ int first, money, indegree; } head[maxn]; bool topoSort(int n) { int i, front = 0, back = 0, u; for(i = 1; i <= n; ++i) if(!head[i].indegree) queue[back++] = i; while(front != back){ u = queue[front++]; ans += head[u].money; for(i = head[u].first; i != 0; i = map[i].next) if(!--head[map[i].to].indegree){ head[map[i].to].money = head[u].money + 1; queue[back++] = map[i].to; } } return back == n; } int main() { int n, m, a, b, i; while(scanf("%d%d", &n, &m) != EOF){ memset(head, 0, sizeof(head)); for(i = 1; i <= m; ++i){ scanf("%d%d", &a, &b); map[i].to = a; map[i].next = head[b].first; ++head[a].indegree; head[b].first = i; } ans = 888 * n; if(!topoSort(n)) printf("-1\n"); else printf("%d\n", ans); } return 0; }
HDU2647 Reward 【拓扑排序】,布布扣,bubuko.com
标签:hdu2647
原文地址:http://blog.csdn.net/chang_mu/article/details/38336427