Art_of_Programming_Contest_Part12

# Art_of_Programming_Contest_Part12 - APPENDIX B COMMON...

This preview shows pages 1–6. Sign up to view the full content.

APPENDIX B COMMON CODES/ROUTINES FOR PROGRAMMING 223 void bfs() { for(r=0, c=0; r<m; r++) nQ(r, c, -1, mat[r][c]); do { dQ(&r, &c, &p, &s); if(c<n-1) for(i=-1; i<2; i++) { nr=(m+r+i)%m, nc=c+1; if(M[nr][nc] > s+mat[nr][nc]) nQ(nr, nc, p, s+mat[nr][nc]), M[nr][nc] = s+mat[nr][nc]; } else if(s<finalSum) finalSum = s, leaf = p; } while(rear!=front); } void dfs(int leaf) { if(Q[leaf][2]==-1) { printf(" <%d, %d>", Q[leaf][0]+1, Q[leaf][1]+1); return; } dfs(Q[leaf][2]); printf(" <%d, %d>", Q[leaf][0]+1, Q[leaf][1]+1); } void main() { clrscr(); int i, j, t; init(); freopen("in.txt", "r", stdin); scanf("%d%d", &m, &n); for(i=0; i<m; i++) for(j=0; j<n; j++) { scanf("%d", &t); mat[i][j] = t; } bfs(); printf("Final sum: %d\nPath:", finalSum); dfs(leaf); }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
APPENDIX B COMMON CODES/ROUTINES FOR PROGRAMMING 224 Floyed Warshal Input 5 7 1 2 4 1 3 1 1 5 6 2 5 3 2 4 1 3 2 1 4 5 1 0 0 Code : #include<stdio.h> #include<values.h> #define N 100 #define INF MAXINT int mat[N][N], path[N][N], n, e; void initMat() { for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) mat[i][j] = INF; } void initPath() { for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) if(mat[i][j]!=INF) path[i][j] = j; else path[i][j] = 0; } void floyd_warshall() { for(int k=1; k<=n; k++) for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) if(mat[i][k]!=INF && mat[k][j]!=INF) if(mat[i][k]+mat[k][j] < mat[i][j]) mat[i][j] = mat[i][k] + mat[k][j], path[i][j] = path[i][k];
APPENDIX B COMMON CODES/ROUTINES FOR PROGRAMMING 225 } void showPath(int i, int j) { if(i==j) { printf("->%d", i); return; } printf("->%d", i); showPath(path[i][j], j); } void main() { while(scanf("%d%d", &n, &e) && n && e) { initMat(); for(int i, j, c, k=0; k<e; k++) { scanf("%d%d%d", &i, &j, &c); mat[i][j] = c; } initPath(); floyd_warshall(); for(i=1; i<=n; i++) { for(j=1; j<=n; j++) if(path[i][j]) { printf("%d", i); showPath(path[i][j], j); printf("\n"); } printf("\n"); } } } Graph Coloring #include<stdio.h> int a[20][20],x[20],n,m; void next(int k) { int j; while(1)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
APPENDIX B COMMON CODES/ROUTINES FOR PROGRAMMING 226 { x[k]=(x[k]+1)%(m+1); if(x[k]==0) return; for(j=1;j<=n;j++) if(a[k][j]!=0&&x[k]==x[j]) break; if(j==n+1) return; } } void mcolor(int k) { int j; while(1) { next(k); if(x[k]==0) return; if(k==n) { printf(" "); for(j=1;j<=n;j++) printf("%2d",x[j]); } else mcolor(k+1); } } void main() { int i,u,v; printf("\n\n Enter how many colors : "); scanf("%d",&m); printf("\n\n Enter how many nodes(0<n<20) :"); scanf("%d",&n); printf("\n\n Enter your edges(ex- u sp v)(press 'e' for end) : \n"); for(i=1;i<=(n*n)/2;i++) { if(getchar()=='e') break; scanf("%d%d",&u,&v); a[u][v]=a[v][u]=1; }mcolor(1); printf("\n\n");} Cycle Detection (Hamiltonian) #include<stdio.h> int a[20][20],x[20],n; void next(int k) { int j;
APPENDIX B COMMON CODES/ROUTINES FOR PROGRAMMING 227 while(1) { x[k]=(x[k]+1)%(n+1); if(x[k]==0) return; if((a[x[k-1]][x[k]])!=0) { for(j=1;j<=k-1;j++) if(x[k]==x[j]) break; if(j==k) if((k<n)||(k==n&&a[x[n]][x[1]]!=0)) return; } } } void hamilt(int k)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This document was uploaded on 08/10/2011.

### Page1 / 20

Art_of_Programming_Contest_Part12 - APPENDIX B COMMON...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online