poj 3613 Cow Relays【矩阵快速幂+Floyd】
!:自环也算一条路径
矩阵快速幂,把矩阵乘法的部分替换成Floyd(只用一个点扩张),这样每“乘”一次,就是经过增加一条边的最短路,用矩阵快速幂优化,然后因为边数是100级别的,所以把点hash一下最多剩下200个
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=205,inf=1e9;
int n,m,s,t,g[N],tot,x[N],y[N],z[N],h[1005],has;
struct jz
{
int a[N][N];
jz operator * (const jz &b) const
{
jz c;
for(int i=1;i<=has;i++)
for(int j=1;j<=has;j++)
c.a[i][j]=inf;
for(int k=1;k<=has;k++)
for(int i=1;i<=has;i++)
for(int j=1;j<=has;j++)
c.a[i][j]=min(c.a[i][j],a[i][k]+b.a[k][j]);
return c;
}
}a,r;
int main()
{
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&z[i],&x[i],&y[i]);
g[++tot]=x[i],g[++tot]=y[i];
}
sort(g+1,g+1+tot);
for(int i=1;i<=tot;i++)
if(i==1||g[i]!=g[i-1])
h[g[i]]=++has;
for(int i=1;i<=has;i++)
for(int j=1;j<=has;j++)
a.a[i][j]=inf;
for(int i=1;i<=m;i++)
a.a[h[x[i]]][h[y[i]]]=a.a[h[y[i]]][h[x[i]]]=min(z[i],a.a[h[y[i]]][h[x[i]]]);//,cerr<<h[y[i]]<<" "<<h[x[i]]<<" "<<z[i]<<endl;
s=h[s],t=h[t];
r=a;
n--;
while(n)
{
if(n&1)
r=r*a;
a=a*a;
n>>=1;
}
printf("%d\n",r.a[s][t]);
return 0;
}
poj 3613 Cow Relays【矩阵快速幂+Floyd】的更多相关文章
- Poj 3613 Cow Relays (图论)
Poj 3613 Cow Relays (图论) 题目大意 给出一个无向图,T条边,给出N,S,E,求S到E经过N条边的最短路径长度 理论上讲就是给了有n条边限制的最短路 solution 最一开始想 ...
- poj 3070 && nyoj 148 矩阵快速幂
poj 3070 && nyoj 148 矩阵快速幂 题目链接 poj: http://poj.org/problem?id=3070 nyoj: http://acm.nyist.n ...
- POJ 3613 [ Cow Relays ] DP,矩阵乘法
解题思路 首先考虑最暴力的做法.对于每一步,我们都可以枚举每一条边,然后更新每两点之间经过\(k\)条边的最短路径.但是这样复杂度无法接受,我们考虑优化. 由于点数较少(其实最多只有\(200\)个点 ...
- poj 3070 Fibonacci(矩阵快速幂,简单)
题目 还是一道基础的矩阵快速幂. 具体的居者的幂公式我就不明示了. #include<stdio.h> #include<string.h> #include<algor ...
- POJ 3070 Fibonacci(矩阵快速幂)
题目链接 题意 : 用矩阵相乘求斐波那契数的后四位. 思路 :基本上纯矩阵快速幂. #include <iostream> #include <cstring> #includ ...
- poj 2778 AC自动机+矩阵快速幂
题目链接:https://vjudge.net/problem/POJ-2778 题意:输入n和m表示n个病毒,和一个长为m的字符串,里面只可以有'A','C','G','T' 这四个字符,现在问这个 ...
- Scout YYF I POJ - 3744(概率dp + 矩阵快速幂)
题意: 一条路上有n个地雷,你从1开始走,单位时间内有p的概率走一步,1-p的概率走两步,问安全通过这条路的概率 解析: 很容易想到 dp[i] = p * dp[i-1] + (1 - p) * d ...
- POJ 3070 Fibonacci 【矩阵快速幂】
<题目链接> Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 ...
- POJ 3734 Blocks (矩阵快速幂)
题目链接 Description Panda has received an assignment of painting a line of blocks. Since Panda is such ...
随机推荐
- MySQL SQL语句 生成32位 UUID
在运营中,有时会碰到线下下单,线下注册,需要在数据库对其数据批量生成导入的场景. 此时如果你的数据表主键并不是Int整型自动递增而是32位的UUID这种情况该怎么办呢? MySQL 其实实现了UUID ...
- expdp,impdp,include,exclude
The examples below are based on:- the demo schema SCOTT that is created with script: $ORACLE_HOME/rd ...
- hdu 5044 树链剖分
转载:http://blog.csdn.net/qinzhenhua100/article/details/39716851 二种操作,一种更新结点值,一种更新路径值,最后输出更改后的结点值和路径值. ...
- [Nescafé 20] 玉蟾宫
★ 输入文件:jademoon.in 输出文件:jademoon.out 简单对比 时间限制:1 s 内存限制:128 MB [背景] 有一天,小猫rainbow和freda来到了湘西 ...
- windows开启远程
windows开启远程桌面超级简单,跟linux相比太简单了. 补充:有瑕疵,应该是远程中的远程桌面属性打钩,但是W8.1没有这个选项,W7可以,其次创建一个管理员账户,身份是管理员,不是标准用户,要 ...
- struct init
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h& ...
- 确定位置的经纬度LocationUtil
package com.pingyijinren.test; import android.content.Context; import android.location.Location; imp ...
- Codeforces 651D Image Preview【二分+枚举】
题意: 若干张照片,从头开始可以向左右两边读,已经读过的不需要再读,有的照片需要翻转,给定读.滑动和翻转消耗的时间,求在给定时间内最多能读多少页? 分析: 首先明确,只横跨一次,即先一直读一边然后再一 ...
- Ice Cave-CodeForces(广搜)
链接:http://codeforces.com/problemset/problem/540/C You play a computer game. Your character stands on ...
- cogs——21. [HAOI2005] 希望小学
21. [HAOI2005] 希望小学 ★★ 输入文件:hopeschool.in 输出文件:hopeschool.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述 ...