poj 3635(full tank?)
2011-10-06 17:36
417 查看
我感觉是dp类型的吧。。。。
discuss里一个讲解把状态的设置说的很好
discuss里一个讲解把状态的设置说的很好
设一个 money[1001][101] 表示 到点i时, 油量为j 的最小花费; 然后用dijstra的广搜变种来搜即可: 每次找一个最小花费点, if money[x][y + 1]满足, 拓展入队即可; 然后再更新x的邻点入队拓展即可;
#include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; #define inf 900000000 #define M 1010 #define N 100000 struct node1{ int v,w,next; }edge ; int head[M],p,a[M],dp[M][105],vis[M][105],c,s,e; struct node{ int v,fuel,cst; bool operator <(const node &k) const{ return k.cst<cst; } }; priority_queue<node> que; node u,nex; void ainit(){ p=0,memset(head,-1,sizeof(head)); } int bfs(int n){ int i,j,v,f,len; for(i=0;i<=n;i++) for(j=0;j<=c;j++) dp[i][j]=inf,vis[i][j]=0; while(!que.empty()) que.pop(); //少些这一句,wa了很长时间。。。 dp[s][0]=0,u.v=s,u.cst=0,u.fuel=0; que.push(u); while(!que.empty()){ u=que.top(),que.pop(); v=u.v,f=u.fuel,len=u.cst; if(v==e) return len; vis[v][f]=1; if(f+1<=c && !vis[v][f+1] && dp[v][f+1]>dp[v][f]+a[v]){ dp[v][f+1]=dp[v][f]+a[v]; nex.v=v,nex.fuel=f+1,nex.cst=dp[v][f+1]; que.push(nex); } for(i=head[v];i!=-1;i=edge[i].next){ int t=edge[i].v,k=f-edge[i].w; if(k>=0 && !vis[t][k] && len<dp[t][k]){ dp[t][k]=len; nex.v=t,nex.fuel=k,nex.cst=dp[t][k]; que.push(nex); } } } return -1; } void addedge(int u,int v,int w){ edge[p].v=v,edge[p].w=w,edge[p].next=head[u],head[u]=p++; edge[p].v=u,edge[p].w=w,edge[p].next=head[v],head[v]=p++; } int main(){ int i,cas,n,m,u,v,w; while(scanf("%d%d",&n,&m)!=-1){ ainit(); for(i=0;i<n;i++) scanf("%d",&a[i]); while(m--){ scanf("%d%d%d",&u,&v,&w); addedge(u,v,w); } scanf("%d",&cas); while(cas--){ scanf("%d%d%d",&c,&s,&e); int ans=bfs(n); if(ans==-1) printf("impossible\n"); else printf("%d\n",ans); } } return 0; }
相关文章推荐
- Full Tank? POJ - 3635 二维最短路(dp?)
- POJ 3635 Full Tank?
- poj 3635 Full Tank?
- POJ-3635 Full Tank? (记忆化广搜)
- poj3635—Full Tank?(spfa+dp)
- POJ-3635-Full Tank?
- POJ 3635 Full Tank?
- POJ 3635 Full Tank? 最短路变形
- POJ 3635 Full Tank?
- POJ 3635 Full Tank(最短路径变形 + 优先队列)
- poj 3635 Full Tank 最短路
- poj 3635 Full Tank 动态规划思想在spfa算法中的应用
- poj 3635 Full Tank? ( bfs+dp思想 )
- poj 3635 Full Tank? ( bfs+dp思想 )
- poj 3635 Full Tank? 广搜?
- POJ 3635:Full Tank?
- POJ 3635 Full Tank? (dp)
- poj 3635 Full Tank? ( 图上dp )
- POJ 3635 Full Tank?
- POJ 3635 Full Tank?(BFS)