codeforces427c-Checkposts
来源:互联网 发布:南京软件谷管委会 编辑:程序博客网 时间:2024/06/04 00:37
Description
Your city has n junctions. There are mone-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions.
To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junctioni can protect junction j if either i = j or the police patrol car can go toj from i and then come back toi.
Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions.
You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price andin addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other.
Input
In the first line, you will be given an integer n, number of junctions(1 ≤ n ≤ 105). In the next line,n space-separated integers will be given. Theith integer is the cost of building checkpost at theith junction (costs will be non-negative and will not exceed109).
The next line will contain an integer m (0 ≤ m ≤ 3·105). And each of the nextm lines contains two integers ui and vi (1 ≤ ui, vi ≤ n; u ≠ v). A pairui, vi means, that there is a one-way road which goes fromui tovi. There will not be more than one road between two nodes in the same direction.
Output
Print two integers separated by spaces. The first one is the minimum possible money needed to ensure the security of all the junctions. And the second one is the number of ways you can ensure the security modulo1000000007(109 + 7).
Sample Input
31 2 331 22 33 2
3 1
52 8 0 6 061 41 32 43 44 55 1
8 2
101 3 2 2 1 3 1 4 10 10121 22 33 13 44 55 65 76 47 38 99 1010 9
15 6
27 9121 22 1
7 1
还是强连通的老套路,主要是这里要求多种方案。
以价钱和检查点的数量最小优先,实际上就是在每个强连通分量中找到最便宜的那个检查点,计算以最便宜的检查点的价格一共有几个检查点符合。
最后逐个强连通分量的最便宜检查点数量相乘即可。
而求价格更简单,把每个强连通分量的最便宜价格加起来就好。
注意:价格之和会爆int,方案数量由于取余,不会爆。做题要注意。
#include <algorithm>#include <iostream>#include <sstream>#include <cstring>#include <cstdlib>#include <string>#include <vector>#include <cstdio>#include <stack>#include <cmath>#include <queue>#include <map>#include <set>using namespace std;#define N 100005vector<int> e[N];int dfn[N], low[N],stap[N], stop,belong[N],outdeg[N],dindex, cnt,minn[N],num[N],val[N];bool instack[N];int n, m;void tarjan(int x){ int y = 0; dfn[x]=low[x]=++dindex; instack[x]=true; stap[++stop]=x; for (int i=0; i<e[x].size(); i++) { y=e[x][i]; if (!dfn[y]) { tarjan(y); if (low[y]<low[x]) { low[x]=low[y]; } } else if (instack[y]&&dfn[y]<low[x]){ low[x]=dfn[y]; } } if (dfn[x]==low[x]) { cnt++; while (y!=x) { y=stap[stop--]; instack[y]=false; belong[y]=cnt; } }}void solve(){ stop=dindex=cnt=0; memset(dfn, 0, sizeof(dfn)); memset(instack, 0, sizeof(instack)); memset(outdeg, 0, sizeof(outdeg)); memset(minn, 0x3f, sizeof(minn)); for (int i=1; i<=n; i++) { if (!dfn[i]) { tarjan(i); } }}int main(){ int a,b; while (~scanf("%d", &n)) { for (int i=1; i<=n; ++i) { e[i].clear(); cin>>val[i]; } scanf("%d",&m); for (int i=1; i<=m; i++) { scanf("%d%d",&a,&b); e[a].push_back(b); } solve(); for (int i=1; i<=cnt; i++) { num[i]=1; } long long sum=0; for (int i=1; i<=n; i++) { int t=belong[i]; if (val[i]<minn[t]) { num[t]=1; minn[t]=val[i]; } else if (val[i]==minn[t]) { num[t]++; } } long long kind=1; for (int i=1; i<=cnt; i++) { sum+=minn[i]; kind*=num[i]; kind%=1000000007; } cout<<sum<<" "<<kind<<endl; } return 0;}
- codeforces427c-Checkposts
- codeforces244C. Checkposts
- codeforces 427C Checkposts
- codeforces 427C Checkposts
- Codeforces 427 C. Checkposts
- CF 427C Checkposts Tarjan
- CF 427C. Checkposts Strongly connected components
- cf 244c Checkposts 强连通分量
- codeforces 472C Checkposts 强连通
- Codeforces#427C Checkposts(强联通Tarjan)
- CF427C Checkposts(tarjan求强连通)
- Codeforces Round #244 (Div. 2)——Checkposts
- CF:Problem 427C - Checkposts强连通Tarjan算法
- codeforces#244_div2_C Checkposts 强连通分量,Tarjan
- CF:Problem 427C - Checkposts强连通 Tarjan算法
- Checkposts (codeforces #244dv2) 强连通分量 tarjan算法
- codeforces #427C Checkposts(强连通缩点)
- Codeforces 427C Checkposts (强连通分量 tarjan模板)
- 38Count and Say
- IPSec VPN
- 杭电 2066 一个人的旅行(dijkstra)
- 【面向对象】C#中的面向对象
- 泊松分布和指数分布:10分钟教程
- codeforces427c-Checkposts
- Linux命令安装Apache
- 类(继承)的内存分配详解
- 第7章 初步认识JavaBean
- 求一个数组的最大k个数(java)
- The Pilots Brothers' refrigerator(POJ_2965)
- COCOS2D-X 多分辨率适配完全解析
- hdoj 1596 find the safest road
- 黑马程序员_GUI