标签:hdu1285
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11937 Accepted Submission(s): 4753
4 3 1 2 2 3 4 3
1 2 4 3
这题涉及到运算符重载,如果是结构体可以直接用友元函数重载,但是对于内置类型就要换种方法了。
#include <stdio.h> #include <string.h> #include <queue> #include <vector> #define maxn 502 using namespace std; bool map[maxn][maxn]; int indegree[maxn], ans[maxn], queue[maxn]; void topSort(int n) { int id = 0, u; priority_queue<int, vector<int>, greater<int> > Q; for(int i = 1; i <= n; ++i) if(!indegree[i]) Q.push(i); while(!Q.empty()){ u = ans[id++] = Q.top(); Q.pop(); for(int i = 1; i <= n; ++i){ if(map[u][i] && !--indegree[i]) Q.push(i); } } } int main() { int n, m, a, b, i; while(scanf("%d%d", &n, &m) != EOF){ memset(map, 0, sizeof(map)); while(m--){ scanf("%d%d", &a, &b); if(!map[a][b]){ ++indegree[b]; map[a][b] = 1; } } topSort(n); for(i = 0; i < n; ++i) if(i != n - 1) printf("%d ", ans[i]); else printf("%d\n", ans[i]); } return 0; }
HDU1285 确定比赛名次 【拓扑排序】,布布扣,bubuko.com
标签:hdu1285
原文地址:http://blog.csdn.net/chang_mu/article/details/38323499