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

HDU 4738 Caocao's Bridges(找割边)

时间:2014-08-30 00:03:48      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   ar   for   2014   

HDU 4738 Caocao‘s Bridges

题目链接

注意几个坑,可能重边,至少要派一个人去炸,没有连通的时候就不用炸了

代码:

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

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

int pre[N], low[N], dfs_clock;

int n, m;

struct Node {
	int v, val, id;
	Node() {}
	Node(int v, int val, int id) {
		this->v = v;
		this->val = val;
		this->id = id;
	}
};

vector<Node> g[N];
int ans;

int dfs(int u, int fa) {
	int lowu = pre[u] = ++dfs_clock;
	for (int i = 0; i < g[u].size(); i++) {
		int v = g[u][i].v;
		int id = g[u][i].id;
		if (id == fa) continue;
		if (!pre[v]) {
			int lowv = dfs(v, id);
			lowu = min(lowu, lowv);
			if (lowv > pre[u])
				ans = min(ans, g[u][i].val);
		} else lowu = min(lowu, pre[v]);
	}
	return low[u] = lowu;
}

int main() {
	while (~scanf("%d%d", &n, &m) && n) {
		int u, v, val;
		memset(pre, 0, sizeof(pre));
		dfs_clock = 0;
		for (int i = 0; i < n; i++)
			g[i].clear();
		for (int i = 0; i < m; i++) {
			scanf("%d%d%d", &u, &v, &val);
			u--; v--;
			g[u].push_back(Node(v, val, i));
			g[v].push_back(Node(u, val, i));
		}
		int cnt = 0;
		ans = INF;
		for (int i = 0; i < n; i++) {
			if (!pre[i]) {
				cnt++;
				dfs(i, -1);
			}
		}
		if (cnt > 1) printf("0\n");
		else if (ans == INF) printf("-1\n");
		else {
			if (ans == 0) ans = 1;
			printf("%d\n", ans);
		}
	}
	return 0;
}


HDU 4738 Caocao's Bridges(找割边)

标签:style   blog   http   color   os   io   ar   for   2014   

原文地址:http://blog.csdn.net/accelerator_/article/details/38933491

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