题目链接

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read(){
int x=,f=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} /***********************************************************/ int n, m; // n < = 10
const int maxn = 6e4+;
int d[][maxn];
//用三进制形式来表示状态
int dist[][];
//表示路径
int t[];
int cnt, state[maxn];
int temp; //判断S的三进制是否没有一个0
bool legal(int S){
bool ok = true;
for(int i = ;i <= n-;i++){
if(S% == ){
ok = false;
break;
}
S /= ;
}
return ok;
} void init(){
temp = ;
for(int i = ;i <= ;i++){
t[i] = temp;
temp *= ;
}
} //i点在集合S中是否出现了至少一次
inline bool in(int i, int S){
for(int j = ;j < i;j++)
S /= ;
if(S%) return true;
else return false;
} int dp(int i, int S){
if(d[i][S] >= ) return d[i][S];
int &ans = d[i][S];
ans = 1e9;
int S1 = S - t[i];
for(int j = ;j < n;j++){
if(j != i && in(j, S1) && dist[j][i] != -)
ans = min(ans, dp(j, S1) + dist[j][i]);
}
return ans;
} int main(){
init();
while(~scanf("%d %d", &n, &m)){
int max_3 = ;
for(int i = ;i < n;i++)
max_3 += t[i];
cnt = ;
for(int S = max_3;S < t[n];S++){
if(legal(S))
state[cnt++] = S;
}
memset(dist, -, sizeof(dist));
for(int i = ;i < m;i++){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
a--;b--;
//更新长度
if(dist[a][b] == - || dist[a][b] > c)
dist[a][b] = dist[b][a] = c;
}
memset(d, -, sizeof(d));
for(int i = ;i < n;i++)
d[i][t[i]] = ; int sum = dp(, max_3);
for(int i = ;i < n;i++){
for(int j = ;j < cnt;j++){
sum = min(sum, dp(i, state[j]));
}
}
if(sum == 1e9) printf("-1\n");
else printf("%d\n", sum);
}
return ;
}

最新文章

  1. Eclipse修改编码格式
  2. rename() 是原子的么
  3. 基础学习day11--多线程一线程的创建,运行,同步和锁
  4. Java异常的深入研究与分析
  5. mysql DDL语句
  6. B树,B-树,B+树,B*树
  7. pycharm常用快捷键与设置
  8. Android 中Webview 自适应屏幕
  9. 【转】Derivation of the Normal Equation for linear regression
  10. 正则取页面图片URL和TABLE BackGround
  11. zoj 2874 &amp;amp; poj 3308 Paratroopers (最小割)
  12. CSS3制作
  13. 安装MySQL -- SuSE Linux Enterprise Server 11 SP3
  14. Iozone
  15. Towers CodeForces - 229D
  16. Mac版Android Studio的安装和使用
  17. python语法_函数
  18. Regularity criteria for NSE 5: $u_3,\om_3$
  19. 使用 Navicate 连接 Oracle9i 数据库
  20. Nginx 反向代理+高可用

热门文章

  1. C++0X 学习之 auto
  2. C语言逗号运算符和逗号表达式
  3. 浅谈MVC、MVP、MVVM模式
  4. prufer BZOJ1211: [HNOI2004]树的计数
  5. JS之事件监听
  6. 使用Rancher搭建K8S测试环境
  7. MySQL读取各个my.cnf配置文件的先后顺序是:
  8. puppet多环境配置(puppet自动化系列2)
  9. pyodbc简单使用
  10. 【原】spring jar 下载