HDU 3072 Intelligence System(强连通+最小树形图)

HDU 3072 Intelligence System

题目链接

题意:给定有向图,边有权值,求保留一些边,从一点出发,能传递到其他所有点的最小代价,保证有解

思路:先缩点,然后从入度为0的点作为起点(因为题目保证有解,所以必然有一个且只有一个入度为0的点),然后做一下最小树形图即可

代码:

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

const int MAXNODE = 50005;
const int MAXEDGE = 100005;

typedef int Type;
const Type INF = 0x3f3f3f3f;

struct Edge {
	int u, v;
	Type dist;
	Edge() {}
	Edge(int u, int v, Type dist) {
		this->u = u;
		this->v = v;
		this->dist = dist;
	}
};

struct Directed_MST {
	int n, m;
	Edge edges[MAXEDGE];
	int vis[MAXNODE];
	int pre[MAXNODE];
	int id[MAXNODE];
	Type in[MAXNODE];

	void init(int n) {
		this->n = n;
		m = 0;
	}

	void add_Edge(int u, int v, Type dist) {
		edges[m++] = Edge(u, v, dist);
	}

	void add_Edge(Edge e) {
		edges[m++] = e;
	}

	Type dir_mst(int root) {
		Type ans = 0;
		while (true) {
			for (int i = 0; i < n; i++) in[i] = INF;
			for (int i = 0; i < m; i++) { //find min edge
				int u = edges[i].u;
				int v = edges[i].v;
				if (edges[i].dist < in[v] && u != v) {
					in[v] = edges[i].dist;
					pre[v] = u;
				}
			}

			for (int i = 0; i < n; i++) { //judge
				if (i == root) continue;
				if (in[i] == INF) return -1;
			}

			int cnt = 0;
			memset(id, -1, sizeof(id));
			memset(vis, -1, sizeof(vis));
			in[root] = 0;

			for (int i = 0; i < n; i++) { //find circle
				ans += in[i];
				int v = i;
				while (vis[v] != i && id[v] == -1 && v != root) {
					vis[v] = i;
					v = pre[v];
				}
				if (v != root && id[v] == -1) {
					for (int u = pre[v]; u != v; u = pre[u])
						id[u] = cnt;
					id[v] = cnt++;
				}
			}
			if (cnt == 0) break;
			for (int i = 0; i < n; i++)
				if (id[i] == -1) id[i] = cnt++;
			for (int i = 0; i < m; i++) {
				int v = edges[i].v;
				edges[i].u = id[edges[i].u];
				edges[i].v = id[edges[i].v];
				if (edges[i].u != edges[i].v)
					edges[i].dist -= in[v];
			}
			n = cnt;
			root = id[root];
		}
		return ans;
	}
} gao;

const int N = 50005;

vector<Edge> g[N];
int n, m;
int pre[N], dfn[N], sccno[N], sccn, dfs_clock;
stack<int> S;

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

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

int in[N];

int main() {
	while (~scanf("%d%d", &n, &m)) {
		for (int i = 0; i < n; i++) g[i].clear();
		int u, v, val;
		while (m--) {
			scanf("%d%d%d", &u, &v, &val);
			g[u].push_back(Edge(u, v, val));
		}
		find_scc();
		gao.init(sccn);
		memset(in, 0, sizeof(in));
		for (int u = 0; u < n; u++) {
			for (int j = 0; j < g[u].size(); j++) {
				int v = g[u][j].v;
				if (sccno[u] == sccno[v]) continue;
				in[sccno[v]]++;
				gao.add_Edge(sccno[u] - 1, sccno[v] - 1, g[u][j].dist);
			}
		}
		for (int i = 1; i <= sccn; i++) {
			if (!in[i]) {
				printf("%d\n", gao.dir_mst(i - 1));
				break;
			}
		}
	}
	return 0;
}
时间: 2024-10-10 13:46:30

HDU 3072 Intelligence System(强连通+最小树形图)的相关文章

HDU 3072 Intelligence System (强连通分量)

题目地址:HDU 3072 这题一开始理解错题目意思了..(不得不吐槽一下题目意思确实不好理解..)用的强连通+最小生成树做的...然后错了好多遍...sad..题目意思是从一个给定点向其他所有点通信的最少花费,那么入度为0的点肯定是那个给定点.然后对于其它点,枚举就好了.找一个到他花费最少的点的花费. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue&g

HDU - 3072 Intelligence System(强连通分量+类最小生成树)

题目大意:有一个人想要将消息告诉给所有人(在同一个强连通分量里面的人可以相互转告,费用为0),问所有人都知道消息的最小花费是多少 解题思路:求出所有的强连通分量,然后将其缩点,桥就是连接其中的边 因为是张连通图,所以只要求出每个强连通分量被通知的最小价值,然后累加即可 刚开始以为可以用最小生成树,但发现错了,假设求出了三个强连通分量了,分别标号为1,2,3 再给出桥 1-2,权值1,1-3权值5,3-2权值4 按最小生成树的话,得到的结果是5,然而并不是这样的,因为是有向边,所以3这个点是不会被

[tarjan+最小树形图] hdu 3072 Intelligence System

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3072 Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1479    Accepted Submission(s): 653 Problem Description After a day, ALP

hdu 3072 Intelligence System

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2909    Accepted Submission(s): 1259 Problem Description After a day, ALPCs finally complete their ultimate intelligence system, the purpose of it

HDU 3072 Intelligence System (强连通+(贪心||树形图))

大意:在一个联通分量里面的边权值忽略不计,求缩点后的所有联通分量链接在一起的最小权值和. 思路:必然先缩点,最后得到的图为DAG,然后就是统计权值,对于那么到达当前的缩点可能有多个入度,所以选择最小的即可.最后避开起始点的dis[star] == inf.累加即可. #include<map> #include<queue> #include<cmath> #include<cstdio> #include<stack> #include<

HDU——T 3072 Intelligence System

http://acm.hdu.edu.cn/showproblem.php?pid=3072 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2909    Accepted Submission(s): 1259 Problem Description After a day, ALPCs finally complete their

hdoj 3072 Intelligence System【求scc&amp;&amp;缩点】【求连通所有scc的最小花费】

Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1904    Accepted Submission(s): 824 Problem Description After a day, ALPCs finally complete their ultimate intelligence system

hdu3072 强连通+最小树形图

题意:有一个人他要把一个消息通知到所有人,已知一些通知关系:A 能通知 B,需要花费 v,而又知道,如果某一个小团体,其中的成员相互都能直接或间接通知到,那么他们之间的消息传递是不需要花费的,现在问这个人将消息传给所有人所需的最小花费. 首先,一个团体中能够相互通知其实就是一个强连通分量,所以首先找出所有强连通分量,因为内部不需要花费,所以他们就相当于一个点,而早缩点之后,我们就得到了一张有向无环图,消息传递的最小花费其实就是最小树形图的边权和.刚做这个题的时候我还只是看见过最小树形图是什么,做

Hdu 4009 Transfer water【最小树形图】

Transfer water Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 4702 Accepted Submission(s): 1665 Problem Description XiaoA lives in a village. Last year flood rained the village. So they decide to