《挑战程序设计竞赛》 P191 旅行商问题(货担郎) 状态压缩

样例

5  8
0 1 3
0 3 4
1 2 5
2 0 4
2 3 5
3 4 3
4 0 7
4 1 6

输出:

22



都写的不错

http://blog.csdn.net/hellonerd/article/details/50920234  


http://blog.csdn.net/tianshuai1111/article/details/7535026


http://www.360doc.com/content/17/0826/11/36546539_682232069.shtml


http://blog.csdn.net/pandora_madara/article/details/31656161


、、
#include <bits/stdc++.h>

using namespace std;


const  int INF=9999;
const  int  MAX_N = 100;

int dp[ 1<<MAX_N ][MAX_N];

struct edge{
      int to;
      int cost;
      edge(int to ,int cost){
            this->to = to;
            this->cost =cost;

      }

};

vector<edge> G[MAX_N];

int d[MAX_N][MAX_N];

int main(){

    int n;
    cin>>n;
    int m;
    cin >>m;

//    for(int i=1;i<=m;i++){
//
//      int a,b,c; cin>>a>>b>>c;
//      G[a].push( edge(b,c) );
//
//    }
//
      for(int i=0;i<=n;i++)      {
            for(int j=0;j<=n;j++) {
                  d[i][j]=INF;
            }
      }

      for(int i=1;i<=m;i++){
            int a,b,c; cin>>a>>b>>c;
            d[a][b] = c;
      }


    for(int S=0;S < (1<<n) ;S++)  {
          fill(dp[S],dp[S]+n ,INF);
    }

    dp[ ( 1<<n ) -1 ] [0] = 0;

    for( int S= (1<<n)-2 ; S>=0; S--) {
          for( int v=0; v<n; v++){
                  for(int  u=0; u<n ;u++){
                        if( !(S>>u & 1) ) {
                              dp[S][v]  = min(  dp[S][v],   dp[S | 1<<u][u]  + d[v][u]  );
                        }
                  }
                  cout <<" S:"<< bitset<16>( S )<<"  V:" << v <<"   DP[S][V]:" <<  dp[S][v]  << "       ";


          }

          cout << endl;
    }

    //cout << endl;
    cout <<  dp[0][0];
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值