问题描述

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.

样例输入

2 6 6 4

11 4 6

4 4 8

8 4 9

6 6 8

2 6 9

3 8 9

样例输出

10

题目大意

给出一张无向连通图,求S到E经过k条边的最短路。

解析

倍增Floyd模版题。利用矩阵快速幂的形式可以在\(log\)的时间内处理经过k条路径的最短路。

虽然一共有1000000个点,但是因为只有100条边,可以直接用100条边的端点建图,离散化编号即可。

代码

#include <iostream>
#include <cstdio>
#define int long long
#define N 1000002
using namespace std;
const int inf=1<<30;
struct Matrix{
int a[500][500];
}S;
int n,m,s,t,i,j,id[N],cnt;
int read()
{
char c=getchar();
int w=0;
while(c<'0'||c>'9') c=getchar();
while(c<='9'&&c>='0'){
w=w*10+c-'0';
c=getchar();
}
return w;
}
Matrix mult(Matrix a,Matrix b)
{
Matrix c;
for(int i=1;i<=cnt;i++){
for(int j=1;j<=cnt;j++) c.a[i][j]=inf;
}
for(int k=1;k<=cnt;k++){
for(int i=1;i<=cnt;i++){
for(int j=1;j<=cnt;j++) c.a[i][j]=min(c.a[i][j],a.a[i][k]+b.a[k][j]);
}
}
return c;
}
Matrix poww(Matrix a,int b)
{
b--;
Matrix ans=a,base=a;
while(b){
if(b&1) ans=mult(ans,base);
base=mult(base,base);
b>>=1;
}
return ans;
}
signed main()
{
n=read();m=read();s=read();t=read();
for(i=1;i<=2*m;i++){
for(j=1;j<=2*m;j++) S.a[i][j]=inf;
}
for(i=1;i<=m;i++){
int w=read(),u=read(),v=read();
if(!id[u]) id[u]=++cnt;
if(!id[v]) id[v]=++cnt;
S.a[id[u]][id[v]]=S.a[id[v]][id[u]]=w;
}
Matrix ans=poww(S,n);
cout<<ans.a[id[s]][id[t]]<<endl;
return 0;
}

[洛谷P2886] 牛继电器Cow Relays的更多相关文章

  1. 洛谷 [P2886] 牛继电器Cow Relays

    最短路 + 矩阵快速幂 我们可以改进矩阵快速幂,使得它适合本题 用图的邻接矩阵和快速幂实现 注意 dis[i][i] 不能置为 0 #include <iostream> #include ...

  2. 洛谷P2886牛继电器

    传送门啦 倍增 $ Floyd $ 注意结构体里二维数组不能开到 $ 2000 $ #include <iostream> #include <cstdio> #include ...

  3. P2886 [USACO07NOV]牛继电器Cow Relays

    题目描述 For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race ...

  4. [USACO07NOV]牛继电器Cow Relays (最短路,DP)

    题目链接 Solution 非正解 似乎比较蛇啊,先个一个部分分做法,最短路+\(DP\). 在求最短路的堆或者队列中存储元素 \(dis_{i,j}\) 代表 \(i\) 这个节点,走了 \(j\) ...

  5. 洛谷P2886 [USACO07NOV]牛继电器Cow Relays

    题意很简单,给一张图,把基本的求起点到终点最短路改成求经过k条边的最短路. 求最短路常用的算法是dijkstra,SPFA,还有floyd. 考虑floyd的过程: c[i][j]=min(c[i][ ...

  6. 洛谷 P2886 [USACO07NOV]牛继电器Cow Relays

    题面 解题思路 ## floyd+矩阵快速幂,跟GhostCai爷打赌用不用离散化,最后完败..GhostCai真是tql ! 有个巧妙的方法就是将节点重新编号,因为与节点无关. 代码 #includ ...

  7. [LUOGU] P2886 [USACO07NOV]牛继电器Cow Relays

    https://www.luogu.org/problemnew/show/P2886 给定无向连通图,求经过k条边,s到t的最短路 Floyd形式的矩阵乘法,同样满足结合律,所以可以进行快速幂. 离 ...

  8. luogu题解 P2886 【牛继电器Cow Relays】-经过K边最短路&矩阵

    题目链接: https://www.luogu.org/problemnew/show/P2886 Update 6.16 最近看了下<算法导论>,惊奇地发现在在介绍\(APSP\) \( ...

  9. [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 ...

随机推荐

  1. lazarus 给应用程序创建 配置文件哈哈

    lazarus 给应用程序创建 配置文件哈哈procedure TForm1.Button2Click(Sender: TObject);beginForceDirectoriesUTF8(GetAp ...

  2. 网易云课堂_C++程序设计入门(下)_第7单元:出入虽同趣,所向各有宜 – 文件输入和输出_第7单元 - 作业2:编程互评

    第7单元 - 作业2:编程互评 查看帮助 返回   提交作业(剩余10天) 完成并提交作业     作业批改 互评训练   互评作业   自评作业     成绩公布 查看成绩   由于在线编程不支持 ...

  3. 解决 WordPress 后台加载非常缓慢/打不开问题

    在新版的 WordPress 中,为了后台的美观度,开发者在页面上加入了 Google Web 字体,这本来会让英文显示更加精美.我们只要移除 Google 在线字体即可恢复原来的速度.在你的主题的 ...

  4. 03.大型数据库应用技术课堂测试3(java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;Ljava/lang/Object;)V)

    本次问题主要出在了之前没有安装hive,结构导致大部分时间花在了安装上面,主要一直报错,网上找不到相关教程.

  5. 练习1:python设计停车入库出库系统

    前言: 最近在某个测试群看到有人抛出了一个面试题.为了提升自己的编程能力,我也尝试的用python去写了一下. 语言:python,数据库:sqlite  .菜鸟来袭,只是基本实现功能,可能没有考虑太 ...

  6. cocos2dx基础篇(17) 音乐音效SimpleAudioEngine

    [3.x]     (1)获取单例:sharedEngine() 改为 getInstance()     (2)实现了:音量的调节.     (3)修改了播放音效 playEffect() 的参数: ...

  7. 递归算法之Fibonacci 斐波那契数列第n个数的求解

    Fibonacci 斐波那契数列第n个数的求解,也可以用递归和非递归的形式实现,具体如下,dart语言实现. int fibonacci(int n) { if (n <= 0) throw S ...

  8. 深度学习之美(张玉宏)——第四章 人生苦短我用python

    1 函数参数 (1)收集参数:以一个星号*加上形参名的方式,表示这个函数的实参个数不定,可能0个可能n个. def varParaFun(name,*param): print('位置参数是:',na ...

  9. Ubuntu新建用户以及安装pytorch

    环境:Ubuntu18,Python3.6 首先登录服务器 ssh username@xx.xx.xx.xxx #登录一个已有的username 新建用户 sudo adduser username ...

  10. webservice的一些理解

    web services中如果用.NET的话,DataSet可以作为与客户端交互的一个返回值,因为DataSet实质上是XML.而SOAP WSDL等都是基于XML的. --------------- ...