syhd142 |
|
|||
日歷
統計
導航常用鏈接留言簿(2)隨筆檔案(23)文章分類(270)
文章檔案(122)我的豆瓣搜索最新評論
閱讀排行榜
評論排行榜 |
題意:題目翻譯成中文就是無向旅行商,其實就是給你一個n*m的矩陣,要求一條從第一列到最后一列的最小花費的路徑,上下可以互通。 解法:看到這題就覺得和The triangle類似,很快寫出代碼,輸出路徑沒有問題,但是題目說如果有多條路徑要求字典序最小的那個。我開始是從左往右推,網上看了解題報告說這樣做不能保證字典序最小,要從右往左推,于是改了代碼就A了,至于為什么這樣做就能保證字典序最小還沒有想明白。 #include <stdio.h>
#include <string.h> #define N 15 #define M 105 #define INF 1 << 29 int dir[3][2] = {{-1, -1}, {0, -1}, {1, -1}}; int a[N][M], b[N][M], p[N][M], s[M]; int main() { int n, m, x, y, ans, top; while(~scanf("%d %d", &n, &m)) { for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) scanf("%d", &a[i][j]); memset(p, -1, sizeof(p)); for(int i = 1; i <= n; i++) b[i][m] = a[i][m]; for(int i = 1; i <= n; i++) for(int j = 1; j < m; j++) b[i][j] = INF; for(int j = m ; j > 1; j--) for(int i = 1; i <= n; i++) { for(int k = 0; k < 3; k++) { x = i + dir[k][0]; y = j + dir[k][1]; if(x < 1) x = n; if(x > n) x = 1; if(b[i][j] + a[x][y] < b[x][y]) { b[x][y] = b[i][j] + a[x][y]; p[x][y] = k; } } } ans = INF, y = 1, top = 0; for(int i = 1; i <= n; i++) { if(b[i][1] < ans) { ans = b[i][1]; x = i; } } s[top++] = x; while(p[x][y] != -1) { int t = p[x][y]; x -= dir[t][0], y++; if(x > n) x = 1; if(x < 1) x = n; s[top++] = x; } for(int i = 0; i < top; i++) { printf("%d", s[i]); if(i != top - 1) printf(" "); else printf("\n"); } printf("%d\n", ans); } return 0; }
|
![]() |
|
Copyright © Fucker | Powered by: 博客園 模板提供:滬江博客 |