codevs2800送外卖

链接:http://codevs.cn/problem/2800/

题意:中文题。

分析:经典的货郎担问题(TSP),只是这题每个点可以多次路过。先跑一遍Floyd,再状压DP即可。O(n^3+n^2*2^n)

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=1000010;
const int MAX=151;
const int MOD1=1000007;
const int MOD2=100000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000009;
const ll INF=10000000010;
typedef double db;
typedef unsigned long long ull;
int d[20][20],dp[20][70010];
int main()
{
    int i,j,k,n;
    scanf("%d", &n);
    for (i=0;i<=n;i++)
        for (j=0;j<=n;j++) scanf("%d", &d[i][j]);
    for (k=0;k<=n;k++)
        for (i=0;i<=n;i++)
            for (j=0;j<=n;j++)
            d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
    memset(dp,0x3f,sizeof(dp));
    dp[0][0]=0;n++;
    for (i=0;i<1<<n;i++)
        for (j=0;j<n;j++)
            for (k=0;k<n;k++)
            if (i&(1<<j)) dp[j][i]=min(dp[j][i],dp[k][i-(1<<j)]+d[k][j]);
    printf("%d\n", dp[0][(1<<n)-1]);
    return 0;
}


你可能感兴趣的:(codevs2800送外卖)