poj 3635 带花费的Dij+head优化
练习!!
这里主要需要注意的是进队的条件和dp[][]状态的控制,dp[i][j]表示到第i个城市剩余汽油为j的最小花费。
代码:
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#define inf 100000000
#define MAXN 1200
#define MAXM 1200
using namespace std; int head[MAXN],price[MAXN],dp[MAXN][120],vis[MAXN][120]; int temp,n,m; struct Node{
int v,len,next;
}edge[2*MAXN*10]; struct Node1{
int d;
int u;
int cost;
Node1(int x,int y,int z)
{
u=x;
d=y;
cost=z;
}
friend bool operator < (Node1 a,Node1 b)
{
return a.cost>b.cost;
}
};
void addEdge(int x ,int y ,int c)
{
edge[temp].v=x;
edge[temp].len=c;
edge[temp].next=head[y];
head[y]=temp;
temp++; edge[temp].v=y;
edge[temp].len=c;
edge[temp].next=head[x];
head[x]=temp;
temp++;
} priority_queue<Node1> que; int Dijstra(int s,int t,int c)
{
while(!que.empty())que.pop();
for(int i=0;i<=n;i++)
{
for(int j=0;j<=c;j++)
{
dp[i][j]=inf;
}
}
memset(vis,0,sizeof(vis));
dp[s][0]=0;
que.push(Node1(s,0,0)); while(!que.empty())
{
Node1 b=que.top();
que.pop();
int u=b.u;
int d=b.d;
int cost=b.cost;
vis[u][d]=1;
if(u==t) return cost; if(d+1<=c&&!vis[u][d+1]&&dp[u][d+1]>dp[u][d]+price[u])
{
dp[u][d+1]=dp[u][d]+price[u];
que.push(Node1(u,d+1,dp[u][d+1]));
}
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v;
v=edge[i].v;
int len=edge[i].len;
if(d>=len&&!vis[v][d-len]&&dp[v][d-len]>cost)
{
dp[v][d-len]=cost;
que.push(Node1(v,d-len,cost));
}
}
}
return -1;
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{ for(int i=0;i<n;i++)
{
scanf("%d",&price[i]);
}
memset(head,-1,sizeof(head));
temp=0;
for(int i=1;i<=m;i++)
{
int v,w,c;
scanf("%d%d%d",&v,&w,&c);
addEdge(v,w,c);
} int q;
scanf("%d",&q); while(q--)
{
int q1,q2,q3;
scanf("%d%d%d",&q1,&q2,&q3);
int ans=Dijstra(q2,q3,q1);
if(ans!=-1)
{
printf("%d\n",ans);
}
else
printf("impossible\n");
}
}
return 0;
}
poj 3635 带花费的Dij+head优化的更多相关文章
- POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...
- POJ 3635 Full Tank? 【分层图/最短路dp】
任意门:http://poj.org/problem?id=3635 Full Tank? Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- codeforces 449B Jzzhu and Cities (Dij+堆优化)
输入一个无向图<V,E> V<=1e5, E<=3e5 现在另外给k条边(u=1,v=s[k],w=y[k]) 问在不影响从结点1出发到所有结点的最短路的前提下,最多可以 ...
- poj 3635 Full Tank? ( bfs+dp思想 )
Full Tank? Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5857 Accepted: 1920 Descri ...
- dij+堆优化
写这个dij+堆优化的原因是有些地方卡SPFA,只能搞这个: 香甜的奶油: #include<iostream> #include<cstdio> #include<cs ...
- POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...
- poj 3013 Big Christmas Tree (dij+优先级队列优化 求最短)
模板 意甲冠军:给你一个图,1始终根,每一方都有单价值,每个点都有权重新. 每个边缘的价格值 = sum(后继结点重)*单价方值. 最低价格要求树值,它构成了一棵树n-1条边的最小价值. 算法: 1. ...
- POJ 3171.Cleaning Shifts-区间覆盖最小花费-dp+线段树优化(单点更新、区间查询最值)
Cleaning Shifts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4721 Accepted: 1593 D ...
- poj 1125 Stockbroker Grapevine (dij优化 0ms)
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #defin ...
随机推荐
- ASP.NET Core 源码学习之 Logging[2]:Configure
在上一章中,我们对 ASP.NET Logging 系统做了一个整体的介绍,而在本章中则开始从最基本的配置开始,逐步深入到源码当中去. 默认配置 在 ASP.NET Core 2.0 中,对默认配置做 ...
- Apache崩掉:为进程配置合适的线程数
放假以来,服务器Apache二次崩掉了,不能再拖了,找bug解决: 崩掉的具体状况是,服务器出现弹框显示:Apache停止工作: 顺手关掉这个可恶的小弹框,世界就清静了,服务器正常运行: 具体问题: ...
- 常见C++面试题(三)
strcpy和memcpy有什么区别?strcpy是如何设计的,memcpy呢? strcpy提供了字符串的复制.即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制字符串的结束 ...
- IOS数据持久化之归档NSKeyedArchiver
IOS数据持久化的方式分为三种: 属性列表 (自定义的Property List .NSUserDefaults) 归档 (NSKeyedArchiver) 数据库 (SQLite.Core Data ...
- 百度将与W3C中国召开MIP技术研讨会
百度计划与W3C中国共同组织国内W3C会员,于8月30日召开MIP 技术研讨会,讨论 MIP 等技术相关的应用标准,以期推进 MIP/AMP 在W3C中国的标准化进程. MIP (Mobile Ins ...
- postman也可以使用F12功能
背景: 做过接口测试的话,大多数都知道或使用过postman工具,使用postman的时候,有时候希望也可以像chrome一样使用F12功能,这样方便观察一些数据,尤其是当你使用了postman的变量 ...
- ubuntu常见错误--Could not get lock /var/lib/dpkg/lock解决
通过终端安装程序sudo apt-get install xxx时出错: E: Could not get lock /var/lib/dpkg/lock - open (11: Resource t ...
- spring boot / cloud (六) 开启CORS跨域访问
spring boot / cloud (六) 开启CORS跨域访问 前言 什么是CORS? Cross-origin resource sharing(跨域资源共享),是一个W3C标准,它允许你向一 ...
- 不安装vc2015 Redistributable解决api-ms-win-crt-runtime-l1-1-0.dll丢失错误
一.背景 最近用到python下的cx_Oracle模块,开发环境中测试正常,用cx_freeze打包后,到用户机器上一部署,各种奇奇怪怪的问题频出,运行环境如下: a.开发环境:64位win10操作 ...
- for循环 + setTimeout 结合的烂大街的面试题
零.背景 最近在翻看以前的老书<node.js开发指南>,恰好碰到for循环 + setTimeout的经典例子,于是重新梳理了思路并记录下. 一.写在前面,setTimeout和setI ...