Cow Relays POJ - 3613 (floyd+快速幂)
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+快速幂)的更多相关文章
- poj 3613 floyd + 快速幂
题意:本题的大意就是问从S 到 T 经过边得个数恰为k的最短路是多少. 思路:对于邻接矩阵每一次floyd求的是每个点间的最短距离,则n次floyd就是每个点间n条路的最短距离(可以重复边); 但是由 ...
- POJ 3613 Cow Relays(floyd+快速幂)
http://poj.org/problem?id=3613 题意: 求经过k条路径的最短路径. 思路: 如果看过<矩阵乘法在信息学的应用>这篇论文就会知道 现在我们在邻接矩阵中保存距离, ...
- poj3613:Cow Relays(倍增优化+矩阵乘法floyd+快速幂)
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7825 Accepted: 3068 Descri ...
- POJ 3613 floyd+矩阵快速幂
题意: 求s到e恰好经过n边的最短路 思路: 这题已经被我放了好长时间了. 原来是不会矩阵乘法,快速幂什么的也一知半解 现在终于稍微明白了点了 其实就是把矩阵乘法稍微改改 改成能够满足结合律的矩阵&q ...
- poj 1995 裸快速幂
1. poj 1995 Raising Modulo Numbers 2.链接:http://poj.org/problem?id=1995 3.总结:今天七夕,来发水题纪念一下...入ACM这个坑 ...
- POJ3613 Cow Relays [矩阵乘法 floyd类似]
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7335 Accepted: 2878 Descri ...
- poj 3233 矩阵快速幂
地址 http://poj.org/problem?id=3233 大意是n维数组 最多k次方 结果模m的相加和是多少 Given a n × n matrix A and a positive i ...
- poj 3734 Blocks 快速幂+费马小定理+组合数学
题目链接 题意:有一排砖,可以染红蓝绿黄四种不同的颜色,要求红和绿两种颜色砖的个数都是偶数,问一共有多少种方案,结果对10007取余. 题解:刚看这道题第一感觉是组合数学,正向推了一会还没等推出来队友 ...
- poj 3734 矩阵快速幂+YY
题目原意:N个方块排成一列,每个方块可涂成红.蓝.绿.黄.问红方块和绿方块都是偶数的方案的个数. sol:找规律列递推式+矩阵快速幂 设已经染完了i个方块将要染第i+1个方块. a[i]=1-i方块中 ...
随机推荐
- linux usb总线驱动(一)
目录 linux usb总线驱动框架 USB 介绍 传输类型 控制器接口 2440接口 基本流程 alloc_dev choose_address hub_port_init usb_get_devi ...
- nginx第三方库安装以及连接memcache
一.nginx第三方模块的安装 第三方模块查询地址:https://www.nginx.com/resources/wiki/modules/ 后来新出来一个nginx memcache增强版,有空可 ...
- 第十三节: EF的三种模式(三) 之 来自数据库的CodeFirst模式
一. 简介 [来自数据库的Code First模式]实质上并不是CodeFirst模式,而是DBFirst模式的轻量级版本,在该模式中取消了edmx模型和T4模板,直接生成了EF上下文和相应的类,该模 ...
- 深入理解 LINQ to SQL 生成的 SQL 语句
Ø 简介 在 C# 中与数据交互最常用的语句就是 LINQ 了,而 LINQ to SQL 是最直接与数据库打交道的语句,它可以根据 LINQ 语法生成对应的 SQL 语句,在数据库中去执行.本文主 ...
- ssh远程登陆脚本(带跳板机)
mac自带的终端不太好用,被推荐了一个iterm2的终端替代工具,确实比自带的终端好用不少.下面记录下通过脚本一键远程登录的过程: 下载地址:http://m4.pc6.com/xuh3/iTerm2 ...
- python中字符串编码转换
字符串编码转换程序员最苦逼的地方,什么乱码之类的几乎都是由汉字引起的. 其实编码问题很好搞定,只要记住一点: 任何平台的任何编码,都能和Unicode互相转换. UTF-8与GBK互相转换,那就先把U ...
- Ubuntu18.04更换官方默认更新源sources.list
⒈备份官方默认更新源文件 cp /etc/apt/sources.list /etc/apt/sources.list.bak 备份官方更新源文件 ⒉编辑 1.打开 vi /etc/apt/sourc ...
- 题解-CodeChef IOPC14L Sweets Problem
Problem CodeChef-IOPC14L 题目概要:给定 \(n\) 种糖果且给定每种糖果的数量 \(A_i\),\(Q\) 组询问,每次问选出 \(S\) 个糖果的方案数(模\(10^9+7 ...
- C++设计模式——装饰模式
前言 在实际开发时,你有没有碰到过这种问题:开发一个类,封装了一个对象的核心操作,而这些操作就是客户使用该类时都会去调用的操作:而有一些非核心的操作,可能会使用,也可能不会使用:现在该怎么办呢? 将这 ...
- python结合pyvmomi批量关闭vmware虚拟机
#!/usr/bin/env python #参考https://github.com/vmware/pyvmomi/blob/master/sample/poweronvm.py "&qu ...