UVA11367 Full Tank?
优先队列bfs
1 #include<cmath> 2 #include<queue> 3 #include<cstdio> 4 #include<string> 5 #include<cstdlib> 6 #include<cstring> 7 #include<iostream> 8 #include<algorithm> 9 using namespace std; 10 #define rg register 11 typedef long long ll; 12 #define gmax(a,b) a=max(a,b) 13 #define FOR(i,a,b) for(rg int i=a;i<=b;++i) 14 #define qxx(i,a) for(rg int i=fir[a];i;i=nxt[i]) 15 #define gc pa==pb&&(pb=(pa=buf)+fread(buf,1,100000,stdin),pa==pb)?EOF:*pa++ 16 static char buf[100000],*pa(buf),*pb(buf); 17 18 inline int rd() 19 { 20 rg int x(0),w(1); 21 rg char ch(gc); 22 while(ch<'0' || ch>'9') 23 { 24 if(ch=='-') w=-1; 25 ch=gc; 26 } 27 while(ch>='0' && ch<='9') x=x*10+ch-48,ch=gc; 28 return x*w; 29 } 30 31 const int N=1010,M=10010,C=105,INF=99999999; 32 struct node 33 { 34 int u,cc,co; 35 node(int U,int CC,int CO){u=U;cc=CC;co=CO;} 36 }; 37 bool operator < (node x,node y){return x.co>y.co;} 38 int f[N][C]; 39 int pr[N],u[M<<1],v[M<<1],w[M<<1],fir[N],nxt[M<<1]; 40 int n,m,tot,c,s,e; 41 priority_queue<node> q; 42 43 void bfs() 44 { 45 while(!q.empty()) q.pop(); 46 q.push(node(s,0,0)); 47 f[s][0]=0; 48 while(!q.empty()) 49 { 50 node p=q.top();q.pop(); 51 //cout<<p.u<<" "<<p.cc<<" "<<p.co<<endl; 52 if(p.u==e) {cout<<p.co<<endl;return;} 53 if(p.cc<c && p.co+pr[p.u]<f[p.u][p.cc+1]) 54 { 55 f[p.u][p.cc+1]=p.co+pr[p.u]; 56 q.push(node(p.u,p.cc+1,p.co+pr[p.u])); 57 } 58 qxx(i,p.u) 59 { 60 if(p.cc>=w[i] && f[v[i]][p.cc-w[i]]>=p.co) 61 { 62 f[v[i]][p.cc-w[i]]=p.co; 63 q.push(node(v[i],p.cc-w[i],p.co)); 64 } 65 } 66 } 67 cout<<"impossible"<<endl; 68 } 69 70 int main() 71 { 72 n=rd(),m=rd(); 73 FOR(i,1,n) pr[i]=rd(); 74 while(tot<(m<<1)) 75 { 76 u[++tot]=rd(),v[tot]=rd(),w[tot]=rd(); 77 ++u[tot],++v[tot]; 78 nxt[tot]=fir[u[tot]],fir[u[tot]]=tot; 79 ++tot;u[tot]=v[tot-1],v[tot]=u[tot-1],w[tot]=w[tot-1]; 80 nxt[tot]=fir[u[tot]],fir[u[tot]]=tot; 81 } 82 int qq=rd(); 83 FOR(i,1,qq) 84 { 85 memset(f,8,sizeof(f)); 86 c=rd(),s=rd(),e=rd(); 87 ++s,++e; 88 bfs(); 89 } 90 return 0; 91 }