HDU 1285 确定比赛名次
2014-10-12 22:57
316 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1285
当输出满足条件且要求输出时字典序小的在前时应该使用优先队列实现的拓扑排序。
http://acm.hdu.edu.cn/showproblem.php?pid=1285
#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> using namespace std; int map[510][510],in[510],d[510]; int n,m; void toposort(){ memset(d, 0, sizeof(d)); for(int i=1; i<=n; i++){ for(int j=1; j<=n; j++){ if(!in[j]){ in[j]--; d[i] = j;///roat for(int k=1; k<=n; k++){ if(map[j][k]){ in[k]--; } } break; } } } } int main(){ // freopen("in.txt", "r", stdin); while(scanf("%d%d", &n, &m) != EOF){ memset(map, 0, sizeof(map)); memset(in, 0, sizeof(in)); for(int i=0; i<m; i++){ int a,b; scanf("%d%d", &a,&b); if(!map[a][b]){ map[a][b] = 1; in[b]++; } } toposort(); for(int i=1; i<=n; i++){ if(i != 1) cout << " "; cout << d[i]; } cout << endl; } return 0; }
当输出满足条件且要求输出时字典序小的在前时应该使用优先队列实现的拓扑排序。
<span style="font-size:12px;color:#000000;">#include <queue> #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> using namespace std; int n,m,map[510][510],into[510]; priority_queue <int, vector<int>, greater<int> > q; void toposort(){ for(int i=1; i<=n; i++){ if(!into[i]) q.push(i); } int c = 1; while(!q.empty()){ int tmp = q.top(); q.pop(); if(c != 1){///打印路径 cout << " " << tmp; } else cout << tmp; c++; for(int i=1; i<=n; i++){ if(!map[tmp][i]) continue; into[i]--; if(!into[i]) q.push(i); } } } int main(){ // freopen("in.txt", "r", stdin); while(scanf("%d%d",&n,&m) != EOF){ memset(map, 0, sizeof(map)); memset(into, 0, sizeof(into)); for(int i=0; i<m; i++){ int a,b; scanf("%d%d",&a,&b); if(!map[a][b]){///防止有重边 map[a][b] = 1; into[b]++; } } toposort(); cout << endl; } return 0; } </span>
相关文章推荐
- hdu 【1285】 确定比赛名次
- hdu 1285 确定比赛名次(拓扑排序)
- hdu 1285 确定比赛名次 (优先队列+拓扑排序)
- hdu 1285 确定比赛名次 ( 拓扑排序 )
- hdu 1285 确定比赛名次
- hdu 1285 确定比赛名次 (拓扑)
- 【hdu - 1285 确定比赛名次】
- hdu 1285 确定比赛名次_最简单的拓扑排序
- 确定比赛名次(hdu-1285)(拓扑排序队列实现)
- HDU 1285-确定比赛名次(裸拓扑||拓扑+前向星)
- HDU_1285 确定比赛名次
- hdu 1285 确定比赛名次 拓扑排序
- HDU 1285——确定比赛名次(拓扑排序入门)
- HDU 1285 确定比赛名次(简单拓扑排序)
- hdu-1285-确定比赛名次
- HDU - 1285 确定比赛名次
- HDU 1285-确定比赛名次(拓扑排序)
- hdu 1285 确定比赛名次
- hdu 1285 确定比赛名次
- HDU 1285 确定比赛名次