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方块中 ...
随机推荐
- 关于data()获取不到得原因
..原因很简单,版本高低问题 从jQuery 1.4.3起, HTML 5 data- 属性 将自动被引用到jQuery的数据对象中. 所以,还是尽量保持用attr来获取自定义属性
- MySQL的SQL_Mode修改小计
问题复现 今天突然发现MySQL服务器升级之后sql_mode变成宽松摸索了,危害如下: 临时解决 set global sql_mode='strict_trans_tables'(阿里服务器默认是 ...
- python中的三元运算
一.三元运算符 三元运算符就是在赋值变量的时候,可以直接加判断,然后赋值 格式:[on_true] if [expression] else [on_false] res = 值1 if 条件 els ...
- utf8的大小写敏感性测试及其修改方法
utf8的大小写敏感性测试及其修改方法 # 测试utf8的大小写敏感性及其修改方法 -- 以下是utf8不区分大小写 # 修改数据库: ALTER DATABASE database_name CHA ...
- EF CodeFirst系列(1)---CodeFirst简单入门
1.什么是CodeFirst 从EF4.1开始,EF可以支持CodeFirst开发模式,这种开发模式特别适用于领域驱动设计(Domain Driven Design,大名鼎鼎的DDD).在CodeFi ...
- [Android] Android GreenDao 保存 JavaBean 或者List <JavaBean>类型数据
Android GreenDao 保存 JavaBean 或者List <JavaBean>类型数据 简介 数据库存储数据基本上每个APP都有用到,GreenDAO 是一个将对象映射到 S ...
- Geometric regularity criterion for NSE: the cross product of velocity and vorticity 3: $u\times \f{\om}{|\om|}\cdot \f{\vLm^\be u}{|\vLm^\be u|}$
在 [Chae, Dongho; Lee, Jihoon. On the geometric regularity conditions for the 3D Navier-Stokes equati ...
- [再寄小读者之数学篇](2014-06-23 积分不等式 [中国科学技术大学2013年高等数学B 考研试题])
设 $f(x)$ 在 $[a,b]$ 上一阶连续可导, $f(a)=0$. 证明: $$\bex \int_a^b f^2(x)\rd x\leq \cfrac{(b-a)^2}{2}\int_a^b ...
- [再寄小读者之数学篇](2014-06-20 求极限-H\"older 不等式的应用)
设非负严格增加函数 $f$ 在区间 $[a,b]$ 上连续, 有积分中值定理, 对于每个 $p>0$ 存在唯一的 $x_p\in (a,b)$, 使 $$\bex f^p(x_p)=\cfrac ...
- 多人聊天室(Java)
第1部分 TCP和UDP TCP:是一种可靠地传输协议,是把消息按一个个小包传递并确认消息接收成功和正确才发送下一个包,速度相对于UDP慢,但是信息准确安全:常用于一般不要求速度和需要准确发送消息的场 ...