码迷,mamicode.com
首页 > 其他好文 > 详细

[HDOJ1827]Summer Holiday(强连通分量,缩点)

时间:2016-05-27 11:10:27      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1827

缩点后统计入度和当前强连通分量中最小花费,然后记录入度为0的点的个数和花费和就行了。

  1 /*
  2 ━━━━━┒ギリギリ♂ eye!
  3 ┓┏┓┏┓┃キリキリ♂ mind!
  4 ┛┗┛┗┛┃\○/
  5 ┓┏┓┏┓┃ /
  6 ┛┗┛┗┛┃ノ)
  7 ┓┏┓┏┓┃
  8 ┛┗┛┗┛┃
  9 ┓┏┓┏┓┃
 10 ┛┗┛┗┛┃
 11 ┓┏┓┏┓┃
 12 ┛┗┛┗┛┃
 13 ┓┏┓┏┓┃
 14 ┃┃┃┃┃┃
 15 ┻┻┻┻┻┻
 16 */
 17 #include <algorithm>
 18 #include <iostream>
 19 #include <iomanip>
 20 #include <cstring>
 21 #include <climits>
 22 #include <complex>
 23 #include <fstream>
 24 #include <cassert>
 25 #include <cstdio>
 26 #include <bitset>
 27 #include <vector>
 28 #include <deque>
 29 #include <queue>
 30 #include <stack>
 31 #include <ctime>
 32 #include <set>
 33 #include <map>
 34 #include <cmath>
 35 using namespace std;
 36 #define fr first
 37 #define sc second
 38 #define cl clear
 39 #define BUG puts("here!!!")
 40 #define W(a) while(a--)
 41 #define pb(a) push_back(a)
 42 #define Rint(a) scanf("%d", &a)
 43 #define Rll(a) scanf("%I64d", &a)
 44 #define Rs(a) scanf("%s", a)
 45 #define Cin(a) cin >> a
 46 #define FRead() freopen("in", "r", stdin)
 47 #define FWrite() freopen("out", "w", stdout)
 48 #define Rep(i, len) for(int i = 0; i < (len); i++)
 49 #define For(i, a, len) for(int i = (a); i < (len); i++)
 50 #define Cls(a) memset((a), 0, sizeof(a))
 51 #define Clr(a, x) memset((a), (x), sizeof(a))
 52 #define Full(a) memset((a), 0x7f7f, sizeof(a))
 53 #define pi 3.14159265359
 54 #define RT return
 55 #define lowbit(x) x & (-x)
 56 #define onenum(x) __builtin_popcount(x)
 57 typedef long long LL;
 58 typedef long double LD;
 59 typedef unsigned long long ULL;
 60 typedef pair<int, int> pii;
 61 typedef pair<string, int> psi;
 62 typedef map<string, int> msi;
 63 typedef vector<int> vi;
 64 typedef vector<LL> vl;
 65 typedef vector<vl> vvl;
 66 typedef vector<bool> vb;
 67 
 68 const int maxn = 1010;
 69 const int maxm = 2020;
 70 const int inf = 0x7f7f7f;
 71 int n, m;
 72 vector<int> G[maxn];
 73 int vis[maxn][maxn];
 74 int w[maxn];
 75 int dfn[maxn], low[maxn], idx;
 76 int st[maxn], top;
 77 int belong[maxn], bcnt;
 78 int in[maxn], out[maxn];
 79 int cost[maxn];
 80 
 81 void tarjan(int u) {
 82     int v = u;
 83     dfn[u] = low[u] = ++idx;
 84     vis[0][u] = 1; st[++top] = u;
 85     Rep(i, G[u].size()) {
 86         v = G[u][i];
 87         if(!dfn[v]) {
 88             tarjan(v);
 89             low[u] = min(low[u], low[v]);
 90         }
 91         else if(vis[0][v]) low[u] = min(low[u], dfn[v]);
 92     }
 93     if(low[u] == dfn[u]) {
 94         bcnt++;
 95         do {
 96             v = st[top--];
 97             vis[0][v] = 0;
 98             belong[v] = bcnt;
 99         } while(u != v);
100     }
101 }
102 
103 int main() {
104     // FRead();
105     int u, v;
106     while(~Rint(n) && ~Rint(m)) {
107         Cls(dfn); Cls(low); Cls(w);
108         Cls(st); Cls(vis); top = 0;
109         Cls(belong); bcnt = 0; idx = 0;
110         Cls(in); Cls(out);
111         For(i, 1, n+1) {
112             Rint(w[i]);
113             G[i].cl();
114             cost[i] = inf;
115         }
116         Rep(i, m) {
117             Rint(u); Rint(v);
118             G[u].pb(v);
119         }
120         For(i, 1, n+1) if(!dfn[i]) tarjan(i);
121         Cls(vis);
122         For(u, 1, n+1) {
123             cost[belong[u]] = min(cost[belong[u]], w[u]);
124             Rep(i, G[u].size()) {
125                 int v = G[u][i];
126                 if(belong[u] == belong[v]) continue;
127                 in[belong[v]]++;
128                 out[belong[u]]++;
129             }
130         }
131         int ret1 = 0, ret2 = 0;
132         For(i, 1, bcnt+1) {
133             if(in[i] == 0) {
134                 ret1++;
135                 ret2 += cost[i];
136             }
137         }
138         printf("%d %d\n", ret1, ret2);
139     }
140     RT 0;
141 }

 

[HDOJ1827]Summer Holiday(强连通分量,缩点)

标签:

原文地址:http://www.cnblogs.com/vincentX/p/5533761.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!