您的位置:首页 > 其它

poj 3635(full tank?)

2011-10-06 17:36 417 查看
我感觉是dp类型的吧。。。。

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: