题目1449:确定比赛名次(拓扑排序问题)
2017-04-21 17:45
375 查看
题目链接:http://ac.jobdu.com/problem.php?pid=1449
详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus
参考代码:
详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus
参考代码:
// // 1449 确定比赛名次.cpp // Jobdu // // Created by PengFei_Zheng on 21/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. // #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <cmath> #include <vector> #include <queue> #define MAX_SIZE 501 using namespace std; int inDegree[MAX_SIZE]; vector<int> edge[MAX_SIZE]; priority_queue< int, vector<int>, greater<int> > myQueue; int n, m; int main(){ while(scanf("%d%d",&n,&m)!=EOF){ //initation for(int i = 1 ; i <= n ; i++){ inDegree[i]=0; edge[i].clear(); } while(!myQueue.empty()) myQueue.pop(); while(m--){ int p1, p2; scanf("%d%d",&p1,&p2); edge[p1].push_back(p2); inDegree[p2]++; } for(int i = 1 ; i <= n ; i++){ if(inDegree[i] == 0){ myQueue.push(i); } } //there is no need to judge whether the graph is legal or not. int ans = 0; while(!myQueue.empty()){ ans++; int nowP = myQueue.top(); ans == n ? printf("%d\n",nowP) : printf("%d ",nowP); myQueue.pop();//remove the node which has the smaller id and inDegree is equal to 0 for(int i = 0 ; i < edge[nowP].size() ; i++){//update the node's inDegree inDegree[edge[nowP][i]]--; if(inDegree[edge[nowP][i]]==0){ myQueue.push(edge[nowP][i]);//add new node(inDegree is 0) to the priority_queue } } } } return 0; } /************************************************************** Problem: 1449 User: zpfbuaa Language: C++ Result: Accepted Time:10 ms Memory:1532 kb ****************************************************************/
相关文章推荐
- 九度 题目1449:确定比赛名次
- 九度 题目1449:确定比赛名次
- Jobdu 题目1449:确定比赛名次
- HDU 1285 确定比赛名次(第一个拓扑排序题目)
- 九度 题目1449:确定比赛名次
- 题目1449:确定比赛名次
- HDOJ题目1285确定比赛名次(拓扑排序,模板)
- 九度题目1449:确定比赛名次
- 九度 题目1449:确定比赛名次
- 题目1449:确定比赛名次
- 题目1449:确定比赛名次
- 九度1449:确定比赛名次(拓扑排序)
- 题目1449:确定比赛名次
- 九度题目1449:确定比赛名次
- hdu 1285 确定比赛名次 (简单的拓扑排序)
- 拓扑排序模版--hdu 1285 确定比赛名次
- 【拓扑排序】 hdu1285 确定比赛名次
- hdu 1285 确定比赛名次(拓扑排序)
- hdu1285确定比赛名次 拓扑排序之1285普通方法
- hdu-1285确定比赛名次---拓扑排序