Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 32063 | Accepted: 9080 |
Description
Input
Output
Sample Input
1 4 10000 3 2 2 8000 3 5000 1000 2 1 4 200 3000 2 1 4 200 50 2 0
Sample Output
5250
1 #include<stdio.h> 2 #include<string.h> 3 const int INF = 0x3f3f3f3f; 4 5 int map[110][110]; 6 int rank[110],dis[110],vis[110]; 7 int n,m; 8 9 int Dijkstra(int k) 10 { 11 int i,j; 12 memset(vis,0,sizeof(vis)); 13 vis[0] = 1; 14 for(i = 0; i <= n; i++) 15 dis[i] = map[0][i]; 16 dis[0] = 0; 17 for(i = 1; i <= n; i++) 18 { 19 int min = INF,pos; 20 for(j = 0; j <= n; j++) 21 { 22 if(dis[j] < min && !vis[j]) 23 { 24 min = dis[j]; 25 pos = j; 26 } 27 } 28 vis[pos] = 1; 29 for(j = 0; j <= n; j++) 30 { 31 if(!vis[j] && dis[j] > dis[pos] + map[pos][j] && rank[pos] >= k && rank[pos] <= k+m) 32 dis[j] = dis[pos] + map[pos][j]; 33 } 34 } 35 return dis[1]; 36 } 37 int main() 38 { 39 int i; 40 int p,x,t,v; 41 memset(map, 0x3f, sizeof(map)); 42 scanf("%d %d",&m,&n); 43 for(i = 1; i <= n; i++) 44 { 45 scanf("%d %d %d",&p,&rank[i],&x); 46 map[0][i] = p; 47 while(x--) 48 { 49 scanf("%d %d",&t,&v); 50 map[t][i] = v; 51 } 52 } 53 int min = INF,ans; 54 //枚举从rank[1]-m到rank[1]的等级;取这些最短路中最短的; 55 for(i = rank[1]-m; i <= rank[1]; i++) 56 { 57 ans = Dijkstra(i); 58 if(min > ans) 59 min = ans; 60 } 61 printf("%d\n",min); 62 return 0; 63 }