HDU 1827 Summer Holiday(强连通)

HDU 1827 Summer Holiday

题目链接

题意:中文题

思路:强连通缩点,每个点的权值为强连通中最小值,然后入度为0的点就是答案

代码:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;

const int N = 1005;
const int INF = 0x3f3f3f3f;

int n, m, val[N];
vector<int> g[N];
int pre[N], lowlink[N], dfs_clock, sccno[N], scc_cnt, scc_val[N];
stack<int> S;

void dfs(int u) {
	pre[u] = lowlink[u] = ++dfs_clock;
	S.push(u);
	for (int i = 0; i < g[u].size(); i++) {
		int v = g[u][i];
		if (!pre[v]) {
			dfs(v);
			lowlink[u] = min(lowlink[u], lowlink[v]);
		} else if (!sccno[v]) lowlink[u] = min(lowlink[u], pre[v]);
	}
	if (pre[u] == lowlink[u]) {
		scc_cnt++;
		scc_val[scc_cnt] = INF;
		while (1) {
			int x = S.top(); S.pop();
			scc_val[scc_cnt] = min(scc_val[scc_cnt], val[x]);
			sccno[x] = scc_cnt;
			if (x == u) break;
		}
	}
}

void find_scc() {
	dfs_clock = scc_cnt = 0;
	memset(pre, 0, sizeof(pre));
	memset(sccno, 0, sizeof(sccno));
	for (int i = 1; i <= n; i++)
		if (!pre[i]) dfs(i);
}

int in[N];

int main() {
	while (~scanf("%d%d", &n, &m)) {
		for (int i = 1; i <= n; i++) {
			g[i].clear();
			scanf("%d", &val[i]);
		}
		int u, v;
		while (m--) {
			scanf("%d%d", &u, &v);
			g[u].push_back(v);
		}
		find_scc();
		memset(in, 0, sizeof(in));
		for (int u = 1; u <= n; u++) {
			for (int j = 0; j < g[u].size(); j++) {
				int v = g[u][j];
				if (sccno[u] != sccno[v])
					in[sccno[v]]++;
			}
		}
		int ans = 0, cnt = 0;
		for (int i = 1; i <= scc_cnt; i++)
			if (!in[i]) {
				ans += scc_val[i];
				cnt++;
			}
		printf("%d %d\n", cnt, ans);
	}
	return 0;
}
时间: 2024-08-06 07:51:51

HDU 1827 Summer Holiday(强连通)的相关文章

hdu - 1827 Summer Holiday (强连通)

http://acm.hdu.edu.cn/showproblem.php?pid=1827 缩点后,统计入度为0的点有多少个,那么这些点都是需要被通知的,但是这些点可能也是被缩的,所以每次在这个点所属集合找一个最小值即可. 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <vector> 5 #include <cstring> 6 #include

HDU - 1827 Summer Holiday(强连通分量+贪心)

题目大意:To see a World in a Grain of Sand And a Heaven in a Wild Flower, Hold Infinity in the palm of your hand And Eternity in an hour. ―― William Blake 听说lcy帮大家预定了新马泰7日游,Wiskey真是高兴的夜不能寐啊,他想着得快点把这消息告诉大家,虽然他手上有所有人的联系方式,但是一个一个联系过去实在太耗时间和电话费了.他知道其他人也有一些别人

HDU 1827 Summer Holiday (强连通分量)

题目地址:HDU 1827 先缩点,缩完点后,找出入度为0的块就是需要传递的块.然后用块中花费最少的来当代表块中的花费.累加起来就行了. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #includ

hdu 1827 Summer Holiday tarjan+缩点

题意:http://acm.hdu.edu.cn/showproblem.php?pid=1827 Summer Holiday Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2050    Accepted Submission(s): 939 Problem Description To see a World in a Gra

HDU 1827 Summer Holiday(Tarjan缩点)

Problem Description To see a World in a Grain of Sand And a Heaven in a Wild Flower, Hold Infinity in the palm of your hand And Eternity in an hour. -- William Blake 听说lcy帮大家预定了新马泰7日游,Wiskey真是高兴的夜不能寐啊,他想着得快点把这消息告诉大家,虽然他手上有所有人的联系方式,但是一个一个联系过去实在太耗时间和电话

HDU 1827

 HDU - 1827 Summer Holiday Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u To see a World in a Grain of Sand And a Heaven in a Wild Flower, Hold Infinity in the palm of your hand And Eternity in an hour. ―― William Blake

HDU 1827:Summer Holiday(强连通)

http://acm.hdu.edu.cn/showproblem.php?pid=1827 思路:强连通分量缩点后找入度为0的点,然后对于属于该强连通分量的找一个最小耗费的入口. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <queue> 6 #include <cmath> 7 #i

[tarjan] 1827 Summer Holiday

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1827 Summer Holiday Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1721    Accepted Submission(s): 797 Problem Description To see a World in a

HDU 3639 Hawk-and-Chicken (强连通分量+树形DP)

题目地址:HDU 3639 先用强连通分量缩点,缩点之后,再重新按缩点之后的块逆序构图,每个块的值是里边缩的点的个数,那么得到选票的最大的一定是重新构图后入度为0的块,然后求出来找最大值即可. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h>