ACM学习之路___HDU 5723(kruskal + dfs)

时间:2023-03-10 05:40:09
ACM学习之路___HDU 5723(kruskal + dfs)
Abandoned country

Time Limit: / MS (Java/Others)    Memory Limit: / K (Java/Others)
Total Submission(s): Accepted Submission(s): Problem Description
An abandoned country has n(n≤) villages which are numbered from to n. Since abandoned for a long time, the roads need to be re-built. There are m(m≤) roads to be re-built, the length of each road is wi(wi≤). 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≤) 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 Sample Output
3.33

Problem Description

题意 :

  国家有M条废弃的路和N个点,现在要重新修,求出连通每一个点需要的最短路径是多少,并求在连通的路任意选两个点走,它的最小期望。

解法:

  最短路径好求,单纯最小生成树。但是第一眼看见求期望,MD,啥几把题,很难懂,这都过了这么多天,重新捡起这道题,发现网上都是 最小生成树+dfs 纠结了好久,确实不知道dfs究竟怎么求得期望,看了网上一片博文,大致知道了期望怎么求,但是看懂 dfs 还
是花费了好长时间(看懂之后一直觉得这什么傻逼题)。
具体求每条路的贡献 :
  设某一段路在用DFS遍历路径过程中在该条路径遍历过的次数为 cnt,设该条路length ,则它的贡献就是 cnt * ( nodeNum - cnt ) * length。
  最后题目中所求期望就是所有的路的贡献加起来  除以(nodeNum * ( nodeNum - 1.0 ) / 2.0 ) 。
开始参照网上的代码交了一发 内存有4W K+ , 后来按照以前kruskal 算法的模板(毕竟我还是太水)加上自己一些想法优化些,时间没怎么减少,内存可以降到 2W K+ , 上一发我的代码 
 #include <bits/stdc++.h>
#define maxn 100005
using namespace std;
double res; vector< pair<int,int> >Edge1[maxn]; struct Edge
{
int from;
int to;
int length;
friend bool operator < (const Edge &e1 , const Edge &e2)
{
return e1.length > e2.length;//最小值优先
}
}; int father[maxn]; //用来做并查集
int nodeNum,edgeNum; //顶点数、边数
long long MST; //最小生成树边权值和 priority_queue<Edge> myQ; //优先队列 void storeMap() //存储岛的桥构成的图
{
while(!myQ.empty())
myQ.pop(); //清空队列
int from,to,length;
for(int i = ; i < edgeNum ; i++) //kruskal算法对于无向图也只需建一条边即可
{
scanf("%d%d%d",&from,&to,&length);
Edge e;
e.from = from;
e.to = to;
e.length = length;
myQ.push(e);
}
} int findx(int x) //查找父节点
{
if(x == father[x])
return father[x];
return father[x] = findx(father[x]);
} bool judge() //判断是否是一棵最小生成树 ,这里得注意起点和终点
{
int f = findx();
for(int i = ; i <= nodeNum ; i++)
{
if(f != findx(i))
return false;
}
return true;
} void init()//初始化函数
{
for(int i = ; i <= nodeNum ; i++)
{
father[i] = i;
Edge1[i].clear();
}
return;
}//特意把 maxn 改成 nodeNum 并且把这个模块从底下的函数中独立出来,没想到时间一点也没少,反倒增加了,很是迷茫 int kruskal() //kruskal算法
{
MST = ;
int num = ; //记录MST的边数
while(!myQ.empty() && num != nodeNum-)
{
Edge e = myQ.top();
myQ.pop();
int fx = findx(e.from);
int fy = findx(e.to);
if(fx != fy)
{
father[fx] = fy;
MST += e.length;
Edge1[ e.from ].push_back(make_pair(e.to , e.length));
Edge1[ e.to ].push_back(make_pair(e.from , e.length));
num++;
}
}
return num;
} int dfs(int x , int f)
{
int cnt = ;//该条路遍历过次数
for(int i = ; i < Edge1[x].size() ; i++)
{
int v = Edge1[x][i].first;
if(v == f)
continue;
int fcnt = dfs( v , x );
cnt += fcnt;
res = res + 1.0 * fcnt * ( nodeNum - fcnt) * Edge1[x][i].second;//该条路的贡献
}
return cnt + ;
} int main()
{
//freopen("sample.in" , "r" , stdin);
//freopen("sample1.out" , "w" , stdout);
int t;
scanf("%d",&t);
while(t--)
{
res = ;
scanf("%d%d",&nodeNum , &edgeNum);
storeMap();
init();
kruskal();
dfs( , );
res = res * 2.0 / (nodeNum *1.0) /(nodeNum - 1.0);
printf("%I64d %.2lf\n",MST,res );
}
return ;
}