题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1827
12 16 2 2 2 2 2 2 2 2 2 2 2 2 1 3 3 2 2 1 3 4 2 4 3 5 5 4 4 6 6 4 7 4 7 12 7 8 8 7 8 9 10 9 11 10
3 6
中文题目,就不解释了。
解题思路:
tarjan求强连通分量,然后求出入度为0的强联通分量,取出他们中打电话花费最少的,加起来就是结果。
代码:
//#include<CSpreadSheet.h> #include<iostream> #include<cmath> #include<cstdio> #include<sstream> #include<cstdlib> #include<string> #include<string.h> #include<cstring> #include<algorithm> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<ctime> #include<bitset> #include<cmath> #define eps 1e-6 #define INF 0x3f3f3f3f #define PI acos(-1.0) #define ll __int64 #define LL long long #define lson l,m,(rt<<1) #define rson m+1,r,(rt<<1)|1 #define M 1000000007 //#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; #define Maxn 1100 int low[Maxn],dfn[Maxn],sta[Maxn],dep,bc,sc,n,m; int sa[Maxn],in[Maxn],dei[Maxn]; bool iss[Maxn]; vector<vector<int> >myv; vector<vector<int> >bb; void tarjan(int cur) { int ne; low[cur]=dfn[cur]=++dep; sta[++sc]=cur; iss[cur]=true; for(int i=0;i<myv[cur].size();i++) { ne=myv[cur][i]; if(!dfn[ne]) { tarjan(ne); if(low[ne]<low[cur]) low[cur]=low[ne]; } else if(iss[ne]&&dfn[ne]<low[cur]) low[cur]=dfn[ne]; } if(low[cur]==dfn[cur]) { ++bc; do { ne=sta[sc--]; iss[ne]=false; in[ne]=bc; bb[bc].push_back(sa[ne]); }while(ne!=cur); } } void solve() { sc=bc=dep=0; bb.clear(); bb.resize(n+1); memset(iss,false,sizeof(iss)); memset(dfn,0,sizeof(dfn)); for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i); } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while(~scanf("%d%d",&n,&m)) { myv.clear(); myv.resize(n+1); for(int i=1;i<=n;i++) scanf("%d",&sa[i]); for(int i=1;i<=m;i++) { int a,b; scanf("%d%d",&a,&b); myv[a].push_back(b); } solve(); memset(dei,0,sizeof(dei)); for(int i=1;i<=n;i++) { for(int j=0;j<myv[i].size();j++) { int ne=myv[i][j]; if(in[ne]!=in[i]) dei[in[ne]]++; } } int nu=0,ans=0; for(int i=1;i<=bc;i++) { if(!dei[i]) { sort(bb[i].begin(),bb[i].end()); ans+=bb[i][0]; nu++; } } printf("%d %d\n",nu,ans); } return 0; }