Loading

题解 P3451 [POI2007]ATR-Tourist Attractions

题解

这里的做法是卡空间的做法,相比于滚动数组,这种做法因为没有三维数组寻址的大常数,所以较快。

在普通的做法中,\(dp[state][i]\) 表示以 \(i\) 结尾,那么 \(state\) 一定是包含 \(i\) 的状态,所以在 \(state\) 中可以省掉 \(i\) 这一位

所以 \(cost=(k+1)×2^{k-1}×4kb\) ,大约为 \(42MB\)

注:本题用 \(spfa\) 会比 \(dijkstra\) 快很多

Code:
#include<bits/stdc++.h>
#define ri register signed
#define p(i) ++i
using namespace std;
namespace IO{
    char buf[1<<21],*p1=buf,*p2=buf;
    #define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++
    template<typename T>inline void read(T &x) {
        ri f=1;x=0;register char ch=gc();
        while(ch<'0'||ch>'9') {if (ch=='-') f=0;ch=gc();}
        while(ch>='0'&&ch<='9') {x=(x<<1)+(x<<3)+(ch^48);ch=gc();}
        x=f?x:-x;
    }
}
using IO::read;
namespace nanfeng{
    #define node(x,y) (node){x,y}
    #define cmax(x,y) ((x)>(y)?(x):(y))
    #define cmin(x,y) ((x)>(y)?(y):(x))
    #define FI FILE *IN
    #define FO FILE *OUT
    static const int N=2e4+7,M=2e5+7;
    int first[N],dp[1<<19][21],pre[21],dis[21][N],vis[N],n,m,q,k,t=2,S;
    struct edge{int v,w,nxt;}e[M<<1];
    struct node{int x,dis;};
    priority_queue<node> que;
    inline int operator<(const node &n1,const node &n2) {return n1.dis>n2.dis;}
    inline void add(int u,int v,int w) {
        e[t].v=v,e[t].w=w,e[t].nxt=first[u],first[u]=t++;
        e[t].v=u,e[t].w=w,e[t].nxt=first[v],first[v]=t++;
    }
    inline void dij(int rt){
        memset(vis,0,sizeof(vis));
        memset(dis[rt-1],0x3f,sizeof(dis[rt-1]));
        dis[rt-1][rt-1]=0;
        que.push(node(rt,0));rt-=1;
        while(!que.empty()) {
            int x(que.top().x),dist(que.top().dis);
            que.pop();
            if (vis[x]) continue;
            vis[x]=1;
            for (ri i(first[x]),v;i;i=e[i].nxt) {
                if (dis[rt][v=e[i].v-1]>dist+e[i].w) {
                    dis[rt][v]=dist+e[i].w;
                    que.push(node(v+1,dis[rt][v]));
                }
            }
        }
    }
    inline int calc(int x,int l) {
        if ((1<<l)>x) return x;
        ri tmp=x&((1<<l)-1);
        return (x>>(l+1))<<l|tmp; 
    }
    inline int main() {
        // FI=freopen("nanfeng.in","r",stdin);
        // FO=freopen("nanfeng.txt","w",stdout);
        read(n),read(m),read(k);
        for (ri i(1),u,v,w;i<=m;p(i)) read(u),read(v),read(w),add(u,v,w);
        read(q);
        for (ri i(1),r,s;i<=q;p(i)) {
            read(r);read(s);
            pre[s-2]|=1<<(r-2);
        }
        for (ri i(1);i<=k+1;p(i)) dij(i);
        if (!k) {printf("%d\n",dis[0][n-1]);return 0;} 
        memset(dp,0x3f,sizeof(dp));
        dp[0][0]=0;
        for (ri i(1);i<=k;p(i)) if (!pre[i-1]) dp[0][i]=dis[0][i];
        S=(1<<k)-1;
        for (ri i(1);i<=S;p(i)) {
            for (ri j(0);j<k;p(j)) {
                if (!((1<<j)&i)) continue;
                ri tmp1=calc(i,j);
                for (ri l(0);l<k;p(l)) {
                    if (!(i&(1<<l))&&((i&pre[l])==pre[l])) {
                        ri tmp2=calc(i,l);
                        dp[tmp2][l+1]=cmin(dp[tmp1][j+1]+dis[j+1][l+1],dp[tmp2][l+1]);
                    }
                }  
            }
        }
        ri ans=INT_MAX;
        for (ri i(1),tmp;i<=k;p(i)) {
            tmp=calc(S,i-1);
            ans=cmin(ans,dp[tmp][i]+dis[i][n-1]);    
        }
        printf("%d\n",ans);
        return 0;
    } 
}
int main() {return nanfeng::main();}
posted @ 2021-07-10 20:26  ナンカエデ  阅读(118)  评论(0编辑  收藏  举报