uva 1504 - Genghis Khan the Conqueror(生成树)

179 篇文章 0 订阅

题目链接:uva 1504 - Genghis Khan the Conqueror


类似次小成树,先根据已知边建立最小生成树。然后在此基础上,考虑未被选中的边,处理出两两节点间权值最小的边,即为替换最优边。处理的过程为枚举每个点做根,dfs回溯处理,并维护。


#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <algorithm>

using namespace std;
const int maxn = 3005;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;

struct Edge {
	int u, v, d;
	Edge(int u = 0, int v = 0, int d = 0): u(u), v(v), d(d) {}
	bool operator < (const Edge& a) const { return d < a.d; }
}E[maxn * maxn];

int N, M, Q, F[maxn], G[maxn][maxn], D[maxn][maxn], V[maxn][maxn];
int K, first[maxn], jump[maxn << 1], link[maxn << 1];

inline void addEdge(int u, int v) {
	jump[K] = first[u];
	link[K] = v;
	first[u] = K++;
}
int find(int x) { return x == F[x] ? x : F[x] = find(F[x]); }

void init () {
	memset(V, 0, sizeof(V));
	memset(G, inf, sizeof(G));
	memset(D, inf, sizeof(D));

	int u, v, d;
	for (int i = 0; i < M; i++) {
		scanf("%d%d%d", &u, &v, &d);
		E[i] = Edge(u, v, d);
		G[u][v] = G[v][u] = min(G[u][v], d);
	}
}

double solve () {
	int n = N;
	double ret = 0;
	sort(E, E + M);

	K = 0;
	memset(first, -1, sizeof(first));
	for (int i = 0; i < N; i++) F[i] = i;

	for (int i = 0; i < M; i++) {
		int u = E[i].u, v = E[i].v, d = E[i].d;
		if (find(u) != find(v)) {
			F[find(u)] = find(v);
			ret += d;
			addEdge(u, v);
			addEdge(v, u);
			V[u][v] = V[v][u] = 1;
		}
	}
	return ret;
}

int dfs (int root, int u, int fa) {
	int ret = inf;

	for (int i = first[u]; i != -1; i = jump[i]) {
		int v = link[i];
		if (v == fa) continue;
		int tmp = dfs(root, v, u);
		ret = min(tmp, ret);
		D[u][v] = D[v][u] = min(D[u][v], tmp);
	}

	if (fa != root)
		ret = min(ret, G[root][u]);
	return ret;
}

int main () {
	while (scanf("%d%d", &N, &M) == 2 && N + M) {
		init();
		double ans = solve(), sum = 0;
		for (int i = 0; i < N; i++) dfs(i, i, -1);

		scanf("%d", &Q);
		int u, v, d;
		for (int i = 0; i < Q; i++) {
			scanf("%d%d%d", &u, &v, &d);
			if (V[u][v]) {
				sum += min(D[u][v], d) - G[u][v];
			}
		}
		printf("%.4lf\n", sum / Q + ans);
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The Mongol Empire was one of the largest empires in history, stretching from Eastern Europe to Asia. It was founded by Genghis Khan in 1206 and lasted until the mid-14th century. The empire was known for its military prowess, innovative tactics, and the use of terror to intimidate its enemies. Genghis Khan was born in 1162 in the region that is now Mongolia. He was born into a family of nomads and grew up herding sheep and hunting. At the age of 16, he was captured by a rival tribe and enslaved. He escaped and later began to unite the various tribes of the region under his leadership. In 1206, he was declared the supreme ruler of the Mongol Empire. Under Genghis Khan's leadership, the Mongol Empire began to expand rapidly. Genghis Khan was a brilliant military strategist and his army was known for its speed and mobility. The Mongols were also skilled horsemen and archers, and they were able to defeat much larger armies using innovative tactics such as feigned retreats and surprise attacks. After Genghis Khan's death in 1227, his empire was divided among his four sons. Over the next few decades, the Mongol Empire continued to expand under the leadership of Genghis Khan's descendants. They conquered China, Central Asia, and parts of Eastern Europe. The Mongols were known for their brutality and the use of terror to intimidate their enemies. They were also tolerant of other religions and cultures, and they allowed conquered peoples to retain their own customs and traditions. One of the most famous Mongol leaders was Kublai Khan, who ruled China from 1279 to 1294. He founded the Yuan Dynasty, which was the first foreign dynasty to rule China. Kublai Khan was a patron of the arts and literature, and he encouraged the development of new technologies such as paper money and gunpowder. He also sent emissaries to Europe, including Marco Polo, who wrote about his travels in China. The Mongol Empire began to decline in the mid-14th century. The empire had become too large to be effectively governed, and there were internal divisions and conflicts among the ruling families. The Black Death, which swept through Europe and Asia in the mid-14th century, also had a devastating impact on the Mongol Empire. By the end of the 14th century, the Mongol Empire had disintegrated into several smaller states. Despite its reputation for brutality, the Mongol Empire had a significant impact on world history. The Mongols were responsible for the spread of ideas, technologies, and cultures across Eurasia. They also played a role in the development of international trade and commerce. The Mongol Empire was a significant force in world history, and its legacy can still be felt today.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值