首页 > 代码库 > cf 187B.AlgoRace

cf 187B.AlgoRace

floyd。。。太神奇了(不会floyd(大雾))

貌似floyd的外层k是保证最短路从起点逐渐向外扩展(而不是乱搞233)

所以在处理f[i][j]=min(f[i][j],f[i][k]+f[k][j])的时候,f[i][k]都是已经处理过的,而f[k][j]都是没处理的。

所以这样的话就可以再加1维表示换的次数。(%DP)

 1 #include<bits/stdc++.h>
 2 #define LL long long 
 3 #define  N 100005
 4 using namespace std;
 5 inline int ra()
 6 {
 7     int x=0,f=1; char ch=getchar();
 8     while (ch<0 || ch>9) {if (ch==-) f=-1; ch=getchar();}
 9     while (ch>=0 && ch<=9) {x=x*10+ch-0; ch=getchar();}
10     return x*f;
11 }
12 int f[65][65][65],g[65][65][65];
13 int main()
14 {
15     int n=ra(),m=ra(),r=ra();
16     memset(f,0x7f,sizeof(f));
17     for (int i=1; i<=m; i++)
18         for (int j=1; j<=n; j++)
19             for (int k=1; k<=n; k++)
20                 g[i][j][k]=ra();
21     for (int id=1; id<=m; id++)
22         for (int k=1; k<=n; k++)
23             for (int i=1; i<=n; i++)
24                 for (int j=1; j<=n; j++)
25                     g[id][i][j]=min(g[id][i][j],g[id][i][k]+g[id][k][j]);
26         for (int i=1; i<=n; i++)
27             for (int j=1; j<=n; j++)
28                 for (int k=1; k<=m; k++)
29                     f[i][j][0]=min(f[i][j][0],g[k][i][j]);
30         for (int e=1; e<=n; e++)    
31             for (int k=1 ;k<=n; k++)
32                 for (int i=1; i<=n; i++)
33                     for (int j=1; j<=n; j++)
34                         f[i][j][e]=min(f[i][j][e],f[i][k][e-1]+f[k][j][0]);
35         for (int i=1; i<=r; i++)
36         {
37             int s=ra(),t=ra(),k=ra();
38             k=min(k,n);
39             printf("%d\n",f[s][t][k]);
40         }
41         return 0;
42 }

 

cf 187B.AlgoRace