poj 3635 Full Tank? 广搜?
2017-08-17 17:57
405 查看
题目链接:http://poj.org/problem?id=3635
题意:
某人要开车出去旅行,他发现每个地方的油价不相同,为了节省路费,找到一个适当的加油方式使得从s到t的价钱最小。一单位油可以走一单位距离。思路:
每个点扩展出c个状态d[u][o],代表在第u个城市,拥有o单位的油所需的最小价钱。那么在每个点的每个状态,假设为u(与之相连的是v,之间距离为w),有两种选择,花费w单位油走到v点,或者加一单位油。每次取出最小花费的状态去扩展,直到到达终点t,此时就是到达t的最小花费.#include<cstdio> #include<queue>< 4000 /span> #include<iostream> #include<vector> #include<map> #include<cstring> #include<string> #include<set> #include<stack> #include<algorithm> #define cle(a) memset(a,0,sizeof(a)) #define inf(a) memset(a,0x3f,sizeof(a)) #define ll long long #define Rep(i,a,n) for(int i=a;i<=n;i++) using namespace std; const int INF = ( 2e9 ) + 2; const ll maxn = 1e3+10; int p[maxn],head[maxn],vis[maxn][110],d[maxn][110]; struct edge { int v,w,next; }e[20*maxn]; int tot; struct node { int u,d,r; bool operator < (const node &b)const { return d>b.d; } }; void addedge(int u,int v,int w) { e[tot].v=v; e[tot].w=w; e[tot].next=head[u]; head[u]=tot++; e[tot].v=u; e[tot].w=w; e[tot].next=head[v]; head[v]=tot++; } void BFS(int s,int t,int c,int n) { for(int i=0;i<n;i++) for(int j=0;j<=c;j++) { vis[i][j]=0; d[i][j]=INF; // d[i,j] 代表在第i个城市,油量为j的最小费用 } priority_queue<node> q; d[s][0]=0; q.push(node{s,0,0}); while(!q.empty()) { node a=q.top();q.pop(); int u=a.u,o=a.r; if(vis[u][o])continue; vis[u][o]=1; if(u==t) { printf("%d\n",a.d); return; } if(o+1<=c&&!vis[u][o+1]&&d[u][o]+p[u]<d[u][o+1]) { d[u][o+1]=d[u][o]+p[u]; q.push(node{u,d[u][o+1],o+1}); } for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].v,w=e[i].w; if(o>=w&&!vis[v][o-w]&&d[v][o-w]>a.d) { d[v][o-w]=a.d; q.push(node{v,d[v][o-w],o-w}); } } } printf("impossible\n"); } int main() { int n,m; while(~scanf("%d%d",&n,&m)) { tot=0; memset(head,-1,sizeof(head)); for(int i=0;i<n;i++) scanf("%d",&p[i]); for(int i=0;i<m;i++) { int u,v,w; scanf("%d%d%d",&u,&v,&w); addedge(u,v,w); } int c,s,e,q; scanf("%d",&q); while(q--) { scanf("%d%d%d",&c,&s,&e); BFS(s,e,c,n); } } }
相关文章推荐
- POJ 3635 Full Tank(最短路径变形 + 优先队列)
- POJ-3635-Full Tank?
- POJ 3635 Full Tank? (最短路变形,BFS+优先队列)
- POJ 3635 Full Tank? 最短路变形
- poj 3635 Full Tank? ( bfs+dp思想 )
- poj 3635 Full Tank? ( bfs+dp思想 )
- POJ 3635 Full Tank? (最短路变形,BFS+优先队列)
- POJ 3635 Full Tank?
- POJ-3635 Full Tank? (记忆化广搜)
- [POJ 3635] Full Tank?
- poj3635—Full Tank?(spfa+dp)
- Full Tank? POJ - 3635 二维最短路(dp?)
- poj 3635 Full Tank 动态规划思想在spfa算法中的应用
- POJ 3635 Full Tank?
- POJ 3635 Full Tank? 最短路DP
- POJ 3635 Full Tank?
- POJ-3635 Full Tank? 变形最短路
- POJ 3635 Full Tank?
- poj 3635 Full Tank? (优先队列 + bfs)
- POJ 3635:Full Tank?