{"id":34,"date":"2009-11-18T17:58:00","date_gmt":"2009-11-18T09:58:00","guid":{"rendered":"http:\/\/localhost\/?p=34"},"modified":"2015-04-24T19:39:25","modified_gmt":"2015-04-24T19:39:25","slug":"the_spoj_151_state_compression_dp","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/wjmzbmr\/?p=34","title":{"rendered":"SPOJ 151 \u72b6\u6001\u538b\u7f29DP"},"content":{"rendered":"\n<p>\u8fd9\u9053\u9898\u7b97\u7ecf\u5178\u7684\u72b6\u538bDP\u4e86\u3002\u3002<\/p>\n<p>DP[S][P]\u8868\u793a\u5b8c\u6210\u4e86S\u96c6\u5408\u7684\u4efb\u52a1\uff0c\u73b0\u5728\u5728\u7b2cP\u4e2a\u4efb\u52a1\u7684\u843d\u811a\u70b9\u3002\u3002\u3002<\/p>\n<p>\u90a3\u4e48\u76f4\u63a5\u63a8\u5c31OK\u4e86\u3002\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include&lt;iostream&gt;\r\n#include&lt;string.h&gt;\r\n#include&lt;queue&gt;\r\n#define REP(i,n) for(int i=0;i&lt;n;i++)\r\n#define Renew(x,c) x=min(x,c)\r\n#define floyd(d) REP(k,n) REP(i,n) REP(j,n) Renew(d[i][j],d[i][k]+d[k][j]);\r\nusing namespace std;\r\nconst int maxn=120,inf=~0U&gt;&gt;3,maxb=13;\r\nint n,m,b,cnt=0;\r\nint dist[maxn][maxn];\r\nint Go[maxb+1],Reach[maxb+1];\r\n\r\nvoid init(){\r\n    cin&gt;&gt;n&gt;&gt;m&gt;&gt;b;b--;cnt=0;\r\n    REP(i,n) REP(j,n) dist[i][j]=(i==j?0:inf);\r\n    while(m--) {\r\n        int s,t,c;cin&gt;&gt;s&gt;&gt;t&gt;&gt;c;s--;t--;\r\n        dist[s][t]=dist[t][s]=min(c,dist[s][t]);\r\n    }\r\n    int z;cin&gt;&gt;z; while(z--) {\r\n        int s,t,c;cin&gt;&gt;s&gt;&gt;t&gt;&gt;c;s--;t--; while(c--) {\r\n            Go[cnt]=s;Reach[cnt]=t;cnt++;\r\n        }\r\n    }\r\n    Reach[cnt]=b;\r\n}\r\n\r\nint dp[1&lt;&lt;maxb][maxb+1];\r\n\r\nstruct node{\r\n    int f,p;\r\n    node(int f,int p):f(f),p(p){}\r\n};\r\n\r\nvoid solve(){\r\n    floyd(dist); memset(dp,0,sizeof(dp));\r\n    queue&lt;node&gt; Q; Q.push(node(0,cnt)); while(Q.size()){\r\n        node cur=Q.front();Q.pop();\r\n        int cost=dp[cur.f][cur.p];\r\n        for(int i=0;i&lt;cnt;i++)if(!(cur.f&amp;(1&lt;&lt;i))) {\r\n            int nf=cur.f|(1&lt;&lt;i); int get=cost+dist[Reach[cur.p]][Go[i]]+dist[Go[i]][Reach[i]];\r\n            if(dp[nf][i]==0||dp[nf][i]&gt;get) dp[nf][i]=get,Q.push(node(nf,i));\r\n        }\r\n    }\r\n    int ans=inf; REP(i,cnt) Renew(ans,dp[(1&lt;&lt;cnt)-1][i]+dist[Reach[i]][b]); cout&lt;&lt;ans&lt;&lt;endl;\r\n}5\r\n\r\nint main(){\r\n    int t;cin&gt;&gt;t; while(t--) {\r\n        init(); solve();\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u9053\u9898\u7b97\u7ecf\u5178\u7684\u72b6\u538bDP\u4e86\u3002\u3002 DP[S][P]\u8868\u793a\u5b8c\u6210\u4e86S\u96c6\u5408\u7684\u4efb\u52a1\uff0c\u73b0\u5728\u5728\u7b2cP\u4e2a\u4efb\u52a1\u7684\u843d\u811a\u70b9\u3002\u3002\u3002 \u90a3\u4e48\u76f4\u63a5\u63a8\u5c31OK\u4e86\u3002\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=\/wp\/v2\/posts\/34"}],"collection":[{"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=34"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=\/wp\/v2\/posts\/34\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=34"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=34"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}