1. 程式人生 > >BZOJ1139: [POI2009]Wie(洛谷P3489)

BZOJ1139: [POI2009]Wie(洛谷P3489)

最短路 狀壓

f[i][s]f[i][s]為到點ii能殺的怪的狀態為ss的最短路。用Dij刷分層圖最短路的時候轉移就好了。

程式碼:

#include<queue>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 205
#define M 3005
#define F inline
using namespace std;
struct edge{ int nxt,to,d,s; }ed[M<<
1]; struct node{ int x,d,s; }; int n,m,k,K,p,h[N],c[N],f[N][1<<14]; bool ff[N][1<<14]; priority_queue<node> q; F char readc(){ static char buf[100000],*l=buf,*r=buf; if (l==r) r=(l=buf)+fread(buf,1,100000,stdin); return l==r?EOF:*l++; } F int _read(){ int x=0; char ch=readc(); while (!isdigit
(ch)) ch=readc(); while (isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=readc(); return x; } #define add(x,y,z,s) ed[++K]=(edge){h[x],y,z,s},h[x]=K F bool operator < (node a,node b){ return a.d>b.d; } F int Dij(){ for (int i=1;i<=n;i++) for (int j=0;j<(1<<p);j++) f[i][j]=1e9
; for (f[1][0]=0,q.push((node){1,0,0});!q.empty();){ int x=q.top().x,d=q.top().d,s=q.top().s; q.pop(); if (x==n) return d; if (ff[x][s]) continue; ff[x][s]=true,s|=c[x]; for (int i=h[x],v;i;i=ed[i].nxt) if ((ed[i].s&s)==ed[i].s&&d+ed[i].d<f[v=ed[i].to][s]) f[v][s]=d+ed[i].d,q.push((node){v,f[v][s],s}); } return -1; } int main(){ n=_read(),m=_read(),p=_read(),k=_read(); for (int i=1;i<=k;i++) for (int x=_read(),j=_read();j;j--) c[x]|=1<<_read()-1; for (int i=1,x,y,z,s;i<=m;i++){ x=_read(),y=_read(),z=_read(),s=0; for (int j=_read();j;j--) s|=1<<_read()-1; add(x,y,z,s),add(y,x,z,s); } return printf("%d\n",Dij()),0; }