【hdu 5418】 Victor and world

【题目链接】

          点击打开链接

【算法】

         状压DP

         f[i][S]表示走的最后一步在i,状态为S

         于是我们可以用最短路径 + 状压DP解决此题,由于不存在负边,所以可以用dijkstra+堆优化

 

【代码】

        

#include<bits/stdc++.h>
using namespace std;
#define MAXN 16

struct info {
    int home,s,dis;    
    friend bool operator < (info a,info b) {
        return a.dis > b.dis;
    }
};

int T,N,M,i,j,k,mask,u,v,w;
int dist[MAXN+10][MAXN+10],f[MAXN+10][(1<<MAXN)+10],vis[MAXN+10][(1<<MAXN)+10];
priority_queue<info> q;
info x;

template <typename T> inline void read(T &x) {
    int f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
    for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
    x *= f;
}
template <typename T> inline void write(T x) {
    if (x < 0) { putchar('-'); x = -x; }
    if (x > 9) write(x/10);
    putchar(x%10+'0');    
}
template <typename T> inline void writeln(T x) {
    write(x);
    puts("");    
}

int main() {
    
    read(T);
    
    while (T--) {
        memset(vis,0,sizeof(vis));
        read(N); read(M);
        for (i = 1; i <= N; i++) {
            for (j = 1; j <= N; j++) {
                dist[i][j] = 2e9;
            }
        } 
        for (i = 1; i <= M; i++) {
            read(u); read(v); read(w);
            dist[u][v] = min(dist[u][v],w);
            dist[v][u] = min(dist[v][u],w);
        }    
        mask = (1 << N) - 1;
        for (i = 1; i <= N; i++) {
            for (j = 1; j <= mask; j++) {
                f[i][j] = 2e9;
            }
        }
        f[1][0] = 0;
        q.push((info){1,0,0});
        while (!q.empty()) {
            x = q.top(); q.pop();
            if (vis[x.home][x.s]) continue;
            vis[x.home][x.s] = 1;
            for (i = 1; i <= N; i++) {
                if (x.dis + dist[x.home][i] < f[i][x.s|(1<<(i-1))]) {
                    f[i][x.s|(1<<(i-1))] = x.dis + dist[x.home][i];
                    q.push((info){i,x.s|(1<<(i-1)),f[i][x.s|(1<<(i-1))]});    
                }
            }
        }
        if (N == 1) writeln(0);
        else writeln(f[1][(1<<N)-1]);
    }    
    
    return 0;
    
}

 

posted @ 2018-02-12 11:52  evenbao  阅读(132)  评论(0编辑  收藏  举报