现在的位置: 首页 > 综合 > 正文

hdu 3001 Travelling

2018年04月23日 ⁄ 综合 ⁄ 共 2291字 ⁄ 字号 评论关闭
Problem Description
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.
 

Input
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.
 

Output
Output the minimum fee that he should pay,or -1 if he can't find such a route.
 

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

Sample Output
100 90 7
 

Source
 

Recommend
gaojie

三进制状态dp

求每个点走两次的tsp,所以用三进制表示每个点走0、1、2次。

预处理,不然会tle。

有重边……

代码

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define INF 99999999

int dp[100005][12];
int map[15][15];
int three[12];
int n;
int hash[100005][15];

void Init()
{
    int i,p,up;
    three[0]=1;
    for (i=1;i<12;i++)
    {
        three[i]=three[i-1]*3;
    }
    for (i=0;i<three[10];i++)
    {
        p=i;
        up=0;
        while(p!=0)
        {
            hash[i][up++]=p%3;
            p/=3;
        }
    }
}

void Add(int p,int x,int y)
{
    int ret,i,t;
    if (p==0)
    {
        t=1;
        for (i=0;i<n;i++)
        {
            dp[t][i]=0;
            t*=3;
        }
        return;
    }
    if (hash[p][y]==2) return;
    ret=0;
    for (i=n-1;i>=0;i--)
    {
        if (y==i) ret=ret*3+hash[p][i]+1;
        else ret=ret*3+hash[p][i];
    }
  //  printf("%d...%d....%d....%d\n",p,x,y,ret);
    if (dp[ret][y]==-1) dp[ret][y]=dp[p][x]+map[x][y];
    else dp[ret][y]=min(dp[ret][y],dp[p][x]+map[x][y]);
    return;
}

bool Check(int t)
{
    int i;
    for (i=0;i<n;i++)
    {
        if (hash[t][i]==0) return false;
    }
    return true;
}


int main()
{
    int i,j,m,k,x,y,t,ans;
    Init();
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for (i=0;i<n;i++)
        {
            for (j=0;j<n;j++)
            {
                map[i][j]=INF;
            }
        }
        for (i=0;i<m;i++)
        {
            scanf("%d%d%d",&x,&y,&t);
            x--;
            y--;
            map[x][y]=map[y][x]=min(t,map[x][y]);
        }
        memset(dp,-1,sizeof(dp));
        dp[0][0]=0;
        for (i=0;i<three[n];i++)
        {
            for (j=0;j<n;j++)
            {
                if (dp[i][j]==-1) continue;
                for (k=0;k<n;k++)
                {
                    if (j==k) continue;
                    Add(i,j,k);
                }
            }
        }
        ans=INF;
        for (i=0;i<three[n];i++)
        {
            for (j=0;j<n;j++)
            {
             //   printf("%d..%d...%d...%d\n",ans,i,j,dp[i][j]);
                if (dp[i][j]==-1 || dp[i][j]>=ans) continue;
                if (Check(i)==true) ans=dp[i][j];

            }
        }
        printf("%d\n",ans==INF?-1:ans);
    }
    return 0;
}

抱歉!评论已关闭.