Cow Relays
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: NTS, 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

  1. 2 6 6 4
  2. 11 4 6
  3. 4 4 8
  4. 8 4 9
  5. 6 6 8
  6. 2 6 9
  7. 3 8 9

Sample Output

  1. 10

Source


b在大很多也可以
求经过b条边的最短路
貌似直接想floyd不太明白了,一遍floyd怎么是经过一条边的最短路呢?
还是从矩阵乘法考虑,构造一个向量表示距离,不停乘邻接矩阵
定义一种新矩阵乘法,不是加起来而是求最小值
 
初始化时用0x3f
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. using namespace std;
  7. const int N=;
  8. typedef long long ll;
  9. inline int read(){
  10. char c=getchar();int x=,f=;
  11. while(c<''||c>''){if(c=='-')f=-;c=getchar();}
  12. while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
  13. return x*f;
  14. }
  15. int b,n,m,s,t,u,v,w;
  16. int mp[];
  17. struct Mat{
  18. int a[N][N];
  19. Mat(){memset(a,0x3f,sizeof(a));}
  20. }G,ans;
  21. Mat operator *(Mat A,Mat B){
  22. Mat C;
  23. for(int k=;k<=n;k++)
  24. for(int i=;i<=n;i++) if(A.a[i][k])
  25. for(int j=;j<=n;j++) if(B.a[k][j])
  26. C.a[i][j]=min(C.a[i][j],A.a[i][k]+B.a[k][j]);
  27. return C;
  28. }
  29. int main(){
  30. //freopen("in.txt","r",stdin);
  31. b=read();m=read();s=read();t=read();
  32. if(!mp[s]) mp[s]=++n;s=mp[s];
  33. if(!mp[t]) mp[t]=++n;t=mp[t];
  34. for(int i=;i<=m;i++){
  35. w=read();u=read();v=read();
  36. if(!mp[u]) mp[u]=++n;u=mp[u];
  37. if(!mp[v]) mp[v]=++n;v=mp[v];
  38. G.a[u][v]=G.a[v][u]=w;
  39. }
  40. ans=G;b--;
  41. for(;b;b>>=,G=G*G)
  42. if(b&) ans=ans*G;
  43. printf("%d",ans.a[s][t]);
  44. }
 
 

POJ3613 Cow Relays [矩阵乘法 floyd类似]的更多相关文章

  1. poj3613:Cow Relays(倍增优化+矩阵乘法floyd+快速幂)

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7825   Accepted: 3068 Descri ...

  2. [POJ3613] Cow Relays(Floyd+矩阵快速幂)

    解题报告 感觉这道题gyz大佬以前好像讲过一道差不多的?然鹅我这个蒟蒻发现矩阵快速幂已经全被我还给老师了...又恶补了一遍,真是恶臭啊. 题意 给定一个T(2 <= T <= 100)条边 ...

  3. 【POJ3613 Cow Relays】(广义矩阵乘法)

    题目链接 先离散化,假设有\(P\)个点 定义矩阵\(A_{ij}\)表示\(i\)到\(j\)只经过一条边的最短路,\[{(A^{a+b})_{ij}=\min_{1\le k\le p} \{ ( ...

  4. 疯子的算法总结(九) 图论中的矩阵应用 Part 1+POJ3613 Cow Relays

    图的存储有邻接矩阵,那么他就具备一些矩阵的性质,设有一个图的demo[100][100];那么demo[M][N]就是M—>N的距离,若经过一次松弛操作demo[M][N]=demo[M][K] ...

  5. poj3613 Cow Relays【好题】【最短路】【快速幂】

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:9207   Accepted: 3604 Descrip ...

  6. bzoj 1706: [usaco2007 Nov]relays 奶牛接力跑【矩阵乘法+Floyd】

    唔不知道怎么说--大概核心是把矩阵快速幂的乘法部分变成了Floyd一样的东西,非常之神 首先把点离散一下,最多有200个,然后建立邻接矩阵,a[u][v]为(u,v)之间的距离,没路就是inf 然后注 ...

  7. POJ3613 Cow Relays(矩阵快速幂)

    题目大概要求从起点到终点恰好经过k条边的最短路. 离散数学告诉我们邻接矩阵的k次幂就能得出恰好经过k条路的信息,比如POJ2778. 这题也一样,矩阵的幂运算定义成min,而min满足结合律,所以可以 ...

  8. hdu2807 矩阵乘法+floyd

    网上有优化的方法 就是乘上一个一维的矩阵:现在还没有想通.想通了不上代码: 我用的就是普通的矩阵,压着时间过:只是多了一个判断条件,不加这个条件就超时: #include<stdio.h> ...

  9. [POJ3613] Cow Relays

    link 题目大意 给你一个含有边权的无向图,问从$S$到$T$经过$N$条边的最小花费. 试题分析 我们可以很容易推导$dp$方程,$dp(k,i,j)$表示经过$k$条边从$i$到$j$的最小花费 ...

随机推荐

  1. h5拖放-基础知识

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  2. CSS内容

    选择器 框模型 背景和边框 文本效果 2D/3D 转换 动画 多列布局 用户界面 CSS3 边框 border-radius box-shadow border-image

  3. NGINX server配置中if的用法

    server的配置以php为例,如下: 1 server{ 2 root /var/webproject/www/mytools-php; 3 index index.html index.php; ...

  4. (转)CentOS 6.5下Redis安装详细步骤

    Redis简介:Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工作 ...

  5. lshw 命令(查看硬件信息)

    帮助 $ lshw -h Hardware Lister (lshw) - B.02.16 usage: lshw [-format] [-options ...] lshw -version -ve ...

  6. epoll讲解--转自”知乎“

    http://my.oschina.net/dclink/blog/287198 首先我们来定义流的概念,一个流可以是文件,socket,pipe等等可以进行I/O操作的内核对象. 不管是文件,还是套 ...

  7. Python(2.7.6) 迭代器

    除了对列表.集合和字典等进行迭代,还能对其他对象进行迭代:实现 __iter__ 方法的对象.例如, 文件对象就是可迭代的: >>> dir(file) ['__class__', ...

  8. Android 百度地图开发之一(Hello BaiDu Map)

    之前也接触过百度地图的开发,但那是在网上找的案例或代码,而且是比较老的版本.打算重新学习一下百度地图的开发. 本次使用的百度地图的版本是 Android SDK v3.0.0 本篇文章主要讲述百度地图 ...

  9. Linux find常见用法示例

    find命令的参数: pathname: find命令所查找的目录路径.例如用.来表示当前目录,用/来表示系统根目录.-print: find命令将匹配的文件输出到标准输出.-exec: find命令 ...

  10. html表单-双向绑定

    潜水多年.一直是只看不评不写多年,每每看到各位大牛分享的经典文章都是默默的收藏,对大牛技术分享技术表示感谢,这么多年从博客园学到了很多. 这段时间项目告一段落. 正好这段时间相对清闲,我也整理一些常用 ...