写代码的时候太多的地方写错了,,改了很长时间,可以算是模板题
解法:入度为零的缩点必须打电话,,加上该缩点的最小值即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std ;
const int N = 1000 + 11 ;
const int M = 2000 + 11 ;
struct Graph {
struct Edge {
int e ;
int next ;
};
Edge err[M] ;
int head[N] ;
bool degreein[N] ;
int dfn[N] , low[N] , instack[N] ;
int minc[N] , cost[N] ;
vector<int> stk ;
vector<pair<int , int> > que ;
int idx , color , visit_time ;
int n , m ;
void addinfo() {
int a , b ;
for(int i = 1; i <= n; ++i){
scanf("%d" ,cost+i);
}
while(m--) {
scanf("%d%d",&a,&b) ;
if(a == b) continue ;
err[idx].e = b;
err[idx].next = head[a] ;
head[a] = idx++ ;
}
}
void init() {
memset(instack , 0 , sizeof(instack)) ;
memset(head , -1 , sizeof(head)) ;
memset(degreein , 0 , sizeof(degreein)) ;
memset(minc , 1<<6 , sizeof(minc)) ;
idx = 1 , color = 2 , visit_time = 0 ;
stk.clear() ;
que.clear() ;
}
void dfs(int site) {
stk.push_back(site);//这句居然忘了加,,,
instack[site] = 1 ;
dfn[site] = low[site] = ++ visit_time ;
for(int i = head[site] ; i != -1 ; i = err[i].next) {
int e = err[i].e ;//写代码时注意别写错了,,,,
if(instack[e] == 0) {
dfs(e) ;
low[site] = min(low[site] , low[e]) ;
}else if(instack[e] == 1) {
low[site] = min(low[site] , dfn[e]) ;
}
if(instack[e] > 1) {
que.push_back(make_pair(site , instack[e])) ;
}
}
if(low[site] == dfn[site]) {
int tmp ;
while(stk.back() != site) {
tmp = stk.back() ;
stk.pop_back() ;
instack[tmp] = color ;
minc[color] = min(minc[color] , cost[tmp]) ;
}
stk.pop_back() ;
minc[color] = min( minc[color] , cost[site]) ;
instack[site] = color++ ;
}
}
void std_fun(){
init() ;
addinfo() ;
for(int i = 1 ; i <= n ; ++i) {
if(instack[i] == 0) dfs(i) ;
}
while(!que.empty()) {
pair<int , int> tmp = que.back() ;//这你是back(),因为下面是pop_back()
que.pop_back() ;
if(instack[tmp.first] != tmp.second) {
degreein[tmp.second] = true ;
}
}
int s1 = 0 , s2 = 0 ;
for(int i = 2; i < color ; ++i) {
if(degreein[i] == false) {
++ s1 ;
s2 += minc[i] ;
}
}
printf("%d %d\n" , s1 , s2) ;
}
}g ;
int main() {
while(scanf("%d%d",&g.n ,&g.m)==2) {
g.std_fun() ;
}
}