题目描述

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. 栈的C语言实现

    在C++中,可以直接使用std::stack C语言实现如下: stack.c /** * @file stack.c * @brief 栈,顺序存储. * * * */ #include <s ...

  2. ELK之elasticdump迁移es数据

    参考:https://www.cnblogs.com/resn/p/9082663.html elasticsearch部分查询语句 获取集群节点列表 curl "172.16.30.55: ...

  3. informix数据库触发器的写法

    虽然有各种数据库,但触发器的原理都是一样的,懂一种数据库的写法就可以了解其他的. 以前写过mysql数据库的触发器,这次写informix的,还顺带看了oracle的,除了语法上的不同,informi ...

  4. [No000017E]改善C#程序的建议7:正确停止线程

    开发者总尝试对自己的代码有更多的控制.“让那个还在工作的线程马上停止下来”就是诸多要求中的一种.然而事与愿违,这里面至少存在两个问题: 第一个问题是:正如线程不能立即启动一样,线程也并不能说停就停.无 ...

  5. [No0000123]WPF DataGrid Columns Visibility的绑定

    场景:根据配置文件显示DataGrid中的某些列. 问题:Columns集合只是DataGrid的一个属性,这个集合在逻辑树或视觉树中是看不到的,也不会继承DataContext属性. 方法一:对Da ...

  6. java工程师学习计划

  7. hive优化之参数调优

    1.hive参数优化之默认启用本地模式 启动hive本地模式参数,一般建议将其设置为true,即时刻启用: hive (chavin)> set hive.exec.mode.local.aut ...

  8. angular4 辅助路由

    1.辅助路由 2. 创建chat组件 ng g component chat 3. 组件html css: 1 2 3 4 5 6 7 .chat{   background:green;   hei ...

  9. LeetCode 104 Maximum Depth of Binary Tree 解题报告

    题目要求 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  10. 20165336 实验三 敏捷开发与XP实践

    20165336 实验三 敏捷开发与XP实践 一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:康志强 学号:20165336 指导教师:娄嘉鹏 实验日期:2018年4月28日 实验时 ...