【题目链接】
             点击打开链接
【算法】
           朴素算法,就是跑N-1遍floyd
           而满分算法就是通过矩阵快速幂加速这个过程
【代码】
          注意要离散一下
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXP 250
#define MAXC 1010
const int INF = 1e9; int N,T,S,E,i,j,l,u,v,tot;
int h[MAXC]; struct Matrix
{
int mat[MAXP][MAXP];
} ans; inline void multipy(Matrix &a,Matrix b)
{
int i,j,k;
static Matrix ans;
for (i = ; i <= tot; i++)
{
for (j = ; j <= tot; j++)
{
ans.mat[i][j] = INF;
}
}
for (i = ; i <= tot; i++)
{
for (j = ; j <= tot; j++)
{
for (k = ; k <= tot; k++)
{
ans.mat[i][j] = min(ans.mat[i][j],a.mat[i][k]+b.mat[k][j]);
}
}
}
a = ans;
} inline void power(Matrix &a,int n)
{
int i,j;
static Matrix ans=a,p=a;
n--;
while (n > )
{
if (n & ) multipy(ans,p);
n >>= ;
multipy(p,p);
}
a = ans;
} int main()
{ for (i = ; i < MAXP; i++)
{
for (j = ; j < MAXP; j++)
{
ans.mat[i][j] = INF;
}
}
scanf("%d%d%d%d",&N,&T,&S,&E);
for (i = ; i <= T; i++)
{
scanf("%d%d%d",&l,&u,&v);
if (!h[u]) h[u] = ++tot;
if (!h[v]) h[v] = ++tot;
ans.mat[h[u]][h[v]]= ans.mat[h[v]][h[u]] = min(ans.mat[h[u]][h[v]],l);
}
power(ans,N);
printf("%d\n",ans.mat[h[S]][h[E]]); return ;
}

【USACO07FEB】 Cow Relays的更多相关文章

  1. 【POJ3613】Cow Relays 离散化+倍增+矩阵乘法

    题目大意:给定一个 N 个顶点,M 条边的无向图,求从起点到终点恰好经过 K 个点的最短路. 题解:设 \(d[1][i][j]\) 表示恰好经过一条边 i,j 两点的最短路,那么有 \(d[r+m] ...

  2. 【题解】Cow Relays

    题目大意   求在一张有\(m\)条边无向连通图中,点\(s\)到点\(t\)的经过\(k\)条边的最短路(\(1 \leq m \leq 100\),\(1 \leq k \leq 10^6\)). ...

  3. 【BZOJ】【1046】/【POJ】【3613】【USACO 2007 Nov】Cow Relays 奶牛接力跑

    倍增+Floyd 题解:http://www.cnblogs.com/lmnx/archive/2012/05/03/2481217.html 神题啊= =Floyd真是博大精深…… 题目大意为求S到 ...

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

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

  5. 【BZOJ-4422】Cow Confinement 线段树 + 扫描线 + 差分 (优化DP)

    4422: [Cerc2015]Cow Confinement Time Limit: 50 Sec  Memory Limit: 512 MBSubmit: 61  Solved: 26[Submi ...

  6. 【POJ3045】Cow Acrobats(贪心)

    BUPT2017 wintertraining(16) #4 B POJ - 3045 题意 n(1 <= N <= 50,000) 个牛,重wi (1 <= W_i <= 1 ...

  7. 【USACO】Cow Brainiacs

    题意描述 Cow Brainiacs 求 \(n!\) 在 \(b\) 进制表示下的第一位非 \(0\) 位的数字. 算法分析 闲话 忙人自动略过 之前做过一道 \(10\) 进制表示下的题目,感觉差 ...

  8. 【poj3270】 Cow Sorting

    http://poj.org/problem?id=3270 (题目链接) 题意 n个数要要按从小到大的顺序排列,每次只能交换任意两个数,交换的代价为这两个数之和,问最小代价. Solution 题目 ...

  9. 【poj3615】 Cow Hurdles

    http://poj.org/problem?id=3615 (题目链接) 题意 给出一张有向图,求从u到v最大边最小的路径的最大边.→_→不会说话了.. Solution 好久没写Floyd了,水一 ...

随机推荐

  1. LeetCode(46)Permutations

    题目 Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the fo ...

  2. 51NOD 2370 奈芙莲的护符

    >>这是原题传送门<< 答案参考来自 http://www.cnblogs.com/sugewud/p/9822933.html 思路:看到取值范围之后,仅有的思路还是暴力

  3. ..net 3.5新特性之用this关键字为类添加扩展方法

    具体用法如下: public static class ClassHelper { //用this 声明将要吧这个方法附加到Student对象 public static bool CheckName ...

  4. 将 Oracle VirtualBox 中运行的虚拟机导入 VMware Fusion、Workstation 或 Player

    1.从virtualbox种导出电脑为 .ova格式镜像 要导入 Oracle VirtualBox 中运行的虚拟机,必须将该虚拟机从 VirtualBox 导出到开放虚拟化格式存档(.ova 文件) ...

  5. HDU-1272小希的迷宫,并查集?其实不用并查集;

    小希的迷宫                                                                                               ...

  6. 关于OPENSSL的EVP函数的使用

    4月份没什么做,就是做了OPENSSL的 加密和解密的应用,现在公开一下如何调用OPENSSL对字符串进行加密和解密,当中也学会了对加密数据进行BASE64编码,现在公开一下代码,在这感谢GITHUB ...

  7. 看板娘 & 二次元 & live2d

    live2d https://l2dwidget.js.org/dev.html https://github.com/xiazeyu/live2d-widget.js 看板娘 要切换看板娘吗? ht ...

  8. 2018/2/16 解析Logback的AppenderBase源码,并举一反三的实现Logback扩展功能的思路,以及它的实际业务应用场景

    在学习Logback的过程中,知道了它有一个可以将日志往第三方数据源写的功能,这个功能的实现就是这个AppenderBase类,不禁想看看它的源码. 下面是AppenderBase类的所有子类(也就是 ...

  9. [NOIP2006] 提高组 洛谷P1066 2^k进制数

    题目描述 设r是个2^k 进制数,并满足以下条件: (1)r至少是个2位的2^k 进制数. (2)作为2^k 进制数,除最后一位外,r的每一位严格小于它右边相邻的那一位. (3)将r转换为2进制数q后 ...

  10. PatentTips - Register file supporting transactional processing

    BACKGROUND OF THE INVENTION With the rise of multi-core, multi-threaded data processing systems, a k ...