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. Lipshitz

    Portal --> broken qwq Description 大M正在学习函数的光滑性并对Lipschitz常数非常感兴趣:当一个定义域为\([l,r]\)的函数\(f\),对于定义域内的 ...

  2. django 自己编写admin

    继上次CRM项目之后 我们发现了django自带admin的强大之处以及灵活性,但是admin在企业中也一样很难做到完全的对接,因此编写自己的后台管理就显得至关重要. 本次自定义admin项目将接着上 ...

  3. 《剑指offer》— JavaScript(2)替换空格

    替换空格 题目描述 请实现一个函数,将一个字符串中的空格替换成"%20".例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 实现 ...

  4. Codeforces Round #209 (Div. 2)A贪心 B思路 C思路+快速幂

    A. Table time limit per test 1 second memory limit per test 256 megabytes input standard input outpu ...

  5. UESTC--1732

    原题链接:http://acm.uestc.edu.cn/problem.php?pid=1732 分析:dp,n个相同物品放入m个相同的盒子(允许为空)的个数为dp[n][m]=dp[n][m-1] ...

  6. bzoj4873 [Shoi2017]寿司餐厅

    Input 第一行包含两个正整数n,m,分别表示这家餐厅提供的寿司总数和计算寿司价格中使用的常数. 第二行包含n个正整数,其中第k个数ak表示第k份寿司的代号. 接下来n行,第i行包含n-i+1个整数 ...

  7. 洛谷P2633/bzoj2588 Count on a tree (主席树)

    洛谷P2633/bzoj2588 Count on a tree 题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K ...

  8. redis 字符串的管理的一点理解

    redis字符串可以实现通过地址偏移找到所在结构体的首地址,struct sdshdr *sh = (void *)(s - (sizeof(struct sdshdr))) 也就是通过buf地址可以 ...

  9. bzoj 4347 [POI2016]Nim z utrudnieniem DP

    4347: [POI2016]Nim z utrudnieniem Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 733  Solved: 281[Su ...

  10. centos7 ffmpeg安装

    #Nux Dextop库依赖于EPEL库,所有要先安装EPEL库yum -y install epel-release #安装Nux Dextop库rpm -Uvh http://li.nux.ro/ ...