【USACO07FEB】 Cow Relays
#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的更多相关文章
- 【POJ3613】Cow Relays 离散化+倍增+矩阵乘法
题目大意:给定一个 N 个顶点,M 条边的无向图,求从起点到终点恰好经过 K 个点的最短路. 题解:设 \(d[1][i][j]\) 表示恰好经过一条边 i,j 两点的最短路,那么有 \(d[r+m] ...
- 【题解】Cow Relays
题目大意 求在一张有\(m\)条边无向连通图中,点\(s\)到点\(t\)的经过\(k\)条边的最短路(\(1 \leq m \leq 100\),\(1 \leq k \leq 10^6\)). ...
- 【BZOJ】【1046】/【POJ】【3613】【USACO 2007 Nov】Cow Relays 奶牛接力跑
倍增+Floyd 题解:http://www.cnblogs.com/lmnx/archive/2012/05/03/2481217.html 神题啊= =Floyd真是博大精深…… 题目大意为求S到 ...
- luogu题解 P2886 【牛继电器Cow Relays】-经过K边最短路&矩阵
题目链接: https://www.luogu.org/problemnew/show/P2886 Update 6.16 最近看了下<算法导论>,惊奇地发现在在介绍\(APSP\) \( ...
- 【BZOJ-4422】Cow Confinement 线段树 + 扫描线 + 差分 (优化DP)
4422: [Cerc2015]Cow Confinement Time Limit: 50 Sec Memory Limit: 512 MBSubmit: 61 Solved: 26[Submi ...
- 【POJ3045】Cow Acrobats(贪心)
BUPT2017 wintertraining(16) #4 B POJ - 3045 题意 n(1 <= N <= 50,000) 个牛,重wi (1 <= W_i <= 1 ...
- 【USACO】Cow Brainiacs
题意描述 Cow Brainiacs 求 \(n!\) 在 \(b\) 进制表示下的第一位非 \(0\) 位的数字. 算法分析 闲话 忙人自动略过 之前做过一道 \(10\) 进制表示下的题目,感觉差 ...
- 【poj3270】 Cow Sorting
http://poj.org/problem?id=3270 (题目链接) 题意 n个数要要按从小到大的顺序排列,每次只能交换任意两个数,交换的代价为这两个数之和,问最小代价. Solution 题目 ...
- 【poj3615】 Cow Hurdles
http://poj.org/problem?id=3615 (题目链接) 题意 给出一张有向图,求从u到v最大边最小的路径的最大边.→_→不会说话了.. Solution 好久没写Floyd了,水一 ...
随机推荐
- win10下硬盘安装CentOS7
安装环境: 1.系统:Windows 10 2.硬盘:SSD(已装好Win 10) + HHD(用来装CentOS 7) 准备工作: 1.DiskGenius(分区工具):用来给硬盘做分区: 2.系统 ...
- ajax 简单学习
客户端代码function login(type) { $.ajax({ type: "post", url: "logindo.aspx", data: { ...
- 细说php第八章笔记(初稿)
8.1 函数的定义 函数是被命名的: 函数是独立的: 函数执行特定的任务: 函数可以用将一个返回值返回给调用他的程序 函数的优越性 提高程序的重用性 ...
- java连接mysql数据库中文乱码问题
private static final String URL="jdbc:mysql://localhost:3306/ziye?useUnicode=true&character ...
- SPOJ GSS6 Can you answer these queries VI
Can you answer these queries VI Time Limit: 2000ms Memory Limit: 262144KB This problem will be judge ...
- linux & command line & console & logs
linux & command line & console & logs how to get the logs form linux command console htt ...
- Linux下汇编语言学习笔记57 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- Linux下汇编语言学习笔记56 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- jquery判断单选按钮radio是否选中的方法
JQuery控制radio选中和不选中方法总结 一.设置选中方法 复制代码代码如下: $("input[name='名字']").get(0).checked=true; $(&q ...
- 创建Django项目(六)——模板
2013-08-07 22:42:30| 1.设置模板路径 打开 settings.py 文件,修改 TEMPLATE_DIRS 内容,指向模板存放的绝对路径,而不 ...