首页 > 代码库 > HDU 5695 Gym Class (拓扑排序)

HDU 5695 Gym Class (拓扑排序)

题意:略。

析:当时比赛真是脑残了。。。。一直想什么拓扑模板,其实并不需要么,当时脑子短路了。。。。完全可以直接根据题意写的,用优先队列

维护一下就好。最近没刷题真是落后了。。。。不说了,太水了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 100000 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}
vector<int> G[maxn];
int in[maxn];

int main(){
  int T;  cin >> T;
  while(T--){
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; ++i)  G[i].clear();
    memset(in, 0, sizeof in);
    for(int i = 0; i < m; ++i){
      int a, b;
      scanf("%d %d", &a, &b);
      G[a].push_back(b);
      ++in[b];
    }
    LL ans = 0;
    int mmin = INF;
    priority_queue<int> pq;
    for(int i = 1; i <= n; ++i)  if(!in[i])
      pq.push(i);

    while(!pq.empty()){
      int u = pq.top();  pq.pop();
      mmin = min(mmin, u);
      ans += mmin;
      for(int i = 0; i < G[u].size(); ++i){
        int v = G[u][i];  --in[v];
        if(!in[v])  pq.push(v);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

  

HDU 5695 Gym Class (拓扑排序)