题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5695
题意:中文题,不解释
题解:逆向拓扑字典序就行
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<string> 3 #include<set> 4 #include<vector> 5 #include<queue> 6 #include<functional> 7 using namespace std; 8 #define MAX 100010 9 int InDeg[MAX]; 10 int n, m, Count; 11 int Ans[MAX]; 12 vector<int> v[MAX]; 13 void TopoSort() 14 { 15 priority_queue<int> PQ; 16 for(int i=n; i>=1; i--) //找出入度为0的点并放入优先队列 17 if(InDeg[i]==0) PQ.push(i); 18 19 while(!PQ.empty()) //BFS 20 { 21 int Tmp = PQ.top(); 22 PQ.pop(); 23 Ans[Count++] = Tmp; 24 for(int i=0; i<v[Tmp].size(); i++) 25 { 26 InDeg[v[Tmp][i]]--; 27 if(InDeg[v[Tmp][i]]==0) PQ.push(v[Tmp][i]); 28 } 29 } 30 } 31 32 int main() 33 { 34 int x, y,t; 35 scanf("%d",&t); 36 while(t--) 37 { 38 scanf("%d%d",&n,&m); 39 40 for(int i=0;i<=n;i++)v[i].clear(),InDeg[i]=0; 41 Count = 0; 42 for(int i=0; i<m; i++) 43 { 44 scanf("%d%d",&x,&y); 45 InDeg[y]++; 46 v[x].push_back(y); 47 } 48 TopoSort(); 49 int min=1000000000; 50 long long an=0; 51 for(int i=0; i<Count; i++){ 52 min=min>Ans[i]?Ans[i]:min; 53 an+=min; 54 } 55 printf("%I64d ",an); 56 } 57 }