zoukankan      html  css  js  c++  java
  • Travelling HDU

    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.

    InputThere 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.OutputOutput 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 
    经典的旅行商问题Traveling Salesman Problem,TSP;
    当n=10时有3

    10

    组合,对每种路径用3进制表示,复杂度O(3

    n

    n

    2

    )
    //#include <bits/stdc++.h>
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include<cstring>
    #include <algorithm>
    #include <queue>
    #include<map>
    #include<set>
    #include<vector>
    using namespace std;
    typedef long long ll;
    const int inf = 0x3f3f3f3f;
    const int mod = 1e9;
    const int mx = 1e7; //check the limits, dummy
    typedef pair<int, int> pa;
    const double PI = acos(-1);
    ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
    #define swa(a,b) a^=b^=a^=b
    #define re(i,a,b) for(int i=(a),_=(b);i<_;i++)
    #define rb(i,a,b) for(int i=(a),_=(b);i>=_;i--)
    #define clr(a) memset(a, inf, sizeof(a))
    #define lowbit(x) ((x)&(x-1))
    #define mkp make_pai
    //void sc(int& x) { scanf("%d", &x); }void sc(int64_t& x) { scanf("%lld", &x); }void sc(double& x) { scanf("%lf", &x); }void sc(char& x) { scanf(" %c", &x); }void sc(char* x) { scanf("%s", x); }
    int n, m,t;
    int bit[12] = { 0,1,3,9,27,81,243,729,2187,6561,19683,59049 };//3进制每一位的权值
    int tr[60000][11],dp[11][60000],graph[11][11];
    void init() {
        for(int i=0;i<59050;++i) {
            int t = i;
            for(int j=1;j<=10;++j)tr[i][j] = t % 3, t /= 3;
        }
    }
    int comp_dp() {
        int ans = inf;
        clr(dp);
        for(int i=0;i<=n;i++){
            dp[i][bit[i]] = 0;
        }
        for(int i=0;i<bit[n+1];i++) {
            int flag = 1;//所有城市都遍历过1次以上
            for(int j=1;j<=n;j++) {
                if (tr[i][j] == 0) {
                    //judge ending point的位是否为0
                    flag = 0; continue;
                }
                if (i == j)continue;
                for(int k=1;k<=n;k++) {
                    int l = i - bit[j];
                    if (tr[i][k] == 0)continue;
                    dp[j][i] = min(dp[j][i], dp[k][l] + graph[k][j]);
                }
            }
            if (flag)//find the minimum fees
                for(int j=1;j<=n;j++)
                ans = min(ans, dp[j][i]);
        }
        return ans;
    }
    int main()
    {
        ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
        init();
        while (cin >> n >> m)
        {
            clr(graph);
            while (m--) {
                int a, b, c;
                cin >> a >> b >> c;
                if (c < graph[a][b])graph[a][b] = graph[b][a] = c;
            }
            int ans = comp_dp();
            if (ans == inf)cout << -1 << endl;
            else cout << ans << endl;
        }
        return 0;
    }
  • 相关阅读:
    MySQL主从复制-指定数据库复制
    linux下如何添加一个用户并且让用户获得root权限
    vim加密文件
    nginx_upstream_check_module-master对nginx的后端机器进行健康状态检查报403错误【转】
    Nginx配置SSL证书部署HTTPS网站
    MySQL数据备份之mysqldump使用
    nginx location语法使用说明
    Linux 使用 iptables屏蔽IP段
    nginx中获取真实ip
    在nginx中配置如何防止直接用ip访问服务器web server及server_name特性讲解
  • 原文地址:https://www.cnblogs.com/xxxsans/p/12795010.html
Copyright © 2011-2022 走看看