P2886 [USACO07NOV]牛继电器Cow Relays
题目描述
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.
给出一张无向连通图,求S到E经过k条边的最短路。
输入输出格式
输入格式:
* 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.
输入输出样例
2 6 6 4
11 4 6
4 4 8
8 4 9
6 6 8
2 6 9
3 8 9
10 题解:
一句话题意:给出一个有t条边的图,求从s到e恰好经过k条边的最短路。
a:是图的邻接矩阵,f是图中任意两点直接的最短距离、 floyd算法:
f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
一遍floyed后: f[i][j]是i到j的最短距离:中间至少1条边(连通图),最多n-1条边
floyd算法的变形:
a:是图的邻接矩阵,a[i][j]是经过一条边的最短路径。f[i][j]k的初值为∞
f[i][j]-1=∞;
f[i][j]1=a; //经过一条边的最短路径
f[i][j]2=min(f[i][j]2,a[i][k]+a[k][j])=a*a=a2;//经过二条边的最短路径,经过一次floyd.矩阵相乘一次,f[i][j]2初值为∞。
f[i][j]3=min(f[i][j]3,f[i][k]2+a[k][j])=a*a*a=a3;//经过三条边的最短路径,经过二次floyd。f[i][j]3初值为∞
f[i][j]4=min(f[i][j]4,f[i][k]3+a[k][j])=a4;//经过四条边的最短路径,经过三次floyd,f[i][j]4初值为∞
...
f[i][j]k=min(f[i][j]k,f[i][k]k-1+a[k][j])=ak;//经过k条边的最短路径,经过K-1次floyd,f[i][j]k初值为∞ 而floyd的时间复杂度为O(n3),则从的时间复杂度为O(Kn3),非常容易超时。
所以我们可以用快速幂来完成。
f[i][j]r+p=min(f[i][j]r+p,f[i][k]r+f[k][j]p)
程序:
//洛谷2886
//(1)对角线不能设置为0,否则容易自循环。(2)数组要放在主程序外面
//(3)K条边,不一定是最简路
#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
using namespace std;
map<int,int>f;
const int maxn=;
int k,t,s,e,n;
int a[maxn][maxn];
struct Matrix{
int b[maxn][maxn];
};
Matrix A,S;
Matrix operator *(Matrix A,Matrix B){//运算符重载
Matrix c;
memset(c.b,/,sizeof(c.b) );
for(int k=;k<=n;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
c.b[i][j]=min(c.b[i][j],A.b[i][k]+B.b[k][j]);
return c;
}
Matrix power(Matrix A,int k){
if(k==) return A;
Matrix S=A;
for(int i=;i<=n;i++){
for (int j=;j<=n;j++)
cout<<A.b[i][j]<<" ";
cout<<endl;
}
cout<<endl;
while(k){
if(k&)S=S*A;//奇数执行,偶数不执行
/*cout<<k<<":"<<endl;
for(int i=1;i<=n;i++){
for (int j=1;j<=n;j++)
cout<<S.b[i][j]<<" ";
cout<<endl;
}*/
cout<<endl;
A=A*A;
k=k>>; }
return S;
}
int main(){
cin>>k>>t>>s>>e;
int w,x,y;
memset(A.b,/,sizeof(A.b) );// 赋初值
n=;
for(int i=;i<=t;i++){//t<100,x<1000 点不是从1开始的,可以用map离散化,给点从1开始编号。n记录共有多少个点。
cin>>w>>x>>y;
if (f[x]==) f[x]=++n;
if (f[y]==) f[y]=++n;
A.b[f[x]][f[y]]=A.b[f[y]][f[x]]=min(w,A.b[f[y]][f[x]]);
}
S=power(A,k-);//快速幂.执行k-1次floyd矩阵乘
s=f[s];
e=f[e]; cout<<S.b[s][e];
return ;
}
P2886 [USACO07NOV]牛继电器Cow Relays的更多相关文章
- [LUOGU] P2886 [USACO07NOV]牛继电器Cow Relays
https://www.luogu.org/problemnew/show/P2886 给定无向连通图,求经过k条边,s到t的最短路 Floyd形式的矩阵乘法,同样满足结合律,所以可以进行快速幂. 离 ...
- 洛谷P2886 [USACO07NOV]牛继电器Cow Relays
题意很简单,给一张图,把基本的求起点到终点最短路改成求经过k条边的最短路. 求最短路常用的算法是dijkstra,SPFA,还有floyd. 考虑floyd的过程: c[i][j]=min(c[i][ ...
- 洛谷 P2886 [USACO07NOV]牛继电器Cow Relays
题面 解题思路 ## floyd+矩阵快速幂,跟GhostCai爷打赌用不用离散化,最后完败..GhostCai真是tql ! 有个巧妙的方法就是将节点重新编号,因为与节点无关. 代码 #includ ...
- luogu题解 P2886 【牛继电器Cow Relays】-经过K边最短路&矩阵
题目链接: https://www.luogu.org/problemnew/show/P2886 Update 6.16 最近看了下<算法导论>,惊奇地发现在在介绍\(APSP\) \( ...
- [USACO07NOV]牛继电器Cow Relays
题目描述 给出一张无向连通图,求S到E经过k条边的最短路. 输入输出样例 输入样例#1: 2 6 6 4 11 4 6 4 4 8 8 4 9 6 6 8 2 6 9 3 8 9 输出样例#1: 10 ...
- Luogu 2886 [USACO07NOV]牛继电器Cow Relays
BZOJ 1706权限题. 倍增$floyd$. 首先这道题有用的点最多只有$200$个,先离散化. 设$f_{p, i, j}$表示经过$2^p$条边从$i$到$j$的最短路,那么有转移$f_{p, ...
- [USACO07NOV]牛继电器Cow Relays (最短路,DP)
题目链接 Solution 非正解 似乎比较蛇啊,先个一个部分分做法,最短路+\(DP\). 在求最短路的堆或者队列中存储元素 \(dis_{i,j}\) 代表 \(i\) 这个节点,走了 \(j\) ...
- [luoguP2886] [USACO07NOV]牛继电器Cow Relays(矩阵)
传送门 矩阵快速幂,本质是floyd 把 * 改成 + 即可 注意初始化 因为只有100条边,所以可以离散化 #include <cstdio> #include <cstring& ...
- [洛谷P2886] 牛继电器Cow Relays
问题描述 For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race ...
随机推荐
- linux下dpkg繁忙,解决方法
安装软件,提示: E: 无法获得锁 /var/lib/dpkg/lock - open (11: 资源暂时不可用)E: 无法锁定管理目录(/var/lib/dpkg/),是否有其他进程正占用它? 只要 ...
- kotlin 委托
委托模式是软件设计模式中的一项基本技巧.在委托模式中,有两个对象参与处理同一个请求,接受请求的对象将请求委托给另一个对象来处理. Kotlin 直接支持委托模式,更加优雅,简洁.Kotlin 通过关键 ...
- linux生成公钥私钥并上传到服务器上实现免密登陆
1. 生成密钥对 # -t 指定加密算法: -b 指定生成的密钥长度: -C 一句话,一般都填邮箱地址. # 更多参数说明可以在终端输入:ssh-keygen --help 查看 ssh-keygen ...
- jemter分布式部署及linux下分布式脚本执行
jmeter进行接口性能测试,占用内存较大,在模拟千万计并发用户时,使用分布式部署进行分压测试. 操作步骤:选择一台机器作为调度机,其他机器作为执行机 一.jmeter分布式部署 前提条件:A.执行机 ...
- linux所有命令不能用显示-bash: ls: command not found
所有的命令都显示找不到了,原因是修改了/etc/profile造成的 解决方法 1.修正属性文件中的错误 /usr/bin/vi /etc/profile 2.生效属性文件 source /etc/p ...
- max of 直线划平面
在一个无限延伸平面上有一个圆和n条直线,这些直线中每一条都在一个圆内,并且同其他所有的直线相交,假设没有3条直线相交于一点,试问这些直线最多将圆分成多少区域. Input 第一行包含一个整数T,(0& ...
- This application failed to start because it could not find or load the Qt platform plugin异常
双击项目Release文件夹下的exe程序无法启动: 解决办法: 1.将用到的QT组件拷贝到程序目录: 2.将D:\Qt\Qt5.3.2\5.3\msvc2013_64_opengl\plugins目 ...
- angular学习3
#创建了一个component 查看angular.json文件: "prefix":"app", 在所创建的component的selector上添加了app ...
- SpringBoot是什么,可以做什么?
SpringBoot简析 1.SpringBoot是什么? 在Spring框架这个大家族中,产生了很多衍生框架,比如 Spring.SpringMvc框架等,Spring的核心内容在于控制反转( ...
- 总结const、readonly、static三者的区别
const:静态常量,也称编译时常量(compile-time constants),属于类型级,通过类名直接访问,被所有对象共享! a.叫编译时常量的原因是它编译时会将其替换为所对应的值: b.静态 ...