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 ...
随机推荐
- File的getParentFile()和getParent()
代码: File file = new File("D:\\javaemp\\code\\java.txt"); String str1 = file.getP ...
- html5 textarea 文本框根据输入内容自适应高度
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- NYOJ 71 独木舟上的旅行(贪心)
独木舟上的旅行 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别.一条独木舟最多只能乘坐两个人,且乘客 ...
- python细碎语法点
在系统入门python有的是没有遇到,有的是学过了缺乏使用没有记住,就开篇随笔记录这些基础的语法点,随时更新. with...as... 也就是说with是一个控制流语句,跟if/for/while/ ...
- Entity Framework Core 2.0 新特性
本文翻译来自:https://docs.microsoft.com/en-us/ef/core/what-is-new/index 一.模型级查询过滤器(Model-level query filte ...
- append()方法生成的元素绑定的事件失效解决办法
我使用append()方法动态生成的a链接的click事件没有起效果,查找了资料,了解到,我使用的onclick方法绑定的事件对动态生成的元素是无效的,解决办法如下: 使用事件委托,并且要用on来绑定 ...
- Ubuntu安装iNOde
学校使用的是Inode客户端认证上网的.而且还只能使用iNodeSetup3.60-6210版本进行连接,之前使用Ubuntu 32位版本,可以完美地安装并能够连接到网站.由于我的机子是64位的机子, ...
- Wincc flexable的按钮组态
1.题目 2.画面切换按钮组态 1)新建变量 2)组态画面进行命名 3)组态按钮常规 4)组态按钮属性 5)在事件中选择单击,系统函数中选择画面中的ActivateScreen函数切换画面 3.给变量 ...
- IE10 特有 MSGesture() 方法
gesturechange方法 是 ie10以上特有的方法. 初始化:oMsg = new MSGesture(); 主要功能和方法: 用于IE下浏览器的DOM元素触发事件 包括 : MSGestur ...
- 地图性能测试利器PerfQA Analyzer
PerfQA Analyzer作为一个地图性能测试工具,能针对ArcGIS 技术堆栈的系统进行性能问题的排查,用于系统试运行阶段的压力测试以及在线系统运维.大大减轻了GIS系统管理员的性能调优工作压力 ...