1. Description
  2.  
  3. 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.
  4.  
  5. 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.
  6.  
  7. 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.
  8.  
  9. 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.
  10.  
  11. Input
  12.  
  13. * Line 1: Four space-separated integers: N, T, S, and E
  14. * Lines 2..T+1: Line i+1 describes trail i with three space-separated integers: lengthi , I1i , and I2i
  15.  
  16. Output
  17.  
  18. * Line 1: A single integer that is the shortest distance from intersection S to intersection E that traverses exactly N cow trails.
  19.  
  20. Sample Input
  21.  
  22. 2 6 6 4
  23. 11 4 6
  24. 4 4 8
  25. 8 4 9
  26. 6 6 8
  27. 2 6 9
  28. 3 8 9
  29. Sample Output
  30.  
  31. 10
  32. Source
  33.  
  34. USACO 2007 November Gold

题面

用一个矩阵a(i, j)来表示i到j经过若干条边的最短路,
初始化a为i到j边的长度,没有则是正无穷。
比如a矩阵表示经过n条边,b矩阵表示经过m条边,
那么a * b得到的矩阵表示经过m + n条边,
采用Floyd的思想进行更新。

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<algorithm>
  5. #include<cmath>
  6. #include<queue>
  7. #include<string>
  8. #include<map>
  9. #define ll long long
  10. using namespace std;
  11. ll n,m,S,T,l;
  12. map<ll,ll>id;
  13. struct node{
  14. ll a[][];
  15. friend node operator *(node x,node y)
  16. {
  17. node z;
  18. memset(z.a,0x3f,sizeof(z.a));
  19. for(ll k=;k<=l;k++)
  20. for(ll i=;i<=l;++i)
  21. for(ll j=;j<=l;++j)
  22. z.a[i][j]=min(z.a[i][j],x.a[i][k]+y.a[k][j]);
  23. return z;
  24. }
  25. }s,ans;
  26. void ksm()
  27. {
  28. ans=s;
  29. n--;
  30. while(n)
  31. {
  32. if(n&) ans=ans*s;
  33. s=s*s;
  34. n>>=;
  35. }
  36. }
  37. int main()
  38. {
  39. freopen("run.in","r",stdin);
  40. freopen("run.out","w",stdout);
  41. memset(s.a,0x3f,sizeof(s.a));
  42. scanf("%lld%lld%lld%lld",&n,&m,&S,&T);
  43. for(ll i=,x,y,z;i<=m;++i)
  44. {
  45. scanf("%lld%lld%lld",&z,&x,&y);
  46. if(id[x]) x=id[x];
  47. else l++,id[x]=l,x=l;
  48. if(id[y]) y=id[y];
  49. else l++,id[y]=l,y=l;
  50. s.a[x][y]=s.a[y][x]=z;
  51. }
  52. S=id[S];T=id[T];
  53. ksm();
  54. printf("%lld",ans.a[S][T]);
  55. return ;
  56. }
  57. /*
  58. 2 6 6 4
  59. 11 4 6
  60. 4 4 8
  61. 8 4 9
  62. 6 6 8
  63. 2 6 9
  64. 3 8 9
  65. 10
  66. */

poj 3613Cow Relays的更多相关文章

  1. Poj 3613 Cow Relays (图论)

    Poj 3613 Cow Relays (图论) 题目大意 给出一个无向图,T条边,给出N,S,E,求S到E经过N条边的最短路径长度 理论上讲就是给了有n条边限制的最短路 solution 最一开始想 ...

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

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

  3. poj 3613 Cow Relays

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5411   Accepted: 2153 Descri ...

  4. POJ 3613 Cow Relays(floyd+快速幂)

    http://poj.org/problem?id=3613 题意: 求经过k条路径的最短路径. 思路: 如果看过<矩阵乘法在信息学的应用>这篇论文就会知道 现在我们在邻接矩阵中保存距离, ...

  5. POJ 3613 Cow Relays 恰好n步的最短路径

    http://poj.org/problem?id=3613 题目大意: 有T条路.从s到e走n步,求最短路径. 思路: 看了别人的... 先看一下Floyd的核心思想: edge[i][j]=min ...

  6. POJ 3613 Cow Relays【k边最短路】

    题目链接:http://poj.org/problem?id=3613 题目大意: 给出n头牛,t条有向边,起点以及终点,限制每头牛放在一个点上,(一个点上可以放多头牛),从起点开始进行接力跑到终点, ...

  7. Cow Relays POJ - 3613 (floyd+快速幂)

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

  8. POJ 3613 Cow Relays (floyd + 矩阵高速幂)

    题目大意: 求刚好经过K条路的最短路 我们知道假设一个矩阵A[i][j] 表示表示 i-j 是否可达 那么 A*A=B  B[i][j]  就表示   i-j 刚好走过两条路的方法数 那么同理 我们把 ...

  9. poj 3613 Cow Relays【矩阵快速幂+Floyd】

    !:自环也算一条路径 矩阵快速幂,把矩阵乘法的部分替换成Floyd(只用一个点扩张),这样每"乘"一次,就是经过增加一条边的最短路,用矩阵快速幂优化,然后因为边数是100级别的,所 ...

随机推荐

  1. CentOS 7.3下使用yum安装MySQL

    CentOS 7的yum源中默认是没有mysql的,要先下载mysql的repo源. 1.下载mysql的repo源 $ wget http://repo.mysql.com/mysql-commun ...

  2. 【Linux开发】【Qt开发】Qt界面键盘、触摸屏、鼠标的响应设置

    USB键盘 经过一番搜索,发现对Qt键盘的支持主要关系到两个方面: 1. 键盘类型确定: 4.7以前的Qt版本,如果是PS2圆孔键盘,Qt编译时需加上选项:-qt-kbd-vr41xx(未测试):如果 ...

  3. redis学习(二)

    深入了解redis字符串,列表,散列和有序集合命令,了解发布,订阅命令和其他命令.   一,字符串   1.字符串可以存储3种类型的值 字符串,整数,浮点数 2.运算命令列表 incr : incr ...

  4. Linux-Maven部署

    一.Maven是什么 二.Maven部署 1.环境信息: (1)centos7.3 (2)jdk1.8 (3)maven3.5.3 2.安装jdk (1)下载地址[http://www.oracle. ...

  5. mysqlreport 安装&使用

    安装包:mysqlreport-3.5.tgz 下载地址:http://hackmysql.com/scripts/mysqlreport-3.5.tgz 安装办法:[root@nagios ~]# ...

  6. 选择排序--python

    def findSmallest(arr): smallest = arr[0] smallest_index = 0 for i in range(1, len(arr)): if arr[i] & ...

  7. 自己手动用原生实现bind/call/apply

    自己手动用原生实现bind/call/apply:https://www.cnblogs.com/LHLVS/p/10595784.html

  8. nginx知识问答

    1.请解释一下什么是Nginx? 答:Nginx是一个web服务器和反向代理服务器,用于HTTP.HTTPS.SMTP.POP3和IMAP协议.2.请列举Nginx的一些特性? 答:Nginx服务器的 ...

  9. c# 杀死占用某个文件的进程

    原文:c# 杀死占用某个文件的进程 需要使用微软提供的工具Handle.exe string fileName = @"H:\abc.dll";//要检查被那个进程占用的文件 Pr ...

  10. 2017第二届广东省强网杯线上赛--Nonstandard

    测试文件:http://static2.ichunqiu.com/icq/resources/fileupload/CTF/echunqiu/qwb/Nonstandard_26195e1832795 ...