NBUT 1221 Intermediary
2017-03-28 18:17
211 查看
最短路,三进制状态压缩。
$dis[i][j]$表示到$i$节点,每个中介用了几次的情况下的最小花费,跑最短路即可。
$dis[i][j]$表示到$i$节点,每个中介用了几次的情况下的最小花费,跑最短路即可。
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map> #include<queue> #include<string> #include<iostream> using namespace std; int n,m,q; int e[110],f[110],r[10]; struct X { int a,b,z,d; int nx; }ee[11000]; struct Y { int id,st,dis; Y(int ID,int ST,int DIS) { id=ID; st=ST; dis=DIS; } bool operator <(const Y&y) const { return dis>y.dis; } }; int h[110], dis[110][20000]; int t[10]; void dij() { for(int i=0;i<n;i++) for(int j=0;j<t[m];j++) dis[i][j]=0x7FFFFFFF; dis[0][0]=0; priority_queue<Y>Q; Q.push(Y(0,0,0)); while(!Q.empty()) { Y q = Q.top(); Q.pop(); if(q.dis>dis[q.id][q.st]) continue; if(q.id==n-1) { printf("%d\n",q.dis); return ; } for(int i=h[q.id];i!=-1;i=ee[i].nx) { int tmp=q.st; for(int j=0;j<m;j++) r[j]=tmp%3,tmp/=3; int cost = ee[i].d, v = 1; if(r[ee[i].z]==1) cost += e[ee[i].z]; else if(r[ee[i].z]==2) cost += f[ee[i].z],v=0; if(q.dis+cost<dis[ee[i].b][q.st+v*t[ee[i].z]]) { dis[ee[i].b][q.st+v*t[ee[i].z]] = q.dis+cost; Q.push(Y(ee[i].b,q.st+v*t[ee[i].z],dis[ee[i].b][q.st+v*t[ee[i].z]])); } } } } int main() { t[0]=1; for(int i=1;i<=9;i++) t[i]=3*t[i-1]; while(~scanf("%d%d%d",&n,&m,&q)) { for(int i=0;i<m;i++) scanf("%d",&e[i]); for(int i=0;i<m;i++) scanf("%d",&f[i]); memset(h,-1,sizeof h); for(int i=1;i<=q;i++) { scanf("%d%d%d%d",&ee[i].a,&ee[i].b,&ee[i].z,&ee[i].d); ee[i].nx = h[ee[i].a]; h[ee[i].a]=i; } dij(); } return 0; }
相关文章推荐
- NBUT 1221 Intermediary(优先队列+dijkstra)(状态压缩)
- NBUT 1221 Intermediary(优先队列+dijkstra)(状态压缩)
- NBUT 1221 Intermediary(优先队列+dijkstra)(状态压缩)
- NBUT 1221 Intermediary 2010辽宁省赛
- NBUT 1221 Intermediary(优先队列+dijkstra)(状态压缩)
- NBUT 1221 Intermediary(优先队列+dijkstra)(状态压缩)
- NBUT 1221 Intermediary(优先队列+dijkstra)(状态压缩)
- NBUT 1221 Intermediary(优先队列+dijkstra)(状态压缩)
- NBUT 1221 Intermediary(优先队列+dijkstra)(状态压缩)
- NBUT 1221 Intermediary(优先队列+dijkstra)(状态压缩)
- NBUT 1221 Intermediary(优先队列+dijkstra)(状态压缩)
- NBUT 1221 Intermediary(优先队列+dijkstra)(状态压缩)
- NBUT 1221 Intermediary(优先队列+dijkstra)(状态压缩)
- NBUT 1221 Intermediary(优先队列+dijkstra)(状态压缩)
- NBUT 1221 Intermediary(优先队列+dijkstra)(状态压缩)
- NBUT 1221 Intermediary(Bellman-Ford)(最短路三进制状态压缩)
- but when call at some dlg,only no echo,but after add a Invalidate() ,it will be ok very strange.
- JBOSS,datasource,But In the init() mothod couldn't lookup datasource.
- Intel Core - Continue discussion here, but only in English
- API mixerGetNumDevs But why, d1