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方块中 ...
随机推荐
- ovs之组网实验
介绍 本示例将创建两个OVS实例和两个主机,其中每个OVS上接入一个主机,OVS实例之间有链路连接,形成一个链状拓扑,如图.在OVS组网完成之后,再通过手动方式添加流表,实现网络通信,从而验证实验可行 ...
- day08读取文件
可参考;https://www.cnblogs.com/gengcx/p/6713646.html主要内容: 1.只读 2.只写 3.追加 4.r+读写 5.w+写读 6.a+写读 7.其他一.使用p ...
- centos7修改网卡名称为eth0
原文链接:https://www.cnblogs.com/freeblogs/p/7881597.html 在安装系统的时候配置: 修改内核选项:net.ifnames=0 biosdevname=0 ...
- (BFS) leetcode 279. Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- 关于try catch finally 三者之间的关系(JDK 1.8)
话不多说 线上代码 package System; import java.util.Scanner; /** * * @author chris * */ public class TryCathf ...
- CMDB服务器管理系统【s5day90】:获取今日未采集主机列表
1.目录结构 1.服务器端 2.客户端 2.具体代码如下 1.数据库增加两个字段 class Server(models.Model): """ 服务器信息 " ...
- [简洁]JavaScript中添加、移除、移动、复制、创建和查找节点元素
查找: document.getElementsByTagName通过标签名获取元素,不论有多少个都返回元素集合. document.getElementsByClassName通过类名获取元素,同上 ...
- Leetcode#70. Climbing Stairs(爬楼梯)
题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...
- if判断中的true和false
真值指的是在 布尔值 上下文中转换后(使用强制类型转换)的值为真的值.所有值都是真值,除非它们被定义为 falsy (即除了 false,0,"",null,undefined 和 ...
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or.....
1,<activity android:name=".DialogActivity" android:theme="@android:style/Theme.Dia ...