POJ3613 Cow Relays [矩阵乘法 floyd类似]
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7335 | Accepted: 2878 |
Description
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.
Input
* 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
Output
* 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
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N=;
typedef long long ll;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int b,n,m,s,t,u,v,w;
int mp[];
struct Mat{
int a[N][N];
Mat(){memset(a,0x3f,sizeof(a));}
}G,ans;
Mat operator *(Mat A,Mat B){
Mat C;
for(int k=;k<=n;k++)
for(int i=;i<=n;i++) if(A.a[i][k])
for(int j=;j<=n;j++) if(B.a[k][j])
C.a[i][j]=min(C.a[i][j],A.a[i][k]+B.a[k][j]);
return C;
}
int main(){
//freopen("in.txt","r",stdin);
b=read();m=read();s=read();t=read();
if(!mp[s]) mp[s]=++n;s=mp[s];
if(!mp[t]) mp[t]=++n;t=mp[t];
for(int i=;i<=m;i++){
w=read();u=read();v=read();
if(!mp[u]) mp[u]=++n;u=mp[u];
if(!mp[v]) mp[v]=++n;v=mp[v];
G.a[u][v]=G.a[v][u]=w;
}
ans=G;b--;
for(;b;b>>=,G=G*G)
if(b&) ans=ans*G;
printf("%d",ans.a[s][t]);
}
POJ3613 Cow Relays [矩阵乘法 floyd类似]的更多相关文章
- poj3613:Cow Relays(倍增优化+矩阵乘法floyd+快速幂)
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7825 Accepted: 3068 Descri ...
- [POJ3613] Cow Relays(Floyd+矩阵快速幂)
解题报告 感觉这道题gyz大佬以前好像讲过一道差不多的?然鹅我这个蒟蒻发现矩阵快速幂已经全被我还给老师了...又恶补了一遍,真是恶臭啊. 题意 给定一个T(2 <= T <= 100)条边 ...
- 【POJ3613 Cow Relays】(广义矩阵乘法)
题目链接 先离散化,假设有\(P\)个点 定义矩阵\(A_{ij}\)表示\(i\)到\(j\)只经过一条边的最短路,\[{(A^{a+b})_{ij}=\min_{1\le k\le p} \{ ( ...
- 疯子的算法总结(九) 图论中的矩阵应用 Part 1+POJ3613 Cow Relays
图的存储有邻接矩阵,那么他就具备一些矩阵的性质,设有一个图的demo[100][100];那么demo[M][N]就是M—>N的距离,若经过一次松弛操作demo[M][N]=demo[M][K] ...
- poj3613 Cow Relays【好题】【最短路】【快速幂】
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions:9207 Accepted: 3604 Descrip ...
- bzoj 1706: [usaco2007 Nov]relays 奶牛接力跑【矩阵乘法+Floyd】
唔不知道怎么说--大概核心是把矩阵快速幂的乘法部分变成了Floyd一样的东西,非常之神 首先把点离散一下,最多有200个,然后建立邻接矩阵,a[u][v]为(u,v)之间的距离,没路就是inf 然后注 ...
- POJ3613 Cow Relays(矩阵快速幂)
题目大概要求从起点到终点恰好经过k条边的最短路. 离散数学告诉我们邻接矩阵的k次幂就能得出恰好经过k条路的信息,比如POJ2778. 这题也一样,矩阵的幂运算定义成min,而min满足结合律,所以可以 ...
- hdu2807 矩阵乘法+floyd
网上有优化的方法 就是乘上一个一维的矩阵:现在还没有想通.想通了不上代码: 我用的就是普通的矩阵,压着时间过:只是多了一个判断条件,不加这个条件就超时: #include<stdio.h> ...
- [POJ3613] Cow Relays
link 题目大意 给你一个含有边权的无向图,问从$S$到$T$经过$N$条边的最小花费. 试题分析 我们可以很容易推导$dp$方程,$dp(k,i,j)$表示经过$k$条边从$i$到$j$的最小花费 ...
随机推荐
- 汽车OBD2诊断程序开发 (原文转载,思路很清晰!)
1.因TL718已经为你建立了物理层.数据链层和部分应用层的协议,所以只要OBD2标准应用层协议文本,ISO15031-5 或 SAE J1979(这两个协议是相同的内容). 2.TL718诊断 ...
- Swift 结构体和类
//*---------------------结构体-------------*/ //swift结构体也具有封装的概念 //swift结构体比OC C语言里面的结构体更加进了一步,里面可以有方法, ...
- linux语言环境设置
查看linux的支持的语言集合 执行locale命令 LANG=zh_CN.UTF-8 LANGUAGE=zh_CN:zh LC_CTYPE="zh_CN.UTF-8" LC_NU ...
- 纯CSS实现三列布局(两边固定,中间自适应)
看了一些网上的案例,感觉较繁杂,于是,自己整理了一篇来说明这个东西. 也是给我自己复习吧,以前有人问道,我还没答上来呢.== 看代码: html: <div class="top&qu ...
- Python(2.7.6) 列表推导式
列表推导式是利用已有的列表导出新的列表,它的工作方式类似于 for 循环. 例如,有一个列表,现在想得到一个对应的列表,使得每个元素是原有列表中元素的平方: >>> [x ** 2 ...
- 快速了解Scala技术栈
http://www.infoq.com/cn/articles/scala-technology/ 我无可救药地成为了Scala的超级粉丝.在我使用Scala开发项目以及编写框架后,它就仿佛凝聚成为 ...
- hhgis驱动
function loadPathInfo 在car.js中 function loadPathInfo(carid, fnClass) { var qureydrive; if (jsonp) qu ...
- JAVA访问配置文件总结
一.全局配置的简单 propertie 文件实现 package com.testgs.utils; import java.util.*; import java.io.*; public fina ...
- 一个layer可以跟着画完的线移动ios程序 好玩啊。
用法:采用的是关键帧实现的. 实验目的:让上层的layer子层能够跟着在另一个子层上花的线进行移动 .即当线画完之后,图形开始移动,并且能够停在最后的那个位置 效果图: 采用是直接在layer图层上进 ...
- C# ACM poj1007
求逆序数,快排 public static void acm1007(int a, string[] c) { Dictionary<int, string> dic = new Dict ...