Abandoned country
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 378 Accepted Submission(s): 104
Problem Description
An abandoned country has
n(n≤100000)
villages which are numbered from 1 to n.
Since abandoned for a long time, the roads need to be re-built. There are
m(m≤1000000)
roads to be re-built, the length of each road is wi(wi≤1000000).
Guaranteed that any two wi
are different. The roads made all the villages connected directly or indirectly before destroyed. Every road will cost the same value of its length to rebuild. The king wants to use the minimum cost to make all the villages connected with each other directly
or indirectly. After the roads are re-built, the king asks a men as messenger. The king will select any two different points as starting point or the destination with the same probability. Now the king asks you to tell him the minimum cost and the minimum
expectations length the messenger will walk.
Input
The first line contains an integer
T(T≤10)
which indicates the number of test cases.
For each test case, the first line contains two integers n,m indicate the number of villages and the number of roads to be re-built. Next m lines, each line have three number i,j,wi, the length of a road connecting the village i and the village j is wi.
For each test case, the first line contains two integers n,m indicate the number of villages and the number of roads to be re-built. Next m lines, each line have three number i,j,wi, the length of a road connecting the village i and the village j is wi.
Output
output the minimum cost and minimum Expectations with two decimal places. They separated by a space.
Sample Input
1 4 6 1 2 1 2 3 2 3 4 3 4 1 4 1 3 5 2 4 6
Sample Output
6 3.33
解析:
求图的最小生成树,在最小生成树的基础上,求最小期望长度,实际上就是求每一条边的贡献度。
注意强制转换的过程。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 1000000+10;
struct node {
int u, v;
LL val;
node (){}
node (int _u, int _v, int _val){
u = _u;
v = _v;
val = _val;
}
bool operator < (const node &a) const
{
return val<a.val;
}
};
LL val[maxn];
node e[maxn];
int su[100000+10];
int fa[100000+10];
vector<node>p[100000+10];
void init() {
for (int i=0; i<100000+10; i++)
fa[i] = i, p[i].clear();
memset(su, 0, sizeof(su));
memset(val, 0, sizeof(val));
}
void DFS(int u, int pre) {
su[u] = 1;
for (int i=0; i<p[u].size(); i++) {
int v = p[u][i].v;
if (v == pre)
continue;
val[v] = p[u][i].val;
DFS(v, u);
su[u] += su[v];
}
}
int find(int x) {
return x==fa[x]?x:fa[x] = find(fa[x]);
}
int main() {
int T;
scanf("%d",&T);
while (T--) {
int n, m;
init();
scanf("%d%d", &n, &m);
for (int i=0; i<m; i++)
scanf("%d%d%I64d", &e[i].u, &e[i].v, &e[i].val);
sort(e, e+m);
LL sum=0;
for (int i=0; i<m; i++) {
int v = find(e[i].v);
int u = find(e[i].u);
if (u != v) {
fa[u] = v;
sum += e[i].val;
p[e[i].u].push_back((node){e[i].u, e[i].v, e[i].val});
p[e[i].v].push_back((node){e[i].v, e[i].u, e[i].val});
}
}
DFS(1, -1);
LL ans = 0;
for (int i=2; i<=n; i++)
ans += (LL)(n-su[i])*(LL)su[i]*val[i];
LL t = (LL)n*(n-1)/2;
printf("%I64d %.2lf\n",sum, (double)(ans)/(double)(t));
}
return 0;
}
本文探讨了如何通过计算最小生成树解决一个废弃国家中重建道路的问题,并在此基础上求解消息员行走的最小期望长度。文章提供了一段C++代码实现,详细展示了从输入数据到计算最小生成树并求解期望路径长度的全过程。
625

被折叠的 条评论
为什么被折叠?



