HDU 5695 Gym Class (topological sorting, greedy, priority queue)

Gym Class

Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2887    Accepted Submission(s): 1084

Problem Description
As we all know, the degree of bears like all kinds of sports activities.
Today, it finally became a physical education teacher dream. The first class, which found an interesting thing. Before class, all the students to be in a row, began to assume that most everyone has a unique ID, from 1 to N , after lined up, each student will find the smallest ID including himself in front of all the students , as their evaluation of this class score. The trouble is that some students do not want a (some) students came in his (her) in front of, in the case of satisfying this premise, budding physical education teacher - degree of Bear, hope the final result may be that all line up evaluation scores and students maximum.
 
input
A first line integer T , represents T ( . 1 T 30 ) sets of data.
For each test, the first line of the input two integers N and M ( . 1 N 100000 , 0 M 100000 ) , respectively, represent the total number of students and certain preferences.
Next M rows, each row two integers A and B ( . 1 A , B N ) , as represented by ID A students undesirable ID is B students came in his (her) before. You can think of topics to ensure that there is at least one arrangement is to meet all requirements.
 
Output
For each test, the maximum output score.
 
Sample Input
3 1 0 2 1 1 2 3 1 3 1
 
Sample Output
1 2 6
 

Topic analysis

This question and much like conventional topological sorting, individuals are N, M relationship, is different, and needs to score maximum

Put aside the topological sorting aside, for this integral, natural is the large ID row forward as much as possible of this greedy method is most appropriate

In greedy but also to meet on the basis of topological sorting, naturally it is easy to think of a priority queue, each time a team is the largest point of the ID 0

Note that there are two pits, one is the other input and output timeout longlong

#include<bits/stdc++.h>
using namespace std;

long long  T,x,y,n,m,minn,anss,into[100005];
vector<int>mp[100005];
priority_queue<int,vector<int>>q;

void bfs()
{
    while(!q.empty())
    {
        int now=q.top();
        q.pop();
        if(minn>now)
        {
            minn=now;
        }
        anss+=minn;
        for(int i=0;i<mp[now].size();i++)
        {
            int next=mp[now][i];
            into[next]--;
            if(into[next]==0)
            {
                q.push(next);
            }
        }
    }
}

int main ()
{
    ios::sync_with_stdio(false); //不写会TLE 
    cin>>T;
    while(T--)
    {
        memset(mp,0,sizeof(mp));
        memset(into,0,sizeof(into));
        anss = and 0 ;
        minn = 0x3f3f3f3f ;
        cin>>n>>m;
        while(m--)
        {
            cin>>x>>y;
            mp[x].push_back(y);
            into [and] ++ ;
        }
        for(int i=1;i<=n;i++)
        {
            if(into[i]==0)
            {
                q.push(i);
            }
        }
        bfs();
        cout<<anss<<endl;
    }
}

 

Guess you like

Origin www.cnblogs.com/dyhaohaoxuexi/p/12405318.html