您的位置:首页 > 编程语言 > C语言/C++

2016ACM多校训练第一场_1001_Abandoned Country 并查集+DFS

2016-07-21 20:11 357 查看
        这道题是求最小生成树的总权值,以及生成树任意两点之间距离的期望。求最小生成树就直接用Kruskal算法生成就好了,求期望一开始用的是map发现会爆内存,后来改了之后还是超时了,方法不对,正解其实是用dfs来更新每个节点的儿子个数,然后由于数据范围比较大,一定要记得开long long还有double,下面是原题和代码:


Abandoned country

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 2735    Accepted Submission(s): 672


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.

 

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

 

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
#include<queue>
#include<stack>//最小生成树、并查集、dfs
using namespace std;
typedef long long ll;
typedef struct Edge{
int fro, to;
int w;
}Edge;
Edge e[1000010];
vector< pair<int, int> >p[100010];//只存了最小生成树数据结构的表

int pre[100010];//记录每个节点的前驱
int son[100010];//记录了每个节点的儿子个数
int wt[100010];//记录了每个子节点点所拥有的边的边权
int vis[100010];
bool cmp(Edge a, Edge b)
{
return a.w < b.w;
}
int find(int i)
{
if(pre[i] == i)
return i;
else
return pre[i] = find(pre[i]);
}

void join(int x, int y)
{
int fx = find(x), fy = find(y);
if(fx != fy)
pre[fx] = fy;//这里有问题啊。。。
}

int init(int k)//更新每个节点的儿子数
{
vis[k] = 1;
int ans = 0;
for(int i = 0; i < p[k].size(); i++)
{
if(!vis[p[k][i].first])
{
wt[p[k][i].first] = p[k][i].second;
ans += init(p[k][i].first);
}
}
return son[k] = (ans+1);
}

int main()
{
int t, num = 0;
//freopen("1001.in", "r", stdin);记得删掉文件重定向啊!!!
//freopen("1001.txt","w", stdout);
scanf("%d", &t);
while(t--)
{
int n, m;
ll maxa = 0;
ll exp = 0;
scanf("%d%d", &n, &m);
memset(son, 0, sizeof(son));
memset(vis, 0, sizeof(vis));
memset(pre, 0, sizeof(pre));
memset(wt, 0, sizeof(wt));

for(int i= 1; i <= n; i++)
p[i].clear();
for(int i = 1; i <= m; i++)
scanf("%d%d%d", &e[i].fro, &e[i].to, &e[i].w);

if(n == 1)//特殊情况要判定
{
printf("0 0\n");
continue;
}
sort(e+1, e+m+1, cmp);
for(int i = 1; i <= n; i++)
pre[i] = i;
int rst = n, ans = 0;

for(int i = 1; i <= m && rst > 1; i++)
{
int x = e[i].fro, y = e[i].to;
int fx = find(x), fy = find(y);
if(fx == fy)
continue;
else
{
join(x, y);
rst--;
maxa += e[i].w;
p[x].push_back(make_pair(y, e[i].w));//只存节点的数据结构,只能存最小生成树的结构,不要搞错了!!
p[y].push_back(make_pair(x, e[i].w));
}
}
int t = find(1);//寻找整个生成树的根
init(t);//给最小生成树建树,并更新每个节点的子节点个数

for(int i = 1; i <= n; i++)//统计总的边权
{
//printf("son[%d]:%d %d\n", i, son[i], wt[i]);
if(i != t)
exp += ((ll)son[i]*(n-son[i])*wt[i]);//这里要用ll不然会爆
}

double down = (double)n*(n-1)/2;
printf("%I64d %.2f\n", maxa, exp/(double)down);
//num++;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  CC++ acm dfs HDU