标签:
Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 768 Accepted Submission(s): 309
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<string>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
#pragma comment(linker, "/STACK:102400000,102400000")
const int maxn = 1e5+300;
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef unsigned long long ULL;
priority_queue<int>PQ;
queue<int>Q;
int ind[maxn];
vector<int> G[maxn];
int main(){
int T, n, m;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
int u, v;
for(int i = 1; i <= n; i++){
G[i].clear();
}
memset(ind,0,sizeof(ind));
for(int i = 1; i <= m; i++){
scanf("%d%d",&u,&v);
ind[v]++;
G[u].push_back(v);
}
for(int i = 1; i <= n; i++){
if(ind[i] == 0){
PQ.push(i);
}
}
int Min = INF;
LL ans = 0;
while(!PQ.empty()){
int u = PQ.top(); PQ.pop();
Min = min(Min, u);
ans += Min;
for(int j = 0; j < G[u].size(); j++){
int &v = G[u][j];
ind[v]--;
if(ind[v] == 0){
PQ.push(v);
}
}
}
printf("%lld\n",ans);
}
return 0;
}
HDU 5695 ——Gym Class——————【贪心思想,拓扑排序】
标签:
原文地址:http://www.cnblogs.com/chengsheng/p/5527397.html