zoukankan      html  css  js  c++  java
  • HDU3001 Travelling

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


    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

    看到点数就会想到状压,然而题目限制每个城市不能经过超过两次,二进制难以表示——那就用三进制表示!

    (其实刚开始的想法是二进制相邻两位表示一个城市的到达状态,然而那样1<<20的数组范围吃不消)

    除了三进制以外,这题和普通的状压求最短路没啥差别

     1 /*by SilverN*/
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<cmath>
     7 #include<vector>
     8 using namespace std;
     9 const int mxn=100010;
    10 int read(){
    11     int x=0,f=1;char ch=getchar();
    12     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    13     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
    14     return x*f;
    15 }
    16 int f[60010][12],b[20];
    17 int n,m;
    18 int mp[12][12];
    19 int t[60010][12];
    20 void init(){
    21     for(int i=0;i<60000;i++){
    22         int tmp=i;
    23         for(int j=1;j<12;j++){
    24             t[i][j]=tmp%3;
    25             tmp/=3;
    26         }
    27     }
    28     return;
    29 }
    30 int main(){
    31     int i,j,k,u,v,w;
    32     b[1]=1;
    33     for(i=2;i<12;i++)b[i]=b[i-1]*3;
    34     init();
    35     while(scanf("%d%d",&n,&m)!=EOF){
    36         memset(mp,0x3f,sizeof mp);
    37         memset(f,0x3f,sizeof f);
    38         for(i=1;i<=m;i++){
    39             u=read();v=read();w=read();
    40             mp[u][v]=mp[v][u]=min(mp[u][v],w);
    41         }
    42         for(i=1;i<=n;i++){
    43             f[b[i]][i]=0;
    44         }
    45         int ans=0x3f3f3f3f;
    46         int ed=b[n+1]-1;
    47         for(i=0;i<=ed;i++){
    48             bool all=1;
    49             for(j=1;j<=n;j++){
    50                 if(!t[i][j]){
    51                     all=0;continue;
    52                 }
    53                 for(k=1;k<=n;k++){
    54                     if(j==k)continue;
    55                     if(t[i][k]>1)continue;
    56                     f[i+b[k]][k]=min(f[i+b[k]][k],f[i][j]+mp[j][k]);
    57                 }
    58             }
    59             if(all){
    60                 for(j=1;j<=n;j++)
    61                     ans=min(ans,f[i][j]);
    62             }
    63         }
    64         if(ans==0x3f3f3f3f)ans=-1;
    65         printf("%d
    ",ans);
    66     }
    67     return 0;
    68 }
  • 相关阅读:
    ViewPager留出边 显示左右两边的视图
    Retrofit 2.0 上传文件
    android一个app打开另一个app的指定页面
    Java多线程消费者、生产者的基本思路
    Android 8.0+ 更新安装apk失败的问题
    Android 8.0+ 通知不显示的适配
    android 7.0+ FileProvider 访问隐私文件 相册、相机、安装应用的适配
    android 6.0+ 动态权限 拒绝不再询问后跳转设置应用详情页面
    ViewPager中Fragment的重复创建、复用问题
    Android源码学习(2) Handler之Looper
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/6435387.html
Copyright © 2011-2022 走看看