郊区春游

题解:状压dp,dp[ s ][ i ]表示当前已经游玩的点(R中的点),且终点是 i 的最小花费。预处理出任意两点之间的最短距离。总的来说,面向数据编程。

。。。。。。。心里有句mmp。调试了一个小时,始终0%,所有数据怒开long long,然后过了,,,,,

#include<bits/stdc++.h>
#define ll long long
#define P pair<int,int>
#define pb push_back
#define lson root << 1
#define INF (int)2e9 + 7
#define maxn (int)1e4 + 7
#define rson root << 1 | 1
#define LINF (unsigned long long int)1e18
#define mem(arry, in) memset(arry, in, sizeof(arry))
using namespace std;

ll n, m, R;
ll d[204][204], dp[1 << 16][16], r[20];

void Inite(){
    for(int i = 1; i <= 204; i++){
        for(int j = 1; j <= 204; j++) d[i][j] = (i == j ? 0 : INF);
    }
    for(int i = 0; i < (1 << 16); i++){
        for(int j = 0; j < 16; j++) dp[i][j] = INF;
    }
}

void Floyd(){
    for(int k = 1; k <= n; k++){
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }
}

void Solve(){
    for(int i = 0; i < R; i++) dp[1 << i][i] = 0;
    for(int s = 0; s < (1 << R); s++) {
        for(int u = 0; u < R; u++) if(s & (1 << u)) {
            for(int v = 0; v < R; v++) if(!(s & (1 << v))) {
                dp[s | (1 << v)][v] = min(dp[s | (1 << v)][v], dp[s][u] + d[r[u]][r[v]]);
            }
        }
    }
    res = dp[(1 << R) - 1][0];
    for(int i = 1; i < R; i++) res = min(res, dp[(1 << R) - 1][i]);
    cout << res << endl;
}

int main()
{
    cin >> n >> m >> R;
    for(int i = 0; i < R; i++) cin >> r[i];
    Inite();
    for(int i = 1; i <= m; i++) {
       int u, v, w;
       cin >> u >> v >> w;
       d[u][v] = d[v][u] = w;
    }
    Floyd();
    Solve();
    return 0;
}

 

posted @ 2018-06-17 21:57  天之道,利而不害  阅读(132)  评论(0编辑  收藏  举报