标签:des style blog http java color
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5421 Accepted Submission(s):
1988
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 #define INF 0xfffffff 6 7 8 int map[1005][1005], disk[1005], pathnum[1005]; 9 int n, m; 10 11 int getmin(int x, int y){ 12 return x > y ? y : x; 13 } 14 15 int dis(){ 16 int i, j, visit[1005], idmin, min; 17 memset(visit, 0, sizeof(visit)); 18 for(i = 1; i <= n; i ++){ 19 disk[i] = map[2][i]; 20 } 21 disk[2] = 0; 22 for(i = 1; i <= n; i ++){ 23 idmin = 0; 24 min = INF; 25 for(j = 1; j <= n; j ++){ 26 if(!visit[j] && disk[j] < min){ 27 min = disk[j]; 28 idmin = j; 29 } 30 } 31 visit[idmin] = 1; 32 for(j = 1; j <= n; j ++){ 33 if(!visit[j]){ 34 disk[j] = getmin(disk[j], map[idmin][j] + disk[idmin]); 35 } 36 } 37 } 38 return 0; 39 } 40 41 int dfs(int start){ 42 int sum, i; 43 if(start == 2){ 44 return 1; 45 } 46 if(pathnum[start] != -1){ 47 return pathnum[start]; 48 } 49 sum = 0; 50 for(i = 1; i <= n; i ++){ 51 if(map[i][start] != INF && map[i][start] == disk[start] - disk[i]){ 52 sum += dfs(i); 53 } 54 } 55 pathnum[start] = sum; 56 return pathnum[start]; 57 } 58 59 int main(){ 60 int x, y, d, i, j; 61 while(scanf("%d", &n) && n){ 62 scanf("%d", &m); 63 for(i = 0; i < 1005; i ++){ 64 for(j = 0; j < 1005; j ++){ 65 map[i][j] = INF; 66 } 67 } 68 //printf("%d\n", map[1][1]); 69 for(i = 0; i < m; i ++){ 70 scanf("%d %d %d", &x, &y, &d); 71 map[x][y] = map[y][x] = d; 72 } 73 dis(); 74 //printf("%d\n", disk[1]); 75 memset(pathnum, -1, sizeof(pathnum)); 76 //printf("%d\n", pathnum[1]); 77 printf("%d\n", dfs(1)); 78 } 79 return 0; 80 }
杭电1142(最短路径+dfs),布布扣,bubuko.com
标签:des style blog http java color
原文地址:http://www.cnblogs.com/xiaoyeye/p/3851467.html