zoukankan      html  css  js  c++  java
  • Travelling

    Travelling

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

     

    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
    2009 Multi-University Training Contest 11 - Host by HRBEU
     

     

    Recommend
    gaojie

    把所有的城市访问次数合在一起作为状态的描述,因为每个城市最多访问2次,所以状态压缩的结果是3进制的.
    状态转移方程:DP[i][j]=Min(DP[i.pre][k]+G[k][j])
    DP[i][j]表示达到i状态并且此时位于j城市所需要的最小花费.

    #include<stdio.h>
    #include<string.h>
    int N,M;
    int state[60000][10];
    int DP[60000][10];
    int len[60000];
    int G[15][15];
    int s[15];
    int f(int x)
    {
        int i;
        for (i=0;i<10;i++)
        if (state[x][i]==0) return i;
        return 10;
    }
    void init()
    {
        int i,j;
        s[0]=1;
        for (i=1;i<15;i++) s[i]=s[i-1]*3;
        for (i=0;i<s[10];i++)
        {
            int tmp=i;
            for (j=0;j<10;j++)
            {
                state[i][j]=tmp%3;
                tmp/=3;
            }
        }
        for (i=0;i<s[10];i++) len[i]=f(i);
    }
    int main()
    {
        init();
        while (scanf("%d%d",&N,&M)!=EOF)
        {
            int i,j,k;
            memset(G,0x3f,sizeof(G));
            for (i=1;i<=M;i++)
            {
                int u,v,c;
                scanf("%d%d%d",&u,&v,&c);
                u--;
                v--;
                if (c<G[u][v]) G[u][v]=c;
                if (c<G[v][u]) G[v][u]=c;
            }
            memset(DP,0x3f,sizeof(DP));
            for (i=0;i<N;i++) DP[s[i]][i]=0;
            for (i=0;i<s[N];i++)
             for (j=0;j<N;j++)
             if (state[i][j])
             {
                 int last=i-s[j];
                 for (k=0;k<N;k++)
                 if (DP[last][k]+G[k][j]<DP[i][j] && state[last][k])
                     DP[i][j]=DP[last][k]+G[k][j];
             }
            int Min=100000000;
            for (i=0;i<s[N];i++)
            if (len[i]==N)
             for (j=0;j<N;j++)
             if (state[i][j] && DP[i][j]<Min)
                 Min=DP[i][j];
            if (Min==100000000) printf("-1
    ");
            else printf("%d
    ",Min);
        }
        return 0;
    }

     

  • 相关阅读:
    深入浅出JMS(二)--ActiveMQ简单介绍以及安装
    redis学习
    浏览器的渲染过程
    http请求
    常用正则表达式
    js继承的几种实现方式
    js中的浅拷贝和深拷贝
    webpack常用的plugin
    linux基本命令
    webpack压缩代码组件uglifyjs-webpack-plugin
  • 原文地址:https://www.cnblogs.com/dramstadt/p/3199901.html
Copyright © 2011-2022 走看看