POJ 3631 Cow Relays 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.
输入格式
* 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
输出格式
* 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
分析
一句话题意:给定一个T(2 <= T <= 100)
条边的无向图,求S
到E
恰好经过N(2 <= N <= 1000000)
条边的最短路。
这种类型的题之前已经有人分享过了,感觉没什么好说的,就是矩阵快速幂+Floyd
需要注意的就是初始化
代码
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
const int maxn=220;
typedef long long ll;
ll n,t,s,e,cnt;
map<ll,ll> mp;
struct asd{
ll jz[maxn][maxn];
asd(){
for(ll i=0;i<maxn;i++){
for(ll j=0;j<maxn;j++){
jz[i][j]=0x3f3f3f3f;
}
}
}
};
asd a1,a2;
asd cheng(asd xx,asd yy){
asd zz;
for(ll k=1;k<=cnt;k++){
for(ll i=1;i<=cnt;i++){
for(ll j=1;j<=cnt;j++){
zz.jz[i][j]=min(zz.jz[i][j],xx.jz[i][k]+yy.jz[k][j]);
}
}
}
return zz;
}
void solve(ll xx){
a2=a1;
xx--;
while(xx){
if(xx&1) a2=cheng(a1,a2);
a1=cheng(a1,a1);
xx>>=1;
}
}
int main(){
scanf("%lld%lld%lld%lld",&n,&t,&s,&e);
for(ll i=1;i<=t;i++){
ll w,aa,bb;
scanf("%lld%lld%lld",&w,&aa,&bb);
if(!mp[aa]) mp[aa]=++cnt;
if(!mp[bb]) mp[bb]=++cnt;
a1.jz[mp[aa]][mp[bb]]=w;
a1.jz[mp[bb]][mp[aa]]=w;
}
solve(n);
printf("%lld\n",a2.jz[mp[s]][mp[e]]);
return 0;
}
POJ 3631 Cow Relays Floyd+矩阵快速幂的更多相关文章
- poj 3613 Cow Relays【矩阵快速幂+Floyd】
!:自环也算一条路径 矩阵快速幂,把矩阵乘法的部分替换成Floyd(只用一个点扩张),这样每"乘"一次,就是经过增加一条边的最短路,用矩阵快速幂优化,然后因为边数是100级别的,所 ...
- [POJ3613] Cow Relays(Floyd+矩阵快速幂)
解题报告 感觉这道题gyz大佬以前好像讲过一道差不多的?然鹅我这个蒟蒻发现矩阵快速幂已经全被我还给老师了...又恶补了一遍,真是恶臭啊. 题意 给定一个T(2 <= T <= 100)条边 ...
- POJ3613 Cow Relays(矩阵快速幂)
题目大概要求从起点到终点恰好经过k条边的最短路. 离散数学告诉我们邻接矩阵的k次幂就能得出恰好经过k条路的信息,比如POJ2778. 这题也一样,矩阵的幂运算定义成min,而min满足结合律,所以可以 ...
- poj 2888 Magic Bracelet(Polya+矩阵快速幂)
Magic Bracelet Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 4990 Accepted: 1610 D ...
- poj 3613 经过k条边最短路 floyd+矩阵快速幂
http://poj.org/problem?id=3613 s->t上经过k条边的最短路 先把1000范围的点离散化到200中,然后使用最短路可以使用floyd,由于求的是经过k条路的最短路, ...
- poj 3735 Training little cats 矩阵快速幂+稀疏矩阵乘法优化
题目链接 题意:有n个猫,开始的时候每个猫都没有坚果,进行k次操作,g x表示给第x个猫一个坚果,e x表示第x个猫吃掉所有坚果,s x y表示第x个猫和第y个猫交换所有坚果,将k次操作重复进行m轮, ...
- POJ 3233 Matrix Power Series 矩阵快速幂+二分求和
矩阵快速幂,请参照模板 http://www.cnblogs.com/pach/p/5978475.html 直接sum=A+A2+A3...+Ak这样累加肯定会超时,但是 sum=A+A2+...+ ...
- POJ 3233 Matrix Power Series 矩阵快速幂
设S[k] = A + A^2 +````+A^k. 设矩阵T = A[1] 0 E E 这里的E为n*n单位方阵,0为n*n方阵 令A[k] = A ^ k 矩阵B[k] = A[k+1] S[k] ...
- foj 2173 floyd+矩阵快速幂
Problem 2173 Nostop Accept: 52 Submit: 210 Time Limit: 3000 mSec Memory Limit : 32768 KB Pro ...
随机推荐
- Pi-star MMDVM双工板介绍
Pi-star MMDVM双工板介绍(2020/2) pi-star里控制模式选择:双工模式(DUPLEX Mode)/单工模式(SIMPLE Mode) 双工板工作频率范围:144-148,219- ...
- ASP.NET Core Blazor Webassembly 之 路由
web最精妙的设计就是通过url把多个页面串联起来,并且可以互相跳转.我们开发系统的时候总是需要使用路由来实现页面间的跳转.传统的web开发主要是使用a标签或者是服务端redirect来跳转.那今天来 ...
- intput子系统
1.按键驱动程序的第一个版本:day07/04 //内核模块的基本要求 init.h module.h LICENSE struct cdev btn_cdev; b ...
- <Android> Location Service 分析
由于各种原因,老师希望我学习Android系统源码以应对可能参与的项目.我只好深入曹营,刺探军情了. 定位服务是手机上最常用的功能之一,据说也是相对比较简单的服务,所以从这里入手.其他系统服务的架构都 ...
- EAT表
0X0 EAT表 在windows系统中,"库"是为了方便其他程序调用而集中包含相关的函数的文件(dll,sys).win32 API是最具有代表性的库. EAT是一种核心机制,它 ...
- 基于session对象实现简单的购物车应用
大部分购物网站都会实现购物车的功能,基于session对象的购物车应用可以实现一个用户会话有效期内,用户所选多个商品的存储. 为了实现这样的功能需要编写三个JSP页面,分别是login.jsp.mai ...
- PyQt5中QTableView函数讲解
如果想熟悉QTableWidget,请参考PyQt5高级界面控件之QTableWidget(四) setSpan(int, int, int, int)四个参数分别代表,起始行,列,合并的行数,全并的 ...
- XAI/MLI 可解释机器学习系列1- 开源&paper汇总
一直在关注可解释机器学习领域,因为确实在工作中有许多应用 模型检查,特征重要性是否符合预期和AUC一样重要 模型解释,比起虚无缥缈的模型指标,解释模型学到的规律更能说服业务方 样本解释,为什么这些用户 ...
- 命令中"|"的意义
管道命令,是指 | 的左边运行结果是|右边的输入条件或者范围.如:history | grep date指从history这条命令运行的结果中显示包含有 “date” 的命令 下面举一个例子: 这是运 ...
- 解决Mac中anaconda作图中文异常显示的问题
说明 本篇主要针对在MAC系统中Anaconda环境下,matplotlib显示不了中文的问题,提出解决Python绘图时中文显示的方法. 运行环境 macOS Mojave 10.14.6 Pyth ...