标签:++ main while div void 访问 ide 单词 head
1 #include <stdio.h> 2 int map[10][10], book[10], n, m,sum; 3 void dfs(int cur) 4 { 5 int i; 6 printf("%d ",cur); 7 sum++; 8 if (sum == n) 9 return; 10 for (i = 1;i <= n;i++) 11 { 12 if (book[i] == 0 && map[i][cur]) 13 { 14 book[i] = 1; 15 dfs(i); 16 } 17 } 18 return; 19 } 20 void bfs(int front) 21 { 22 int que[100]; 23 int head = 0, tail = 0; 24 int i; 25 que[tail++] = front; 26 book[front] = 1; 27 while (head < tail) 28 { 29 for (i = 1;i <= n;i++) 30 { 31 if (map[que[head]][i] && book[i]==0) 32 { 33 book[i] = 1; 34 que[tail++] = i; 35 } 36 } 37 head++; 38 } 39 for (i = 0;i < tail;i++) 40 printf("%d ",que[i]); 41 printf("\n"); 42 } 43 int main() 44 { 45 int x, y,i; 46 scanf("%d%d",&n,&m); 47 for (i = 1;i <= m;i++) 48 { 49 scanf("%d%d",&x,&y); 50 map[x][y] = 1; 51 map[y][x] = 1; 52 } 53 /*book[1] = 1; 54 dfs(1); 55 printf("\n");*/ 56 bfs(1); 57 return 0; 58 }
1 #include <stdio.h> 2 int map[10][10], book[10], n; 3 int min = 99999; 4 void dfs(int cur,int sum) 5 { 6 int i; 7 if (min < sum) 8 return; 9 if (cur == n) 10 { 11 if (min > sum) 12 min = sum; 13 return; 14 } 15 for (i = 1;i <= n;i++) 16 { 17 if (book[i] == 0 && map[i][cur]) 18 { 19 book[i] = 1; 20 dfs(i, sum + map[i][cur]); 21 book[i] = 0; 22 } 23 } 24 return; 25 } 26 int main() 27 { 28 int e,i,x,y,t; 29 scanf("%d%d",&n,&e); 30 for (i = 1;i <= n;i++) 31 { 32 scanf("%d%d%d", &x, &y, &t); 33 map[x][y] = t; 34 map[y][x] = t; 35 } 36 book[1] = 1; 37 dfs(1,0); 38 printf("%d\n",min); 39 40 return 0; 41 }
标签:++ main while div void 访问 ide 单词 head
原文地址:http://www.cnblogs.com/naglish/p/7070737.html