More lumber is required
hdu4396:http://acm.hdu.edu.cn/showproblem.php?pid=4396
题意:一个无向带权图,然后给出起点s,终点e,让你求s到e的最短路径,但是这里的路径有要求的。每经过一条边会得到10单位的财富,这条路径必须得到的财富至少k值。
题解:一开始以为是DP,看了别人的代码,才知道了这就是传说中的二维最短路径。然后学习了一下,其实,就是图上的DP。dist[i][j]表示到达i点得到j个财富的最短路径。然后,利用spfa跑最短路,在这个过程中不断更新dist数组,最终的dist[e][k]就是所求的答案。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define inf 100000000
using namespace std;
int n,m;
int s,e,k;
struct Edge{
int v;
int val;
int next;
}edge[];
int dist[][],vis[][];
struct Node{
int pos,num;
};
int head[];
int cnt;
void init(){
cnt=;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,int val){
edge[cnt].v=v;
edge[cnt].val=val;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int SPFA(int s,int e,int k){
for(int i=;i<=n;i++)
for(int j=;j<=k;j++)
dist[i][j]=inf;//初始化
memset(vis,,sizeof(vis));
Node tmp;
tmp.pos=s;
tmp.num=;
dist[s][]=;
vis[s][]=;
queue<Node>Q;
Q.push(tmp);
while(!Q.empty()){
Node t=Q.front();
Q.pop();
vis[t.pos][t.num]=;//可能会多次加到队列
for(int i=head[t.pos];i!=-;i=edge[i].next){
Node tp;
tp.pos=edge[i].v;
tp.num=t.num+;
if(tp.num>k)
tp.num=k;//题目是至少得到k,所以在以相同路径长度当到达e时候能得到更多的财富是符合要求的
if(dist[tp.pos][tp.num]>dist[t.pos][t.num]+edge[i].val){//更新dist数组
dist[tp.pos][tp.num]=dist[t.pos][t.num]+edge[i].val;
if(vis[tp.pos][tp.num]==){//如果不在队列,则把这个加入队列
vis[tp.pos][tp.num]=;
Q.push(tp);
}
}
}
}
if(dist[e][k]==inf)return -;
else
return dist[e][k];
}
int main(){
int a,b,c;
while(~scanf("%d%d",&n,&m)){
init();
for(int i=;i<=m;i++){//建图
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
addedge(b,a,c);
}
scanf("%d%d%d",&s,&e,&k);
int ans=SPFA(s,e,k);
printf("%d\n",ans);
} }
More lumber is required的更多相关文章
- HDU 4396More lumber is required 过至少K条边的最短路
/* ** 题目要求过最少k条边的最短路 */ #include <iostream> #include <cstdio> #include <cstring> # ...
- 2012 Multi-University #10
容斥原理 A Number Sequence 题意:给出n个数,b1,b2,b3……bn,构造n个数,a1,a2,……an(ai>1),使得a1*a2*a3……an=b1*b2……bn 分析:容 ...
- 【转】最短路&差分约束题集
转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...
- 转载 - 最短路&差分约束题集
出处:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★ ...
- 记一个mvn奇怪错误: Archive for required library: 'D:/mvn/repos/junit/junit/3.8.1/junit-3.8.1.jar' in project 'xxx' cannot be read or is not a valid ZIP file
我的maven 项目有一个红色感叹号, 而且Problems 存在 errors : Description Resource Path Location Type Archive for requi ...
- required
required,这是HTML5中的一个新属性:这是HTML5中input元素中的一个属性. required译为必须的,在input元素中应用这一属性,就表示这一input元素节点是必填的或者必选的 ...
- The type javax.ws.rs.core.MediaType cannot be resolved. It is indirectly referenced from required .class files
看到了http://stackoverflow.com/questions/5547162/eclipse-error-indirectly-referenced-from-required-clas ...
- Hibernate整合Spring异常'sessionFactory' or 'hibernateTemplate' is required
今日在写GenericDao时,发现了一个异常,内容如下: org.springframework.beans.factory.BeanCreationException: Error creatin ...
- 关于Access restriction: The type 'Application' is not API (restriction on required library)
原文链接:http://rxxluowei.iteye.com/blog/671893 今天写第一次写JavaFX的入门程序就GG 遇到了导入API的问题,无奈疯狂地通过网络找解决方案.. 我的问题是 ...
随机推荐
- xargs 参数
hadoop fs -ls /source/recommend/at_access | awk -F "/" '{print $NF}' | grep -v $(date +%Y% ...
- curl 模拟ajax 请求
主要是在header请求里加一个 X-Requested-With: XMLHttpRequest curl -v -H "X-Requested-With: XMLHttpRequest ...
- Git 中README.md中MarkDown语法示例
转 http://blog.csdn.net/brokge/article/details/38388757 简介 Markdown的语法简洁明了.学习容易,而且功能比纯文本更强,因此有很多人用它写博 ...
- dubbo简述
转http://blog.csdn.net/hzzhoushaoyu/article/details/4327309 一.前言 部门去年年中开始各种改造,第一步是模块服务化,这边初选dubbo试用在一 ...
- Qt应用中检测内存泄露——VLD
本文简要描述一下在Qt应用中使用VLD来检测内存泄露.本次测试环境:QtCreator2.3 + Qt4.7.4-vs2008 + VS2008 Express. 1.下载并安装:VLD-2.2: h ...
- Mysql update error: Error Code: 1175. You are using safe update mode and you tried to update a table
Mysql update error: Error Code: 1175. You are using safe update mode and you tried to update a table ...
- 模板-->Guass消元法(求解多元一次方程组)
如果有相应的OJ题目,欢迎同学们提供相应的链接 相关链接 所有模板的快速链接 简单的测试 None 代码模板 /* * TIME COMPLEXITY:O(n^3) * PARAMS: * a The ...
- 【小TIP】记录各种错误【更新中】
最好程序一遍通过,为了提高代码能力,这里将用TIP的形式记录来犯过的错误.不断更新中. *已经转移到闪存.. [150214]WA:检查是否数组开小了. [150212]WA:如果程序中有乘号,需要留 ...
- 各种开发语言示例调用WebService接口
ASP示例: <% uid="账号"pwd="密码"tos="13900041123"msg="你们好"url = ...
- windows安装服务
我安装了windows服务的时候监控其他机子的时候,我在调试的时候用的是Account是其他用户是可以正常的,但是安装成服务的时候,老是被拒绝. 后来我将Account改成NetworkService ...