HDU3001 Travelling(DP)

来源:互联网 发布:阿里云服务器无法连接 编辑:程序博客网 时间:2024/05/29 03:18

Travelling

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6388    Accepted Submission(s): 2069

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 11 2 1003 21 2 402 3 503 31 2 31 3 42 3 10
 
Sample Output
100907
 
Source
2009 Multi-University Training Contest 11 - Host by HRBEU 
 
Recommend
gaojie
 

注意看清题目,并不是一个点只能走一次,而是一个点不能走超过两次。所以要用三进制表示状态。

/* * HDU3001状态压缩DP */#include <iostream>#include <string.h>#include <algorithm>using namespace std;const int MAXN=15;const int INF=0x3f3f3f3f;int g[MAXN][MAXN];int dp[60000][MAXN];int p[12];int n;int a[12];int ArrayToInt(int a[]){    int ret=0;    for(int i=n-1;i>=0;i--)    {        ret*=3;        ret+=a[i];    }    return ret;}void intToArray(int x){    for(int i=0;i<n;i++)    {        a[i]=x%3;        x/=3;    }}int main(){    p[0]=1;    for(int i=1;i<=11;i++)p[i]=3*p[i-1];    int u,v,w;    int m;    while(scanf("%d%d",&n,&m)==2)    {        memset(g,-1,sizeof(g));        while(m--)        {            scanf("%d%d%d",&u,&v,&w);            u--;            v--;            if(g[u][v]==-1)g[u][v]=g[v][u]=w;            else g[u][v]=g[v][u]=min(w,g[u][v]);        }        if(n==1)        {            printf("0\n");            continue;        }        memset(dp,-1,sizeof(dp));        int tot=0;        for(int i=0;i<n;i++)tot+=p[i];        for(int i=0;i<n;i++)dp[p[i]][i]=0;        int ans=INF;        for(int i=0;i<p[n];i++)            for(int j=0;j<n;j++)            {                intToArray(i);                if(a[j]==0)continue;                if(dp[i][j]<0)continue;                bool full=true;                for(int t=0;t<n;t++)                    if(a[t]==0)                        full=false;                if(full)ans=min(ans,dp[i][j]);                for(int k=0;k<n;k++)                    if(j!=k && g[j][k]!=-1 && a[k]<2)                    {                        a[k]++;                        int s=ArrayToInt(a);                        if(dp[s][k]==-1)dp[s][k]=dp[i][j]+g[j][k];                        else dp[s][k]=min(dp[s][k],dp[i][j]+g[j][k]);                        a[k]--;                    }            }        if(ans==INF)printf("-1\n");        else            printf("%d\n",ans);    }    return 0;}


0 0