programming-challenges Unidirectional TSP (111104) 题解
来源:互联网 发布:淘宝商家等级划分 编辑:程序博客网 时间:2024/06/11 21:31
http://algorithmist.com/index.php/UVa_116 这里有很好的测试用例。这道题其实只是很直观的迭代。
#include <iostream>#include <sstream>#include <fstream>#include <string>#include <vector>#include <list>#include <queue>#include <map>#include <set>#include <stack>#include <assert.h>#include <algorithm>#include <math.h>#include <ctime>#include <functional>#include <string.h>#include <stdio.h>#include <numeric>#include <float.h>using namespace std;const int N = 110, M = 20; long long a[M][N]; struct Rec {vector<int> path; };bool operator<(const Rec& rec1, const Rec& rec2) {for (int i = 0; i < rec1.path.size(); i++) {if (rec1.path[i] < rec2.path[i]) return true;if (rec1.path[i] > rec2.path[i]) return false;}return false; }vector<Rec> paths(M); int main() {int m, n; while (cin >> m) {cin >> n; memset(a, 0, sizeof(a));paths.clear();paths.resize(M);for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {cin >> a[i][j]; }}for (int i = 0; i < m; i++) {paths[i].path.push_back(i);}for (int i = 1; i < n; i++) {vector<Rec> tempPaths(M);for (int j = 0; j < m; j++) {long long minVal = a[(j + m - 1) % m][i-1] + a[j][i];int row = (j + m - 1) % m; for (int k = 0; k <= 1; k++) {long long val = a[(j + m + k) % m][i-1] + a[j][i];if (val < minVal) {minVal = val; row = (j + m + k) % m; }else if (val == minVal) {int newRow = (j + m + k) % m; if (paths[row].path > paths[newRow].path) {row = newRow; } }}tempPaths[j] = paths[row];tempPaths[j].path.push_back(j); a[j][i] = minVal; }paths = tempPaths;}long long obj = 0x7fffffffffffffff; for (int i = 0; i < m; i++) {obj = obj < a[i][n - 1] ? obj : a[i][n - 1];}bool firstPath = true;Rec bestPath; for (int i = 0; i < m; i++) {if (a[i][n - 1] == obj) {if (firstPath) {firstPath = false;bestPath = paths[i];}else {bestPath = min(bestPath, paths[i]);}}}for (int i = 0; i < bestPath.path.size(); i++) {cout << bestPath.path[i] + 1;if (i + 1 < bestPath.path.size())cout << " ";}cout << endl << obj << endl; }return 0; }
0 0
- programming-challenges Unidirectional TSP (111104) 题解
- programming-challenges WERTYU (110301) 题解
- programming-challenges ShellSort (110407) 题解
- programming-challenges Ones (110504) 题解
- programming-challenges Chopsticks (111107) 题解
- programming-challenges Bicoloring (110901) 题解
- Unidirectional TSP
- programming-challenges Crypt Kicker (110204) 题解
- programming-challenges Stack 'em Up (110205) 题解
- programming-challenges Poker Hands (110202) 题解
- programming-challenges Contest Scoreboard (110207) 题解
- programming-challenges Where's Waldorf? (110302) 题解
- programming-challenges Common Permutation (110303) 题解
- programming-challenges Crypt Kicker II (110304) 题解
- programming-challenges Automated Judge Script (110305) 题解
- programming-challenges File Fragmentation (110306) 题解
- programming-challenges Vito's Family (110401) 题解
- programming-challenges Stacks of Flapjacks (110402) 题解
- 二分查找
- 经典的30道JAVA面试题
- 异步过程调用APC
- HDU 4354 Missile(树形DP)
- Java抽象类和接口的区别
- programming-challenges Unidirectional TSP (111104) 题解
- View的bounds和frame的区别
- Java的四中引用类型
- 源码解析Android中View的measure量算过程
- Android 开发手记二 C可执行程序编译实例
- 有效的括号序列(LintCode)
- Java内部类为什么可以访问外部类的成员
- Android 多线程-----AsyncTask详解
- 《程序员面试金典》--寻找二叉树中某个节点中序遍历的下一个节点