题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5695
#include <bits/stdc++.h>
#define sf scanf
#define pf printf
typedef long long LL;
using namespace std;
const int maxn = 100000 + 5,INF = 2e9;
vector<int> _ADJ[maxn];
int ins[maxn],pos[maxn];
priority_queue<int> Q;
int main(){
int T;sf("%d",&T);
while( T-- ){
int n,m;sf("%d %d",&n,&m);
for(int i = 1;i <= n;++i) _ADJ[i].clear(),ins[i] = 0;
for(int i = 0;i < m;++i){
int u,v;sf("%d %d",&u,&v);
_ADJ[u].push_back(v);ins[v]++;
}
for(int i = 1;i <= n;++i)
if(ins[i] == 0) Q.push(i);
int MIN = INF;
LL ret = 0;
for(int i = 1;i <= n;++i){
pos[i] = Q.top();Q.pop();
MIN = min(MIN,pos[i]);
ret = ret + MIN;
int len = _ADJ[pos[i]].size();
for(int j = 0;j < len;++j){
ins[_ADJ[pos[i]][j]]--;
if(ins[_ADJ[pos[i]][j]] == 0) Q.push(_ADJ[pos[i]][j]);
}
}
pf("%lld\n",ret);
}
}