最短路( spfa)
最短路
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2622
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <stdio.h> 2 #include <string.h> 3 #include <queue> 4 #include <vector> 5 using namespace std; 6 const long long INF=1LL<<38; 7 long long dis[102][12];//dis[i][x]i表示结点,x记录路径数 8 int vis[102]; 9 int n,m,x; 10 struct node 11 { 12 int v; 13 long long w; 14 }hh; 15 vector<node>G[102]; 16 void init() 17 { 18 memset(vis,0,sizeof(vis)); 19 for (int i = 0;i < n;i ++){ 20 G[i].clear(); 21 for (int j = 0;j < 12;j ++) 22 { 23 dis[i][j] = INF; 24 } 25 } 26 27 } 28 void spfa(int uu) 29 { 30 queue<int>q; 31 q.push(uu); 32 vis[uu] = 1; 33 dis[uu][0] = 0; 34 while(!q.empty()) 35 { 36 int u = q.front(); 37 q.pop(); 38 vis[u] = 0; 39 for (int i = 0;i < G[u].size();i ++) 40 { 41 int v = G[u][i].v; 42 long long w = G[u][i].w; 43 for (int j = 0;j < x;j ++) 44 { 45 if (dis[u][j] + w < dis[v][(j+1)%x]) 46 { 47 dis[v][(j+1)%x] = dis[u][j] + w ; 48 if (!vis[v]) 49 { 50 q.push(v); 51 vis[v] = 1; 52 } 53 } 54 } 55 } 56 } 57 } 58 int main() 59 { 60 int t; 61 scanf("%d",&t); 62 while(t--) 63 { 64 int u,v; 65 long long w; 66 scanf("%d%d",&n,&m); 67 init(); 68 for (int i = 0;i < m;i ++) 69 { 70 scanf("%d%d%lld",&u,&v,&w); 71 hh.v=v; 72 hh.w=w; 73 G[u].push_back(hh); 74 } 75 int s,e; 76 scanf("%d%d%d",&s,&e,&x); 77 spfa(s); 78 if (dis[e][0] < INF) 79 printf("%lld\n",dis[e][0]); 80 else 81 printf("No Answer!\n"); 82 } 83 return 0; 84 } 85