拓扑排序

来源:互联网 发布:水果竞猜php源码 编辑:程序博客网 时间:2024/06/07 07:33

1424. 奖金

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

    由于无敌的凡凡在2005年世界英俊帅气男总决选中胜出,Yali Company总经理Mr.Z心情好,决定给每位员工发奖金。公司决定以每个人本年在公司的贡献为标准来计算他们得到奖金的多少。
    于是Mr.Z下令召开m方会谈。每位参加会谈的代表提出了自己的意见:“我认为员工a的奖金应该比b高!”Mr.Z决定要找出一种奖金方案,满足各位代表的意见,且同时使得总奖金数最少。每位员工奖金最少为100元。

【数据范围】
数据满足n<=10000,m<=20000。

Input

    第一行两个整数n,m,表示员工总数和代表数;
    以下m行,每行2个整数a,b,表示某个代表认为第a号员工奖金应该比第b号员工高。

Output

    若无法找到合法方案,则输出“Poor Xed”;否则输出一个数表示最少总奖金。

Sample Input

2 11 2

Sample Output

201

#include<iostream>#include<cstring> #include<vector> #include<queue>using namespace std;const int MAX=10001;int cost[MAX];//每个结点的工资int inDeg[MAX];//入度 vector<int> e[MAX];//用vector储存临界表 queue<int> q; int ans=0;int main(){int n,m,u,v;cin>>n>>m;memset(cost,0,sizeof(cost));      memset(inDeg,0,sizeof(inDeg));    int sorted=0;//已经成功拓扑排序的数目     while(m--){    cin>>u>>v;    e[v].push_back(u);//v->u建图     inDeg[u]++;//u的入度增加     }     for(int i=1;i<=n;i++){    if(inDeg[i]==0){//拓扑排序从入度为0的点开始      cost[i]=100;//入度为0的奖金全为100     q.push(i);    }    }    while(!q.empty()){    int top=q.front();    q.pop();    sorted++;//每从队列丢掉一个,则说明成功排序一个     ans+=cost[top];    for(int j=0;j<e[top].size();j++){    if((--inDeg[e[top][j]])==0)//如果入度-1后等于0,加入队列     q.push(e[top][j]);    cost[e[top][j]]=cost[top]+1;    }    }    if(sorted==n)    cout<<ans<<endl;    else        cout<<"Poor Xed"<<endl;return 0;}



0 0