hdu3001

题意

选择从任意一点出发,经过所有点的最小花费(经过每个点的次数不能多于 2 次)。

分析

类似于 poj3311

经过每个点的次数有限制,考虑用三进制数存储每个点被访问过的次数,其它和上面一题几乎相同。

code

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int MAXN = 60000;
const int INF = 1e9;
int dp[MAXN][12];
int dis[12][12];
int main() {
int m, n;
while(cin >> n >> m) {
memset(dis, 0x3f, sizeof dis);
memset(dp, 0x3f, sizeof dp);
for(int i = 0; i < m; i++) {
int x, y, c;
cin >> x >> y >> c; x--; y--;
dis[y][x] = dis[x][y] = min(c, dis[x][y]);
}
int total = 1;
for(int i = 0; i < n; i++) {
dis[i][i] = 0;
total *= 3;
}
for(int i = 0; i < total; i++) {
for(int j = 0; j < n; j++) {
int s = i, add = 1;
int t = j;
while(t--) {
s /= 3;
add *= 3;
}
s %= 3;
if(s < 2) {
dp[add][j] = 0; // 可以从任意一点出发
s = i;
for(int k = 0; k < n; k++) {
if(s % 3 > 0) {
dp[i + add][j] = min(dp[i + add][j], dp[i][k] + dis[k][j]);
}
s /= 3;
}
}
}
}
int ans = INF;
for(int j = 0; j < n; j++) {
ans = min(ans, dp[total - 1][j]);
}
if(ans == INF) ans = -1;
cout << ans << endl;
}
return 0;
}

最新文章

  1. 使用SqlDataReader和SqlDataAdapter的注意
  2. npm下载包时代理配置
  3. P6 EPPM Installation and Configuration Guide 16 R1 April 2016
  4. NODE学习:利用nodeJS去抓网页的信息
  5. 208. Implement Trie (Prefix Tree)
  6. C#与java中的集合区别
  7. 2016&quot;百度之星&quot; - 资格赛(Astar Round1) Problem D
  8. Div.2 C. Dasha and Password
  9. AngularJs 学习笔记(四)服务
  10. 编辑方法分享之如何编辑PDF文件内容
  11. boost asio 网络聊天 代码修改学习
  12. tomcat点击startup.bat一闪而退的方法
  13. Linux装机利器Cobbler安装配置
  14. Python3 写Windows Service服务程序
  15. java通过文件头来判断文件类型
  16. .NET后台访问其他站点代码整理
  17. 认识Java标识符
  18. 最大比例(贪心+思维+gcd)
  19. octave简易操作
  20. java图形验证码

热门文章

  1. (原)App源码
  2. 抓包工具 - Fiddler - (一)
  3. Python之日志处理 logging模块
  4. os--留
  5. awk学习笔记
  6. (总结)Linux服务器上最简单的Nginx反向代理配置
  7. hdu 1535 Invitation Cards (最短路径)
  8. P3531 [POI2012]LIT-Letters
  9. Educational Codeforces Round 2 A. Extract Numbers
  10. Codeforces Round #352 (Div. 2) A