For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race using the T (2 ≤ T ≤ 100) cow trails throughout the pasture.

Each trail connects two different intersections (1 ≤ I1i ≤ 1,000; 1 ≤ I2i ≤ 1,000), each of which is the termination for at least two trails. The cows know the lengthi of each trail (1 ≤ lengthi  ≤ 1,000), the two intersections the trail connects, and they know that no two intersections are directly connected by two different trails. The trails form a structure known mathematically as a graph.

To run the relay, the N cows position themselves at various intersections (some intersections might have more than one cow). They must position themselves properly so that they can hand off the baton cow-by-cow and end up at the proper finishing place.

Write a program to help position the cows. Find the shortest path that connects the starting intersection (S) and the ending intersection (E) and traverses exactly N cow trails.

Input

* Line 1: Four space-separated integers: N, T, S, and E
* Lines 2..T+1: Line i+1 describes trail i with three space-separated integers: lengthi , I1i , and I2i

Output

* Line 1: A single integer that is the shortest distance from intersection S to intersection E that traverses exactly N cow trails.

Sample Input

2 6 6 4
11 4 6
4 4 8
8 4 9
6 6 8
2 6 9
3 8 9

Sample Output

10

题意:求过s,t两点的刚好经过k条边的最短路
思路:任意最短路可以想到Floyd,(01矩阵的乘积A^k中,A[i][j]代表刚好经过k条边的从i到j的数量)
maps【i】【j】 为 经过一条边的最短路, 对于maps【i】【k】 + maps【k】【j】 可以看出是经过两条边的最短路
那么对于
r+m == k 且 A为经过r条边的最短路,B为经过m条边的最短路,通过maps【i】【k】+maps【k】【j】就得到了刚好经过k条边的最短路
 #include<cstdio>
#include<iostream>
#include<cstring>
using namespace std; int n,k,m,s,t;
int has[];
struct matrix
{
int maps[][];
matrix operator *(const matrix &x)const
{
matrix c;
memset(c.maps,0x3f,sizeof(c.maps));
for(int k=;k<=n;k++)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
c.maps[i][j] = min(c.maps[i][j],maps[i][k]+x.maps[k][j]);
}
}
}
return c;
}
}; matrix qpow(matrix a,int k)
{
matrix ans = a;
k--;
while(k)
{
if(k&)ans = ans * a;
a = a*a;
k >>= ;
}
return ans;
}
int main()
{
while(~scanf("%d%d%d%d",&k,&m,&s,&t))
{
int tot=;
matrix ans;
memset(ans.maps,0x3f,sizeof(ans.maps));
for(int i=;i<=m;i++)
{
int w,x,y;
scanf("%d%d%d",&w,&x,&y);
if(!has[x])
{
has[x] = ++tot;
}
if(!has[y])
{
has[y] = ++tot;
}
if(w < ans.maps[has[x]][has[y]])
{
ans.maps[has[x]][has[y]] = ans.maps[has[y]][has[x]] = w;
}
}
n = tot;
ans = qpow(ans,k);
printf("%d\n",ans.maps[has[s]][has[t]]);
}
}

Cow Relays POJ - 3613 (floyd+快速幂)的更多相关文章

  1. poj 3613 floyd + 快速幂

    题意:本题的大意就是问从S 到 T 经过边得个数恰为k的最短路是多少. 思路:对于邻接矩阵每一次floyd求的是每个点间的最短距离,则n次floyd就是每个点间n条路的最短距离(可以重复边); 但是由 ...

  2. POJ 3613 Cow Relays(floyd+快速幂)

    http://poj.org/problem?id=3613 题意: 求经过k条路径的最短路径. 思路: 如果看过<矩阵乘法在信息学的应用>这篇论文就会知道 现在我们在邻接矩阵中保存距离, ...

  3. poj3613:Cow Relays(倍增优化+矩阵乘法floyd+快速幂)

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7825   Accepted: 3068 Descri ...

  4. POJ 3613 floyd+矩阵快速幂

    题意: 求s到e恰好经过n边的最短路 思路: 这题已经被我放了好长时间了. 原来是不会矩阵乘法,快速幂什么的也一知半解 现在终于稍微明白了点了 其实就是把矩阵乘法稍微改改 改成能够满足结合律的矩阵&q ...

  5. poj 1995 裸快速幂

    1. poj 1995  Raising Modulo Numbers 2.链接:http://poj.org/problem?id=1995 3.总结:今天七夕,来发水题纪念一下...入ACM这个坑 ...

  6. POJ3613 Cow Relays [矩阵乘法 floyd类似]

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7335   Accepted: 2878 Descri ...

  7. poj 3233 矩阵快速幂

    地址 http://poj.org/problem?id=3233 大意是n维数组 最多k次方  结果模m的相加和是多少 Given a n × n matrix A and a positive i ...

  8. poj 3734 Blocks 快速幂+费马小定理+组合数学

    题目链接 题意:有一排砖,可以染红蓝绿黄四种不同的颜色,要求红和绿两种颜色砖的个数都是偶数,问一共有多少种方案,结果对10007取余. 题解:刚看这道题第一感觉是组合数学,正向推了一会还没等推出来队友 ...

  9. poj 3734 矩阵快速幂+YY

    题目原意:N个方块排成一列,每个方块可涂成红.蓝.绿.黄.问红方块和绿方块都是偶数的方案的个数. sol:找规律列递推式+矩阵快速幂 设已经染完了i个方块将要染第i+1个方块. a[i]=1-i方块中 ...

随机推荐

  1. 爬虫 BeatifulSoup 模块

    BeatifulSoup 模块 介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库 安装 pip install beautifulsoup4  解析器下载 ...

  2. mongoDB 数据库简介

    背景概念-数据库类型 对比关系型数据库 优点 : 逻辑清晰.容易理解,结构类似表格 使用 sql 语句技术成熟,使用方便 数据一致性好,完整性好 关系型数据库比较成熟,可以使用一些复杂操作 缺点 : ...

  3. 【CF1132G】Greedy Subsequences(线段树)

    [CF1132G]Greedy Subsequences(线段树) 题面 CF 题解 首先发现选完一个数之后选择下一个数一定是确定的. 对于每个数预处理出左侧第一个比他大的数\(L\),那么这个数加入 ...

  4. BZOJ 3192: [JLOI2013]删除物品(树状数组)

    题面: https://www.lydsy.com/JudgeOnline/problem.php?id=3192 题解: 首先每次一定是来回移动直到最大的到顶上. 所以我们可以将第两个堆的堆顶接起来 ...

  5. 联想的笔记本有隐藏分区 导致无法安装win10 eufi启动 报错:windows无法更新计算机的启动配置。无法安装

    联想的笔记本都带着类似一键还原等的系统恢复软件,这些软件往往是将出厂设置备份在单 独的一个分区,此分区默认为隐藏,在 Windows 的磁盘管理中可以看到.打开磁盘管理器 的方法是右击计算机——管理, ...

  6. Vue(小案例_vue+axios仿手机app)_购物车

    一.前言 1.购物车 二.主要内容 1.效果演示如下,当我们选择商品数量改变的时候,也要让购物车里面的数据改变 2.具体实现 (1)小球从上面跳到下面的效果 (2)当点击上面的“加入购物车按钮”让小球 ...

  7. <二>ELK-6.5.3学习笔记–使用rsyslog传输管理nginx日志

    http://www.eryajf.net/2362.html 转载于 本文预计阅读时间 28 分钟 文章目录[隐藏] 1,nginx日志json化. 2,发送端配置. 3,接收端配置. 4,配置lo ...

  8. Python中如何设置输出文字的颜色

    一.语法 1.实现过程 终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关.控制字符颜色的转义序列是以ESC开头,即用\033来完成   2.书写过程 开头部分: \033 ...

  9. pip install升级包

    只需要python -m pip install --user --upgrade pip==9.0.3 只需要加一个--user

  10. 分布式监控系统开发【day38】:主机存活检测程序解析(七)

    一.目录结构 二.入口 1.文件MonitorServer.py import os import sys if __name__ == "__main__": os.enviro ...