HDU 4396
http://acm.hdu.edu.cn/showproblem.php?pid=4396
题意:在至少走k条边的前提下求最短路
思路:在原有最短路模板的基础上多加一维,dis[i][j]表示走到i点经过j条边的最短路,没有别的变化
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std ; const int INF=0xfffffff ;
struct node{
int s,t,v,nxt ;
}e[] ; int n,m,k,cnt,head[],vis[][],dis[][] ; void add(int s,int t,int v)
{
e[cnt].s=s ;
e[cnt].t=t ;
e[cnt].v=v ;
e[cnt].nxt=head[s] ;
head[s]=cnt++ ;
} void spfa(int s)
{
for(int i= ;i<=n ;i++)
for(int j= ;j< ;j++)
dis[i][j]=INF ;
dis[s][]= ;
memset(vis,,sizeof(vis)) ;
vis[s][]= ;
queue <pair<int,int> > q ;
q.push(make_pair(s,)) ;
while(!q.empty())
{
pair<int,int> u=q.front() ;
q.pop() ;
vis[u.first][u.second]= ;
int step=u.second+ ;
if(step>k)step=k ;
for(int i=head[u.first] ;i!=- ;i=e[i].nxt)
{
int tt=e[i].t ;
if(dis[tt][step]>dis[u.first][u.second]+e[i].v)
{
dis[tt][step]=dis[u.first][u.second]+e[i].v ;
if(!vis[tt][step])
{
vis[tt][step]= ;
q.push(make_pair(tt,step)) ;
}
}
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
cnt= ;
memset(head,-,sizeof(head)) ;
while(m--)
{
int a,b,c ;
scanf("%d%d%d",&a,&b,&c) ;
add(a,b,c) ;
add(b,a,c) ;
}
int s,t ;
scanf("%d%d%d",&s,&t,&k) ;
k=k/+(k%!=) ;
spfa(s) ;
if(dis[t][k]==INF)puts("-1") ;
else printf("%d\n",dis[t][k]) ;
}
return ;
}
HDU 4396的更多相关文章
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
- hdu 4329
problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟 a. p(r)= R'/i rel(r)=(1||0) R ...
随机推荐
- Octopus系列之开发中灵光点收集,先放到这里,后面会整理的
项目中引用的组件 1.System.Data.SQLite.dll 自行编译 SQLite-1.0.66.0-source 3.5的框架:F:\Code\开源项目\SQLite\1.0.66.0_x8 ...
- FZU 2032 Log函数问题 模拟小数加法
题目链接:Log函数问题 2 / 49 Problem G FZU 2032 Log函数问题 不知道为什么...比赛时高精度难倒了一票人...成功搞出大新闻... 试了一下直接double相加超时,然 ...
- sharepoint workflow不能正常使用
程序集“Microsoft.SharePoint.WorkflowServices, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce ...
- Visual Studio 2012中的为创建类时的添加注释模板
我们往往需要给类添加注释,我们可以把注释块复制出来,放到文件中,然后在需要的时候,复制.粘贴.这样的重复劳动增加了程序员的体力劳动,而VS中给我们提供了项模版,我们只需要在其中修改一点点模版就能达到这 ...
- 在Android应用中使用OpenGL
Android为OpenGL ES支持提供了GLSurfaceView组件,这个组件用于显示3D图形.GLSurfaceView本身并不提供绘制3D图形的功能,而是由GLSurfaceView.Re ...
- 获取checkbox数组 里面的值
echo '<td class="text-left"><input name="tids[]" type="checkbox&q ...
- Js笔试题之正则表达式
一.复习字符串的传统操作 如何获取一个字符串中的数字字符,并按数组形式输出,如 dgfhfgh254bhku289fgdhdy675gfh 输出[254,289,675] 分析:循环用charAt() ...
- MongoDB常用操作一查询find方法db.collection_name.find()
来:http://blog.csdn.net/wangli61289/article/details/40623097 https://docs.mongodb.org/manual/referenc ...
- (BFS)hdoj1242-Rescue
题目地址 初学BFS,第一次用BFS做题.题目就是一个基本的BFS模型,需要稍加注意的是遇到警卫时间要+1,以及最后比的是最短的时间而不是步数. #include<cstdio> #inc ...
- 《算法竞赛入门经典》5.12TeX括号
/* *在TeX中,左双引号是``,右双引号是''.输入一篇包含双引号的文章,你的任务是把它转换成TeX的格式. *样例输入:"To be or not to be,"quoth ...