Description

大家都知道最小生成树一般求的是构成最小生成树的边的权值之和。
现在请求构成最小生成树的边的权值之积 S,最终结果请输出 (S % 100003)。
P.S. 点之间的边为无向边,矩阵保证符合无向图的对称性。

Input

多组数据。

第一行,整数N,表示N个点。(0 < N <= 100)

接下来为一个N*N 保证合法的邻接矩阵,矩阵内均为自然数。

Output

每组数据输出一行整数结果。

Sample Input

3
0 1 2
1 0 3
2 3 0
2
0 5
5 0

Prim

 #include<iostream>
#include<cstring>
using namespace std; #define MAX 105
#define INF 0x7FFFFFFF
int map[MAX][MAX]; // adjacency matrix
bool vis[MAX]; // is the node i visited
int dis[MAX]; // distance between current node and other node i void prim(int n) {
memset(vis, false, sizeof(vis)); // current node is 0
int cur = ;
for (int i = ; i < n; ++i)
dis[i] = map[cur][i]; // dis[] store distance between other nodes and 0
vis[cur] = true; unsigned long long s = ;
for (int i = ; i < n - ; ++i) {
// find the shortest edge between cur and other unvisited nodes
int min = INF;
for(int j = ; j < n; ++j)
if (!vis[j] && dis[j] < min)
min = dis[cur = j]; // update to next shortest edge and potential cur // the other end is visited and is now current node
s *= min;
s %= ;
vis[cur] = true; // update dis[] to store distance between other nodes and cur
// if the node is visited, leave it
for (int j = ; j < n; ++j)
if (!vis[j] && map[cur][j] < dis[j])
dis[j] = map[cur][j];
}
cout << (s % ) << endl;
} int main() {
int n; while(scanf("%d", &n) != EOF) {
for (int i = ; i < n; ++i)
for (int j = ; j < n; ++j)
scanf("%d", &map[i][j]);
prim(n);
} return ;
}

Kruskal,其实改装成了边表,加了并查集,有做路径压缩,可以水过就懒得写带rank的优化了……

 #include <iostream>
#include <cstring>
#include <algorithm>
using namespace std; #define MAX 105
#define INF 0x7FFFFFFF int root[MAX];
int map[MAX][MAX]; struct Edge {
int u, v;
int d;
} e[MAX*MAX]; bool cmp(Edge a, Edge b) {
return a.d < b.d;
} int find(int x) {
while(x != root[x]) {
root[x] = root[root[x]]; // compress
x = root[x];
}
return x;
} unsigned long long kruskal(int vn, int en) {
unsigned long long p = ; // connect to itself
for (int i = ; i < vn; ++i)
root[i] = i; // sort edges by length
std::sort(e, e+en, cmp); for(int i = ; i < en; ++i) {
int ru = find(e[i].u);
int rv = find(e[i].v);
if(ru != rv) { // the ends of e[i] are not in the same spanning tree
root[ru] = rv; // merge
p *= e[i].d;
p %= ;
}
} return p;
} int main() {
int vn;
while(cin >> vn) {
memset(map, , sizeof(map));
for(int i = ; i < vn; ++i)
for(int j = ; j < vn; ++j)
cin >> map[i][j]; int en = ;
for(int i = ; i < vn; ++i) // use upper-right corner
for(int j = i + ; j < vn; ++j) {
// transform to adjacency list
e[en].u = i;
e[en].v = j;
e[en].d = map[i][j];
++en;
}
cout << kruskal(vn, en) % << '\n';
}
return ;
}

因为数据太大所以每次乘完都要取模,不然会溢出。

因为本来就给的是邻接矩阵,kruskal要多做一步处理转换成边表,速度大约是prim的一半

全连通图求最小生成树边权之积(邻接矩阵/prim/kruskal)的更多相关文章

  1. 最小生成树 链式前向星 Prim&Kruskal

    Prim: Prim的思想是将任意节点作为根,再找出与之相邻的所有边(用一遍循环即可),再将新节点更新并以此节点作为根继续搜,维护一个数组:dis,作用为已用点到未用点的最短距离. 证明:Prim算法 ...

  2. hdu 3405 删掉某点后 求最小生成树

    给出N个点的坐标 边的权值为两点间的距离 删掉其中某点 求最小生成树的权值和 要求这权值最小 因为最多50个点 所以具体是删哪个点 用枚举假如有4个点 就要求4次最小生成树 分别是2 3 4 | 1 ...

  3. 【2018 ICPC亚洲区域赛徐州站 A】Rikka with Minimum Spanning Trees(求最小生成树个数与总权值的乘积)

    Hello everyone! I am your old friend Rikka. Welcome to Xuzhou. This is the first problem, which is a ...

  4. Prim算法和Kruskal算法求最小生成树

    Prim算法 连通分量是指图的一个子图,子图中任意两个顶点之间都是可达的.最小生成树是连通图的一个连通分量,且所有边的权值和最小. 最小生成树中,一个顶点最多与两个顶点邻接:若连通图有n个顶点,则最小 ...

  5. prime算法求最小生成树(畅通工程再续)

    连着做了四道畅通工程的题,其实都是一个套路,转化为可以求最小生成树的形式求最小生成树即可 这道题需要注意: 1:因为满足路的长度在10到1000之间才能建路,所以不满足条件的路径长度可以初始化为无穷 ...

  6. Kruskal和Prim算法求最小生成树

    Kruskal算法求最小生成树 测试数据: 5 6 0 1 5 0 2 3 1 2 4 2 4 2 2 3 1 1 4 1 输出: 2 3 1 1 4 1 2 4 2 0 2 3 思路:在保证不产生回 ...

  7. 克鲁斯卡尔(Kruskal)算法求最小生成树

    /* *Kruskal算法求MST */ #include <iostream> #include <cstdio> #include <cstring> #inc ...

  8. poj 2349 求最小生成树里面第m长的边

    题目链接:https://vjudge.net/problem/POJ-2349 题意: 题目就是要我们找到一个最小的值D,把图里面所有大于D的边去掉之后剩余的连通分支的数量为S.这个就是找这个图里面 ...

  9. NYOJ 1875 畅通工程再续 (无节点间距离求最小生成树)

    Description 相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题 ...

随机推荐

  1. 在 Xamarin.Forms 实现密码输入EntryCell

    在 Xamarin.Forms 中,我们通常使用 TableView 来构建输入表单.Xamarin 为我们提供了 EntryCell 用于输入文本,但是其并不支持密码输入,即密码掩码.这里要对 En ...

  2. 用Visual C#开发基于OpenCV的Windows应用程序

    http://blog.163.com/wangxh_jy/blog/static/28233883201001581640283/ 关于详细的配置及程序运行截图,请下载:http://downloa ...

  3. phonegap(cordova)从手机app跳转到web页面在跳转回APP本地页面思路

    项目中需要用到 WAP支付宝支付. 但是 使用PHONEGAP开发 跳转到支付宝支付,然后跳转回来 就回不到APP的本地页面, 就是使用WAP的第三方登录也是一样的.很难从WAP页面在跳转到 app本 ...

  4. poj3648 Wedding

    Wedding Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10975   Accepted: 3355   Specia ...

  5. -webkit-line-clamp 多行文字溢出...

    一.应用 CSS代码: .box { width: 100px; display: -webkit-box; -webkit-line-clamp:; -webkit-box-orient: vert ...

  6. input模拟

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 遍历hashmap

    转]Java中HashMap遍历的两种方式原文地址: http://www.javaweb.cc/language/java/032291.shtml 第一种: Map map = new HashM ...

  8. 推箱子 BFS

    [编程题] 推箱子 大家一定玩过“推箱子”这个经典的游戏.具体规则就是在一个N*M的地图上,有1个玩家.1个箱子.1个目的地以及若干障碍,其余是空地.玩家可以往上下左右4个方向移动,但是不能移动出地图 ...

  9. 【leetcode 简单】第四十八题 旋转数组

    给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6,7] 和 k = 3 输出: [5,6,7,1,2,3,4] 解释: 向右旋转 1 ...

  10. windows下面安装Python和pip教程

    第一步,先来安装Python.windows下面的Python安装一般是通过软件安装包安装而不是命令行,所以首先要在Python的官方主页上面下载最新的Python安装包.下载地址是:https:// ...