样例
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;
}