• 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;
    }

     

  • 相关阅读:
    [转]html5 Canvas画图教程(6)—canvas里画曲线之arcTo方法
    [转]html5 Canvas画图教程(5)—canvas里画曲线之arc方法
    [转]html5 Canvas画图4:填充和渐变
    [转]html5 Canvas画图教程(3)—canvas出现1像素线条模糊不清的原因
    [转]html5 Canvas画图教程(2)—画直线与设置线条的样式如颜色/端点/交汇点
    【Java进阶】Maven 简介
    【Java入地】Spring 的 作用域 & MVC & SSM
    【Linux】Kali 物理机安装笔记、简单过程及注意事项
    【电脑维修】 显卡错误(错误代码:43)
    【Linux】 编程环境搭建 JDK、Node.js、Npm、Yarn、Redis、Maven的安装与搭建
  • 原文地址:https://www.cnblogs.com/dramstadt/p/3199901.html
Copyright © 2020-2023  润新知