题意: 给i到j花费的地图 1到n编号 一个人要从1遍历n个城市后回到1
求最小的花费(可以重复走)
分析
http://www.cnblogs.com/Empress/p/4039240.html
TSP
因为可以重复走 所以先floyd一下求最短路
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <climits> 5 #include <cctype> 6 #include <cmath> 7 #include <string> 8 #include <sstream> 9 #include <iostream> 10 #include <algorithm> 11 #include <iomanip> 12 using namespace std; 13 #include <queue> 14 #include <stack> 15 #include <vector> 16 #include <deque> 17 #include <set> 18 #include <map> 19 typedef long long LL; 20 typedef long double LD; 21 #define pi acos(-1.0) 22 #define lson l, m, rt<<1 23 #define rson m+1, r, rt<<1|1 24 typedef pair<int, int> PI; 25 typedef pair<int, PI> PP; 26 #ifdef _WIN32 27 #define LLD "%I64d" 28 #else 29 #define LLD "%lld" 30 #endif 31 //#pragma comment(linker, "/STACK:1024000000,1024000000") 32 //LL quick(LL a, LL b){LL ans=1;while(b){if(b & 1)ans*=a;a=a*a;b>>=1;}return ans;} 33 //inline int read(){char ch=' ';int ans=0;while(ch<'0' || ch>'9')ch=getchar();while(ch<='9' && ch>='0'){ans=ans*10+ch-'0';ch=getchar();}return ans;} 34 //inline void print(LL x){printf(LLD, x);puts("");} 35 //inline void read(double &x){char c = getchar();while(c < '0') c = getchar();x = c - '0'; c = getchar();while(c >= '0'){x = x * 10 + (c - '0'); c = getchar();}} 36 37 int dp[1<<11][11], mp[11][11]; 38 int n; 39 void floyd() 40 { 41 for(int k=0;k<n;k++) 42 for(int i=0;i<n;i++) 43 for(int j=0;j<n;j++) 44 mp[i][j]=min(mp[i][j], mp[i][k]+mp[k][j]); 45 } 46 int main() 47 { 48 #ifndef ONLINE_JUDGE 49 freopen("in.txt", "r", stdin); 50 freopen("out.txt", "w", stdout); 51 #endif 52 while(~scanf("%d", &n) && n) 53 { 54 n++; 55 for(int i=0;i<n;i++) 56 for(int j=0;j<n;j++) 57 scanf("%d", &mp[i][j]); 58 floyd(); 59 memset(dp, 127, sizeof(dp)); 60 dp[(1<<n)-1][0]=0; 61 for(int s=(1<<n)-2;s>=0;s--) 62 for(int v=0;v<n;v++) 63 for(int u=0;u<n;u++) 64 if(!(s>> u & 1)) 65 dp[s][v]=min(dp[s][v], dp[s | 1<<u][u]+mp[v][u]); 66 printf("%d\n", dp[0][0]); 67 } 68 return 0; 69 }