题目描述

N个城市,标号从0到N-1,M条道路,第K条道路(K从0开始)的长度为2^K,求编号为0的城市到其他城市的最短距离

输入描述:

第一行两个正整数N(2<=N<=100)M(M<=500),表示有N个城市,M条道路
接下来M行两个整数,表示相连的两个城市的编号

输出描述:

N-1行,表示0号城市到其他城市的最短路,如果无法到达,输出-1,数值太大的以MOD 100000 的结果输出。
示例1

输入

复制

4 4
1 2
2 3
1 3
0 1

输出

复制

8
9
11 https://www.cnblogs.com/lca1826/p/6748372.html 关于快速幂的讲解
https://www.cnblogs.com/Asimple/p/6502632.html 代码来自于这个
//Asimple
//#include <bits/stdc++.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cctype>
#include <cstdlib>
#include <stack>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <limits.h>
#include <time.h>
#define INF 0xfffffff
#define mod 100000
#define PI 3.14159265358979323
#define swap(a,b,t) t = a, a = b, b = t
#define CLS(a, v) memset(a, v, sizeof(a))
#define debug(a) cout << #a << " = " << a <<endl
#define dobug(a, b) cout << #a << " = " << a << " " << #b << " = " << b << endl
using namespace std;
typedef long long ll;
const int maxn = ;
int n, m, num, T, k, len, ans, sum, x, y;
int Map[maxn][maxn];
int fa[maxn];
ll qpow(ll a, ll b, ll md) {//这里使用快速幂来计算2的k次方,并且每次都%md,保证不溢出
ll ans = ;
while( b ) {
if( b & ) ans = ans * a % md;
a = a * a % md;
b = b >> ;
}
return ans;
}
int find(int x) {
return x==fa[x]?x:fa[x]=find(fa[x]);
} void solve() {
x = find();
for(int i=; i<n; i++) {
if( x!=find(i) ) cout << "-1" << endl;//如果不在一个集合里,那么就-1
else cout << Map[][i] << endl;
}
} void input() {
while( cin >> n >> m ) {
for(int i=; i<n; i++) {
fa[i] = i;
Map[i][i] = ;
}
for(int i=; i<m; i++) {
cin >> x >> y;
num = qpow(, i, mod);
int xx = find(x);
int xy = find(y);
if( xx==xy ) continue;//如果已经联通了,那么就忽略,因为后来的边权值一定比原来的所有边大,已经联通就不用再放进来了。
for(int j=; j<n; j++) {
if( xx!=find(j) ) continue;//也就是找到当前集合的根
for(int k=; k<n; k++) {
if( xy!=find(k) ) continue;
Map[j][k] = Map[k][j] = (Map[j][x] + num + Map[y][k])%mod;
}
}
fa[xy] = xx;
}
solve();
}
} int main(){
input();
return ;
}

//学习到了很多,b&1得到的结果只是个位与。==我觉得难点就在于这个2的幂会很大,你要怎么去处理?

最短路径-并查集+Floyd[转载]的更多相关文章

  1. [POJ1236]Network of Schools(并查集+floyd,伪强连通分量)

    题目链接:http://poj.org/problem?id=1236 这题本来是个强连通分量板子题的,然而弱很久不写tarjan所以生疏了一下,又看这数据范围觉得缩点这个事情可以用点到点之间的距离来 ...

  2. codeforces 400D Dima and Bacteria 并查集+floyd

    题目链接:http://codeforces.com/problemset/problem/400/D 题目大意: 给定n个集合,m步操作,k个种类的细菌, 第二行给出k个数表示连续的xi个数属于i集 ...

  3. codeforces 400 D Dima and Bacteria【并查集 Floyd】

    题意:给出n个点,分别属于k个集合,判断每个集合里面的点的距离都为0,为0的话输出yes,并输出任意两个集合之间的最短路 这道题目有两个地方不会处理, 先是n个点,分别属于k个集合,该怎么记录下来这里 ...

  4. upc组队赛14 Communication【并查集+floyd /Tarjan】

    Communication 题目描述 The Ministry of Communication has an extremely wonderful message system, designed ...

  5. BZOJ 1854: [Scoi2010]游戏 并查集

    1854: [Scoi2010]游戏 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 2672  Solved: 958[Submit][Status][ ...

  6. 求最小环 —— 并查集 与 Floyd

    对于一个图,如何求出其中的最小环(不包括一元环)? 很显然,对于一个无向图,每一条边都是一个二元环:对于有向图,可以考虑从每一个点出发,用DFS求出它到自己的距离,如果遍历了$N$个点仍未便利到自己, ...

  7. New Year Permutation(Floyd+并查集)

    Description User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to mak ...

  8. 稀疏图(邻接链表),并查集,最短路径(Dijkstra,spfa),最小生成树(kruskal,prim)

    全部函数通过杭电 1142,1162,1198,1213等题目测试. #include<iostream> #include<vector> #include<queue ...

  9. 稠密图(邻接矩阵),并查集,最短路径(Dijkstra,spfa),最小生成树(kruskal,prim)

    全部函数通过杭电 1142,1162,1198,1213等题目测试. #include<iostream> #include<vector> #include<queue ...

随机推荐

  1. 第9天 py就业班基础02.01-02

    明天该看就业班的02    03字串符 2018-4-21 10:47:34 数据类型  py自动给数据分类型 2018-4-21 10:55:05 input使用 定义一个变量 然后input输给变 ...

  2. IIS 使用域账户访问SQL 需要配置

    打开应用程序的 Web.config 文件并添加以下元素:   <authentication mode="Windows" /> <identity imper ...

  3. TOP100summit:【分享实录-途牛】价格中心系统的优化之路

    本篇文章内容来自2016年TOP100summit途牛旅游网高级架构师,技术委员会开发组长赵国光的案例分享.编辑:Cynthia 导读:价格中心系统是途牛网众多系统中很重要的一个,几乎所有的售卖价格计 ...

  4. c++ assert() 使用方法

    assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义: #include <assert.h> void assert( i ...

  5. HDU 1002 - A + B Problem II - [高精度]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 Problem DescriptionI have a very simple problem ...

  6. 深入理解无穷级数和的定义(the sum of the series)

    Given an infinite sequence (a1, a2, a3, ...), a series is informally the form of adding all those te ...

  7. JQuery中数组的创建与使用

    一.创建数组的方式: 1.定义并赋值 var str = ['java', 'php', 'c++', 'c#', 'perl', 'vb', 'html', 'css']; 2.用{}定义后赋值: ...

  8. winform excel导入--自带office.interop.excel方式

    用npoi方式,遇到一个问题,有的excel用加密软件(盖章,只读等)生成的解析不了,所以换成自带的方式,可以解决. 需要引用系统自带Microsoft.office.interop.excel pu ...

  9. Druid 在有赞的实践

    https://mp.weixin.qq.com/s?__biz=MzAxOTY5MDMxNA==&mid=2455759407&idx=1&sn=28390d7f5b2685 ...

  10. mysql学习【第5篇】:事务索引备份视图

    狂神声明 : 文章均为自己的学习笔记 , 转载一定注明出处 ; 编辑不易 , 防君子不防小人~共勉 ! mysql学习[第5篇]:事务索引备份视图 MySQL事务 事务就是将一组SQL语句放在同一批次 ...