杭电1285——确定比赛名次
2015-01-23 21:38
176 查看
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285
这是写的第一个拓扑排序题目,算是水题吧,能过就好。
但还是错了几次:刚开始不知道如何处理输出的顺序问题,原来改用优先队列(priority_queue)就行了,也是百度才知道输入可能存在重复的边。
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
struct node{
int n;
bool operator <(const node &a) const{
return a.n<n;
}
}o;
using namespace std;
int N,M,map[550][550],du[550];
void top_order(){
priority_queue<node> q;
for(int i=1;i<=N;i++)
if(!du[i]) {
o.n=i;
q.push(o);
}
while(!q.empty())
{
int tmp=q.top().n;
q.pop();
cout<<tmp;
for(int i=1;i<=N;i++)
if(map[tmp][i])
{
du[i]--;
if(!du[i]){
o.n=i;
q.push(o);
}
}
if(!q.empty()) cout<<" ";
else cout<<endl;
}
}
int main(){
while(cin>>N)
{
cin>>M;
if(!N && !M) break;
memset(map,0,sizeof(map));
memset(du,0,sizeof(du));
for(int i=0;i<M;i++)
{
int tx,ty;
cin>>tx>>ty;
if(!map[tx][ty]){
map[tx][ty]=1;
du[ty]++;
}
}
top_order();
}
return 0;
}
这是写的第一个拓扑排序题目,算是水题吧,能过就好。
但还是错了几次:刚开始不知道如何处理输出的顺序问题,原来改用优先队列(priority_queue)就行了,也是百度才知道输入可能存在重复的边。
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
struct node{
int n;
bool operator <(const node &a) const{
return a.n<n;
}
}o;
using namespace std;
int N,M,map[550][550],du[550];
void top_order(){
priority_queue<node> q;
for(int i=1;i<=N;i++)
if(!du[i]) {
o.n=i;
q.push(o);
}
while(!q.empty())
{
int tmp=q.top().n;
q.pop();
cout<<tmp;
for(int i=1;i<=N;i++)
if(map[tmp][i])
{
du[i]--;
if(!du[i]){
o.n=i;
q.push(o);
}
}
if(!q.empty()) cout<<" ";
else cout<<endl;
}
}
int main(){
while(cin>>N)
{
cin>>M;
if(!N && !M) break;
memset(map,0,sizeof(map));
memset(du,0,sizeof(du));
for(int i=0;i<M;i++)
{
int tx,ty;
cin>>tx>>ty;
if(!map[tx][ty]){
map[tx][ty]=1;
du[ty]++;
}
}
top_order();
}
return 0;
}
相关文章推荐
- 【杭电1285】确定比赛名次
- 杭电 1285 确定比赛名次(拓扑排序)
- 杭电ACM 1285 确定比赛名次(拓扑排序)
- 杭电 1285 确定比赛名次(拓扑排序)
- 杭电-1285确定比赛名次(topo)
- 杭电 1285 确定比赛名次(拓扑排序)
- 拓扑排序杭电 1285确定比赛名次
- 杭电1285确定比赛名次
- 杭电-1285 确定比赛名次(模板)
- 杭电1285确定比赛名次
- 杭电 1285 确定比赛名次
- 杭电1285确定比赛名次
- 杭电 1285 确定比赛名次 【拓扑排序】
- 【杭电】[1285]确定比赛名次
- 杭电1285 确定比赛名次(拓扑排序)
- 杭电 1285 确定比赛名次
- 杭电acm oj 1285 确定比赛名次
- 杭电 1285 确定比赛名次
- hdu 1285 确定比赛名次
- HDU-1285 确定比赛名次