题意: 给i到j花费的地图 1到n编号 一个人要从1遍历n个城市后回到1
求最小的花费(可以重复走)
分析
http://www.cnblogs.com/Empress/p/4039240.html
TSP
因为可以重复走 所以先floyd一下求最短路
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cctype>
#include <cmath>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
typedef long long LL;
typedef long double LD;
#define pi acos(-1.0)
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
typedef pair<int, int> PI;
typedef pair<int, PI> PP;
#ifdef _WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//LL quick(LL a, LL b){LL ans=1;while(b){if(b & 1)ans*=a;a=a*a;b>>=1;}return ans;}
//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;}
//inline void print(LL x){printf(LLD, x);puts("");}
//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();}} int dp[<<][], mp[][];
int n;
void floyd()
{
for(int k=;k<n;k++)
for(int i=;i<n;i++)
for(int j=;j<n;j++)
mp[i][j]=min(mp[i][j], mp[i][k]+mp[k][j]);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
while(~scanf("%d", &n) && n)
{
n++;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
scanf("%d", &mp[i][j]);
floyd();
memset(dp, , sizeof(dp));
dp[(<<n)-][]=;
for(int s=(<<n)-;s>=;s--)
for(int v=;v<n;v++)
for(int u=;u<n;u++)
if(!(s>> u & ))
dp[s][v]=min(dp[s][v], dp[s | <<u][u]+mp[v][u]);
printf("%d\n", dp[][]);
}
return ;
}
POJ 3311