• HDU 3001 Travelling


    Travelling

    Time Limit: 3000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 3001
    64-bit integer IO format: %I64d      Java class name: Main
     
    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

     
    解题:状压dp...
     
    s[i][j]表示在状态i下,city j被访问了 多少次。
     
    只有s[i][j]中的所有j都大于0 即至少访问过一次,才能把此态视为终态。否则,不能视为终态。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 int tri[] = {0,1,3,9,27,81,243,729,2187,6561,19683,59049};
    18 int e[12][12],dp[59049][12],s[59049][12] = {0};
    19 int main() {
    20     int n,m,x,y,z;
    21     for(int i = 0; i < 59049; i++) {
    22         int tmp = i;
    23         for(int k = 1; k <= 10; k++) {
    24             s[i][k] = tmp%3;
    25             tmp /= 3;
    26         }
    27     }
    28     while(~scanf("%d %d",&n,&m)) {
    29         for(int i = 1; i <= n; i++)
    30             for(int j = 1; j <= n; j++) e[i][j] = INF;
    31         for(int i = 0; i < m; i++) {
    32             scanf("%d %d %d",&x,&y,&z);
    33             if(e[x][y] > z) e[x][y] = e[y][x] = z;
    34         }
    35         for(int i = 0; i < tri[n+1]; i++)
    36             for(int j = 1; j <= 10; j++) dp[i][j] = INF;
    37         for(int i = 0; i <= 10; i++) dp[tri[i]][i] = 0;
    38         int ans = INF;
    39         for(int i = 0; i < tri[n+1]; i++) {
    40             bool flag = true;
    41             for(int j = 1; j <= n; j++) {
    42                 if(s[i][j] == 0) flag = false;
    43                 for(int k = 1; k <= n; k++){
    44                     if(j == k || e[j][k] == INF || s[i][k] == 2) continue;
    45                     int ns = i + tri[k];
    46                     dp[ns][k] = min(dp[ns][k],dp[i][j] + e[j][k]);
    47                 }
    48             }
    49             for(int k = 1; flag && k <= n; k++)
    50                 ans = min(ans,dp[i][k]);
    51         }
    52         ans == INF?puts("-1"):printf("%d
    ",ans);
    53     }
    54     return 0;
    55 }
    View Code
  • 相关阅读:
    第三第四周的笔记
    第一二周的笔记
    jQuery的一些笔记
    函数的执行环境与调用对象
    从click事件理解DOM事件流
    慕课编程题JS选项卡切换
    adb(11)-重新挂载 system 分区为可写
    adb(10)-屏幕截图/录制
    adb(9)-查看设备信息
    adb(8)-查看日志
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4004924.html
Copyright © 2020-2023  润新知