zoukankan      html  css  js  c++  java
  • bc #52 div 2 B || hdoj 5418 Victor and World(tsp问题,状压dp)

    Victor and World

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)
    Total Submission(s): 223    Accepted Submission(s): 102


    Problem Description
    After trying hard for many years, Victor has finally received a pilot license. To have a celebration, he intends to buy himself an airplane and fly around the world. There are n countries on the earth, which are numbered from 1 to n. They are connected by m undirected flights, detailedly the i-th flight connects the ui-th and the vi-th country, and it will cost Victor's airplane wi L fuel if Victor flies through it. And it is possible for him to fly to every country from the first country.

    Victor now is at the country whose number is 1, he wants to know the minimal amount of fuel for him to visit every country at least once and finally return to the first country.
     
    Input
    The first line of the input contains an integer T, denoting the number of test cases.
    In every test case, there are two integers n and m in the first line, denoting the number of the countries and the number of the flights.

    Then there are m lines, each line contains three integers uivi and wi, describing a flight.

    1T20.

    1n16.

    1m100000.

    1wi100.

    1ui,vin.
     
    Output
    Your program should print T lines : the i-th of these should contain a single integer, denoting the minimal amount of fuel for Victor to finish the travel.
     
    Sample Input
    1 3 2 1 2 2 1 3 3
     
    Sample Output
    10
     
    Source
     

    sad

    比赛的时候一看,tsp么

    前些天好像刚写过一个clean robot什么的

    然后发现n才16,而m很大..

    应该有很多重复的.

    那么我们取油费最少的.

    然后先做一遍 floyd

    之后我写了一个dfs...TLE 了...sad

    正解是状压dp

    虽然没写过状压dp

    但是之前写过一道状态压缩的bfs

    所以理解起来没有问题.

    这道题的做法是: 

    用dp[i][j]表示当前访问国家的状态为s,要访问的国家为j的时候的最小费用.i是二进制,i的第p位为1表示第p个国家已经访问过了,否则表示没有访问过.

    那么状态转移方程则为:dp[i|(1<<k)][k] = min(dp[i|(1<<K)][k],dp[i][j]+dp[j][k]);

    初始化的时候d[i][i] =0,其他为inf

    dp[0][0] 表示要访问第一个国家且没有访问国人国家的时候的状态,此时花费为0

    然后先枚举访问国家的状态,再枚举访问的国家.

      1 /*************************************************************************
      2     > File Name: code/bc/#52/rrr1002.cpp
      3     > Author: 111qqz
      4     > Email: rkz2013@126.com 
      5     > Created Time: 2015年08月22日 星期六 22时33分20秒
      6  ************************************************************************/
      7 
      8 #include<iostream>
      9 #include<iomanip>
     10 #include<cstdio>
     11 #include<algorithm>
     12 #include<cmath>
     13 #include<cstring>
     14 #include<string>
     15 #include<map>
     16 #include<set>
     17 #include<queue>
     18 #include<vector>
     19 #include<stack>
     20 #include<cctype>
     21 #define y1 hust111qqz
     22 #define yn hez111qqz
     23 #define j1 cute111qqz
     24 #define ms(a,x) memset(a,x,sizeof(a))
     25 #define lr dying111qqz
     26 using namespace std;
     27 #define For(i, n) for (int i=0;i<int(n);++i)  
     28 typedef long long LL;
     29 typedef double DB;
     30 const int inf = 0x3f3f3f3f;
     31 const int N=17;
     32 int d[N][N];
     33 int n,m;
     34 int dp[(1<<16)+5][N];
     35 
     36 void floyd(){
     37     For(k,n){
     38     For(i,n){
     39         For (j,n){
     40         d[i][j] = min ( d[i][j] , d[i][k]+d[k][j]);
     41         }
     42     }
     43     }
     44 
     45 }
     46 
     47 void solve()
     48 {
     49     dp[0][0] = 0;
     50     for ( int i = 0 ; i <(1<<n) ; i ++){
     51     for ( int j = 0 ; j < n ;j++){
     52         if (dp[i][j]!=inf){
     53         for (int k = 0 ; k < n ; k++){
     54             int tmp = i|(1<<k);
     55             dp[tmp][k] = min(dp[tmp][k],dp[i][j] + d[j][k]);
     56         }
     57         }
     58     }
     59     }
     60 }
     61 int main()
     62 {
     63   #ifndef  ONLINE_JUDGE 
     64     freopen("in.txt","r",stdin);
     65   #endif
     66     int T;
     67     cin>>T;
     68     while (T--){
     69     scanf("%d %d",&n,&m);
     70     ms(d,inf);
     71     ms(dp,inf);
     72     For(i,n){
     73         d[i][i] = 0 ;
     74     }
     75     int u,v,w;
     76     For(i,m){
     77         scanf("%d %d %d",&u,&v,&w);
     78         u--;v--;
     79         d[u][v] = d[v][u] =min(d[u][v],w);
     80     }
     81     floyd();
     82 //    for ( int i = 0 ; i < n; i++){
     83 //        for ( int j = 0 ; j < n ; j++){
     84 //        cout<<d[i][j]<<" ";
     85 //        
     86 //        }
     87 //        cout<<endl;
     88 
     89 //    }
     90     solve();
     91     int ans = dp[(1<<n)-1][0];
     92     printf("%d
    ",ans);
     93 
     94     }
     95   
     96   
     97  #ifndef ONLINE_JUDGE  
     98   fclose(stdin);
     99   #endif
    100     return 0;
    101 }
    View Code
  • 相关阅读:
    团队冲刺第二阶段-7
    用户体验评价
    团队冲刺第二阶段-6
    第十四周学习进度报告
    团队冲刺第二阶段-5
    团队冲刺第二阶段-4
    14周课堂测试---找水王
    进度日报14
    进度日报13
    进度日报12
  • 原文地址:https://www.cnblogs.com/111qqz/p/4751334.html
Copyright © 2011-2022 走看看