洛谷P2886 [USACO07NOV]牛继电器Cow Relays

我不是女神ヾ 2021-12-16 03:01 333阅读 0赞

题目描述

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.

给出一张无向连通图,求S到E经过k条边的最短路。

输入输出格式

输入格式:

* 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

输出格式:

* Line 1: A single integer that is the shortest distance from intersection S to intersection E that traverses exactly N cow trails.

输入输出样例

输入样例#1: 复制

  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

输出样例#1: 复制

  1. 10
  2. 题解:
  3. 一句话题意:给出一个有t条边的图,求从se恰好经过k条边的最短路。
  4. a:是图的邻接矩阵,f是图中任意两点直接的最短距离、
  5. floyd算法:
  6. f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
  7. 一遍floyed后: f[i][j]是ij的最短距离:中间至少1条边(连通图),最多n-1条边
  8. floyd算法的变形:
  9. a:是图的邻接矩阵,a[i][j]是经过一条边的最短路径。f[i][j]k的初值为∞
  10. f[i][j]-1=∞;
  11. f[i][j]1=a; //经过一条边的最短路径
  12. f[i][j]2=min(f[i][j]2,a[i][k]+a[k][j])=a*a=a2;//经过二条边的最短路径,经过一次floyd.矩阵相乘一次,f[i][j]2初值为∞。
  13. f[i][j]3=min(f[i][j]3,f[i][k]2+a[k][j])=a*a*a=a3;//经过三条边的最短路径,经过二次floyd。f[i][j]3初值为∞
  14. f[i][j]4=min(f[i][j]4,f[i][k]3+a[k][j])=a4;//经过四条边的最短路径,经过三次floyd,f[i][j]4初值为∞
  15. ...
  16. f[i][j]k=min(f[i][j]k,f[i][k]k-1+a[k][j])=ak;//经过k条边的最短路径,经过K-1次floyd,f[i][j]k初值为∞ 而floyd的时间复杂度为O(n3),则从的时间复杂度为O(Kn3),非常容易超时。 所以我们可以用快速幂来完成。
  17. f[i][j]r+p=min(f[i][j]r+p,f[i][k]r+f[k][j]p)
  18. 程序:
  19. //洛谷2886
  20. //(1)对角线不能设置为0,否则容易自循环。(2)数组要放在主程序外面
  21. //(3)K条边,不一定是最简路
  22. #include<iostream>
  23. #include<cstdio>
  24. #include<map>
  25. #include<cstring>
  26. using namespace std;
  27. map<int,int>f;
  28. const int maxn=210;
  29. int k,t,s,e,n;
  30. int a[maxn][maxn];
  31. struct Matrix{
  32. int b[maxn][maxn];
  33. };
  34. Matrix A,S;
  35. Matrix operator *(Matrix A,Matrix B){
  36. //运算符重载
  37. Matrix c;
  38. memset(c.b,127/3,sizeof(c.b) );
  39. for(int k=1;k<=n;k++)
  40. for(int i=1;i<=n;i++)
  41. for(int j=1;j<=n;j++)
  42. c.b[i][j]=min(c.b[i][j],A.b[i][k]+B.b[k][j]);
  43. return c;
  44. }
  45. Matrix power(Matrix A,int k){
  46. if(k==0) return A;
  47. Matrix S=A;
  48. for(int i=1;i<=n;i++){
  49. for (int j=1;j<=n;j++)
  50. cout<<A.b[i][j]<<" ";
  51. cout<<endl;
  52. }
  53. cout<<endl;
  54. while(k){
  55. if(k&1)S=S*A;//奇数执行,偶数不执行
  56. /*cout<<k<<":"<<endl;
  57. for(int i=1;i<=n;i++){
  58. for (int j=1;j<=n;j++)
  59. cout<<S.b[i][j]<<" ";
  60. cout<<endl;
  61. }*/
  62. cout<<endl;
  63. A=A*A;
  64. k=k>>1;
  65. }
  66. return S;
  67. }
  68. int main(){
  69. cin>>k>>t>>s>>e;
  70. int w,x,y;
  71. memset(A.b,127/3,sizeof(A.b) );// 赋初值
  72. n=0;
  73. for(int i=1;i<=t;i++){
  74. //t<100,x<1000 点不是从1开始的,可以用map离散化,给点从1开始编号。n记录共有多少个点。
  75. cin>>w>>x>>y;
  76. if (f[x]==0) f[x]=++n;
  77. if (f[y]==0) f[y]=++n;
  78. A.b[f[x]][f[y]]=A.b[f[y]][f[x]]=min(w,A.b[f[y]][f[x]]);
  79. }
  80. S=power(A,k-1);//快速幂.执行k-1次floyd矩阵乘
  81. s=f[s];
  82. e=f[e];
  83. cout<<S.b[s][e];
  84. return 0;
  85. }

转载于:https://www.cnblogs.com/ssfzmfy/p/10740803.html

发表评论

表情:
评论列表 (有 0 条评论,333人围观)

还没有评论,来说两句吧...

相关阅读