POJ-3635 Full Tank? 变形最短路
2013-01-31 00:03
351 查看
题目链接:http://poj.org/problem?id=3635
容易想到用二维数组记录状态求最短路,然后用优先队列优化,类似于Dijkstra和BFS。我开始设计的过程是直接直接从当前点向相邻点转移并找出所有可能状态,结果TLE。貌似是无关状态太多了,没想到卡得这么紧。别人的做法是只考虑两个状态:1、加一单位油 2、向相邻点移动;这样就砍掉了一些对最优解不必要的状态。
容易想到用二维数组记录状态求最短路,然后用优先队列优化,类似于Dijkstra和BFS。我开始设计的过程是直接直接从当前点向相邻点转移并找出所有可能状态,结果TLE。貌似是无关状态太多了,没想到卡得这么紧。别人的做法是只考虑两个状态:1、加一单位油 2、向相邻点移动;这样就砍掉了一些对最优解不必要的状态。
//STATUS:C++_AC_297MS_2404KB #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<iostream> #include<string> #include<algorithm> #include<vector> #include<queue> #include<stack> using namespace std; #define LL __int64 #define pdi pair<double,int> #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) #define mem(a,b) memset(a,b,sizeof(a)) #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 const int N=1010,M=1000000,INF=0x3f3f3f3f,MOD=1999997; const LL LLNF=0x3f3f3f3f3f3f3f3fLL; const double DNF=100000000; struct Node{ int d,u,c; bool operator < (const Node &ths) const { return d>ths.d; } }; struct Edge{ int u,v,w; }e[N*20]; int vis [110],d [110],w ,first ,next[N*20]; int n,m,t,mt,cup; void adde(int a,int b,int c) { e[mt].u=a,e[mt].v=b,e[mt].w=c; next[mt]=first[a],first[a]=mt++; e[mt].u=b,e[mt].v=a,e[mt].w=c; next[mt]=first[b],first[b]=mt++; } int Dijkstra(int s,int end) { int i,j,k,dis,u,v,c; Node t; priority_queue<Node> q; mem(vis,0); mem(d,INF); d[s][0]=0; q.push(Node{0,s,0}); while(!q.empty()){ t=q.top();q.pop(); u=t.u;c=t.c; if(u==end)return t.d; if(vis[u][c])continue; vis[u][c]=1; if(c<cup && t.d+w[u]<d[u][c+1]){ d[u][c+1]=t.d+w[u]; q.push(Node{d[u][c+1],u,c+1}); } for(i=first[u];i!=-1;i=next[i]){ v=e[i].v; if(c>=e[i].w && d[u][c]<d[v][c-e[i].w]){ d[v][c-e[i].w]=d[u][c]; q.push(Node{d[u][c],v,c-e[i].w}); } } } return -1; } int main() { // freopen("in.txt","r",stdin); int i,j,a,b,c,ans; while(~scanf("%d%d",&n,&m)) { mt=0; mem(first,-1); for(i=0;i<n;i++) scanf("%d",&w[i]); for(i=0;i<m;i++){ scanf("%d%d%d",&a,&b,&c); adde(a,b,c); } scanf("%d",&t); while(t--){ scanf("%d%d%d",&cup,&a,&b); ans=Dijkstra(a,b); if(ans!=-1)printf("%d\n",ans); else printf("impossible\n"); } } return 0; }
相关文章推荐
- POJ 3635 Full Tank? (最短路变形,BFS+优先队列)
- POJ 3635 Full Tank? (最短路变形,BFS+优先队列)
- POJ 3635 Full Tank? 最短路变形
- poj 3635/hdu 1676 Full Tank? 车辆加油+最短路
- Full Tank? POJ - 3635 二维最短路(dp?)
- poj 3635/hdu 1676 Full Tank? 车辆加油+最短路
- POJ 3635 Full Tank(最短路径变形 + 优先队列)
- POJ 3635 Full Tank? 最短路DP
- poj 3635 Full Tank 最短路
- poj 3635 Full Tank? (优先队列 + bfs)
- poj3635—Full Tank?(spfa+dp)
- POJ 3635 Full Tank? 拆点建图+dijkstra
- poj 3635 Full Tank? ( bfs+dp思想 )
- POJ 3635 Full Tank?
- POJ 3635 Full Tank?
- poj 3635 Full Tank? ( 图上dp )
- [POJ 3635] Full Tank?
- POJ 3635 Full Tank?
- poj 3635(full tank?)
- POJ3635 周游诸城:变形的SPFA/Dijkstra最短路+动规思想+Heap