http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2622
1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 #include<vector> 5 using namespace std; 6 const long long INF = 1LL << 40; 7 struct node 8 { 9 int v,w; 10 }; 11 vector<struct node>map[110]; 12 int inque[110]; 13 long long dis[110][12]; 14 int s,t,x,n,m; 15 16 void spfa() 17 { 18 queue<int>que; 19 for(int i = 0; i < n; i++) 20 for(int k = 0; k <= x; k++) 21 dis[i][k] = INF; 22 dis[s][0] = 0; 23 memset(inque,0,sizeof(inque)); 24 25 que.push(s); 26 inque[s] = 1; 27 28 while(!que.empty()) 29 { 30 int u = que.front(); 31 inque[u] = 0; 32 que.pop(); 33 for(int i = 0; i < map[u].size(); i++) 34 { 35 int v = map[u][i].v; 36 for(int k = 0; k < x; k++) 37 { 38 if(dis[u][k] < INF && dis[v][(k+1)%x] > dis[u][k]+map[u][i].w) 39 { 40 dis[v][(k+1)%x] = dis[u][k]+map[u][i].w; 41 if(!inque[v]) 42 { 43 que.push(v); 44 inque[v] = 1; 45 } 46 } 47 } 48 } 49 } 50 } 51 int main() 52 { 53 int test; 54 scanf("%d",&test); 55 while(test--) 56 { 57 scanf("%d %d",&n,&m); 58 for(int i = 0; i < n; i++) 59 map[i].clear(); 60 int u,v,w; 61 for(int i = 1; i <= m; i++) 62 { 63 scanf("%d %d %d",&u,&v,&w); 64 map[u].push_back((struct node){v,w}); 65 } 66 scanf("%d %d %d",&s,&t,&x); 67 spfa(); 68 if(dis[t][0] >= INF) 69 printf("No Answer!\n"); 70 else printf("%lld\n",dis[t][0]); 71 } 72 return 0; 73 }