您的位置:首页 > 其它

确定比赛名次(拓扑排序 +有限对列)

2016-07-24 00:00 162 查看
/**
优先队列加拓扑排序
*/

include

include

include

include

using namespace std;
vector edge[500*500];
int du[550];
void creatLinJieBiao(int x,int y)
{

edge[x].push_back(y);
du[y]++;

}
struct myComp///不是结构体时 定义优先队列的方法
{
bool operator ()(int a,int b)///小的优先出队
{
return a>b;
}
};
void topSort(int n)
{
priority_queue ,myComp>q;///不是结构体的优先队列
for(int i=1; i<=n; i++)
{
if(du[i]==0)
q.push(i);
}
int op=0;
while(!q.empty())
{

int a=q.top();
q.pop();
if(op==0)
printf("%d",a);
else printf(" %d",a);
op=1;
for(int i=0; i<edge[a].size(); i++)
{
du[edge[a][i]]--;
if(du[edge[a][i]]==0)
{
q.push(edge[a][i]);
}
}
}
printf("\n");

}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(du,0,sizeof(du));
memset(edge,0,sizeof(edge));///注意初始化
for(int i=1; i<=m; i++)
{
int x,y;
scanf("%d%d",&x,&y);
creatLinJieBiao(x,y);
}
topSort(n);
}
return 0;
}

/**
5 5

3 4
1 5 2 3
5 4 1 2
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: