【算法】DP解决旅行路径问题

问题描述 :

After coding so many days,Mr Acmer wants to have a good rest.So travelling is the best choice!He has decided to visit n cities(he insists on seeing all the cities!And he does not mind which city being his start station because superman can bring him to any city at first but only once.), and of course there are m roads here,following a fee as usual.But Mr Acmer gets bored so easily that he doesn’t want to visit a city more than twice!And he is so mean that he wants to minimize the total fee!He is lazy you see.So he turns to you for help.

输入:

There are several test cases,the first line is two intergers n(1<=n<=10) and m,which means he needs to visit n cities and there are m roads he can choose,then m lines follow,each line will include three intergers a,b and c(1<=a,b<=n),means there is a road between a and b and the cost is of course c.Input to the End Of File.

输出:

There are several test cases,the first line is two intergers n(1<=n<=10) and m,which means he needs to visit n cities and there are m roads he can choose,then m lines follow,each line will include three intergers a,b and c(1<=a,b<=n),means there is a road between a and b and the cost is of course c.Input to the End Of File.

样例输入:

2 1
1 2 100
3 2
1 2 40
2 3 50
3 3
1 2 3
1 3 4
2 3 10

样例输出:

100
90
7 

#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[60000][11];
int cnt[11][11];
int three[12];
int m,n;
int f[60000][11];
void init()
{
    three[1]=1;
    for(int i=2;i<=11;i++)
        three[i]=three[i-1]*3;
    for(int i=0;i<three[11];i++)
    {
        int tmp=i;
        for(int j=1;j<=10;j++)
        {
            f[i][j]=tmp%3;
            tmp/=3;
        }
    }
}
int main()
{
    init();
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(dp,0x7f,sizeof(dp));
        memset(cnt,0x7f,sizeof(cnt));
        int ans=dp[0][0];
        int inf=ans;
        int x,y,d;
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&d);
            cnt[x][y]=cnt[y][x]=min(cnt[x][y],d);
        }
        for(int i=1;i<=n;i++)
            dp[three[i]][i]=0;
        for(int state=1;state<three[n+1];state++)
        {
            bool ok=1;
            for(int i=1;i<=n;i++)
            {
                if(f[state][i]==0)
                    ok=0;
                if(dp[state][i]==inf)
                    continue;
                for(int j=1;j<=n;j++)
                {
                    if(i==j)
                        continue;
                    if(f[state][j]==2)
                        continue;
                    if(cnt[i][j]==inf)
                        continue;
                    dp[state+three[j]][j]=min(dp[state+three[j]][j],dp[state][i]+cnt[i][j]);
                }
            }
            if(ok)
            {
                for(int i=1;i<=n;i++)
                {
                    ans=min(ans,dp[state][i]);
                }
            }
        }
        if(ans==inf)
            ans=-1;
        printf("%d
",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/KID-XiaoYuan/p/6417431.html