zoukankan      html  css  js  c++  java
  • [HDU 3001] Travelling

    Travelling

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

    Total Submission(s): 4166    Accepted Submission(s): 1339 

    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
     

    状压dp、仔细想一下

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    #define INF 0x3f3f3f3f
    
    int n,m;
    int s[12];
    int mpt[12][12];
    int tr[12][60000];
    int dp[12][60000];
    
    void init()                               //预处理s和tr数组
    {
        int i,j,t;
        s[0]=1;
        for(i=1;i<=10;i++)
        {
            s[i]=3*s[i-1];
        }
        for(j=0;j<s[10];j++) 
        {
            t=j;
            for(i=0;i<10;i++) 
            {
                tr[i][j]=t%3;
                t/=3;
            }
        }
    }
    void solve() 
    {
        int i,j,k,MAX=s[n];
        for(i=0;i<n;i++)
        {
            dp[i][s[i]]=0;
        }
        
        for(j=0;j<MAX;j++)
        {
            for(i=0;i<n;i++)
            {
                if(tr[i][j]==0) continue;
                for(k=0;k<n;k++)
                {
                    if(i==k) continue;
                    if(tr[k][j]==0) continue;
                    int prej=j-s[i];
                    dp[i][j]=min(dp[i][j],dp[k][prej]+mpt[k][i]);
                }
            }
        }
        
        int ans=INF;
        for(i=0;i<n;i++)
        {
            for(j=0;j<s[n];j++)
            {
                for(k=0;k<n;k++)
                {
                    if(tr[k][j]==0) break;    
                }
                if(k==n)
                    ans=min(ans,dp[i][j]);
            }
        }
    
        if(ans==INF)
            puts("-1");
        else
            printf("%d
    ",ans);
    }
    int main ()
    {
        init();
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            memset(dp,INF,sizeof(dp));
            memset(mpt,INF,sizeof(mpt));
            while(m--)
            {
                int a,b,c;
                scanf("%d%d%d",&a,&b,&c);
                a--;
                b--;
                if(c<mpt[a][b]) 
                {
                    mpt[a][b]=c;
                    mpt[b][a]=c;
                }
            }
            solve();
        }
        return 0;
    }
    趁着还有梦想、将AC进行到底~~~by 452181625
  • 相关阅读:
    战略就是做出各种选择和不断权衡取舍;战略就是要刻意与众不同
    获取基目录,它由程序集冲突解决程序用来探测程序集
    Entity Framework的原理及使用方式
    NHibernate使用之详细图解
    最重要的不是你认识多少个人,而是你认识多少种人
    强关系利于执行,弱关系利于创新
    判断它是不是你的社群成员,你要看它对你的态度
    粉丝不在于多,在于够残
    没有请不起的人才,只有付不起的诚意
    所有有可能被互联网取代的组织一定会被取代--颠覆式创新研习社
  • 原文地址:https://www.cnblogs.com/hate13/p/4104729.html
Copyright © 2011-2022 走看看