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 ...
随机推荐
- Mybatis连接池及事务
一:Mybatis连接池 我们在学习WEB技术的时候肯定接触过许多连接池,比如C3P0.dbcp.druid,但是我们今天说的mybatis中也有连接池技术,可是它采用的是自己内部实现了一个连接池技术 ...
- Java重置Mysql主键自增长值
MySql 主键自增重置器(统一处理多个表) resetAutoincrement 是一款基于 Java 开发的程序,其功能为重置 mysql 数据库表的主键自增的值为最近的一个. 介绍 开发背景主要 ...
- [julia][学习笔记]julia的安装
目录 julia的安装 下载地址 下载后安装 IDE Julia Pro 安装方法 使用方法(以hello world为例) vscode的Julia插件 下载vscode 安装vscode Juli ...
- 时间序列神器之争:prophet VS lstm
一.需求背景 我们福禄网络致力于为广大用户提供智能化充值服务,包括各类通信充值卡(比如移动.联通.电信的话费及流量充值).游戏类充值卡(比如王者荣耀.吃鸡类点券.AppleStore充值.Q币.斗鱼币 ...
- 线程安全与synchronized
线程安全性与synchronized 线程安全:多线程访问某个类时,这个类始终都能表现出正确的行为,这个类就是线程安全的. 简单的说,就是多线程执行的结果与单线程执行的结果始终一致,不会因为多线程的执 ...
- 列表、元组、字典和简单if语句【python实验1】
第一次实验报告: 学生姓名 总成绩 tom 90 jack 89 john 96 kate 86 peter 100 实验内容3-1 建立两个列表分别对学生的姓名和总成绩信息进行存储 name=['t ...
- controller介绍
Loadrunner Controller可以使用Loadrunner Controller管理和维护方案可以从一个单一的控制点简单有效的控制所有的Vuser 承担着多种工作任务.最常见的就是场景的设 ...
- 腾讯音乐Android工程师一面面试题记录,拿走不谢!
最近参加了一次鹅厂音乐Android工程师面试,这里凭记忆记录了一些一面的面试题,希望能帮到正在面试的你! 1.Java调用函数传入实际参数时,是值传递还是引用传递? 2.单例模式的DCL方式,为什么 ...
- 吃货联盟订餐系统 源代码 Java初级小项目
咳咳,今天博主给大家写一个小的项目:吃货联盟订餐系统.博主不是大神(互联网架构师的路上ing),也是小白一个,不过是刚入门的小白^_^.项目功能也很简单:只是模拟日常的订餐流程呦,所以有错误以及功能不 ...
- Python3-configparser模块-配置文件解析器
Python3中的configparser模块主要用于处理类似于windows ini 文件结构的配置文件 1.configparser模块提供实现基本配置语言的ConfigParser类 2.配置文 ...