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的更多相关文章

  1. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  2. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  3. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  4. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  5. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  6. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  7. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

  8. HDU 3791二叉搜索树解题(解题报告)

    1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...

  9. hdu 4329

    problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟  a.     p(r)=   R'/i   rel(r)=(1||0)  R ...

随机推荐

  1. github注册使用以及体会

    一.个人介绍 我叫冯越,学号是1413042065,班级是网络工程143,兴趣爱好有打羽毛球,素描,读书,个人编程能力一般,行数没有计算过,大一学习C++时,实验题目一些书后的题目,大二学习数据结构时 ...

  2. jquery用Ajax中的回调函数时注意事项

    前端代码 <script language="javascript" type="text/javascript" src="<?php ...

  3. ecshop数据库取数据

    取出所有数据: test_getAll(); function test_getAll() { global $db; $sql = "SELECT user_id, user_name, ...

  4. [整理]Linux压缩与解压缩命令整理。

    一.压缩文件命令 1.*.Z compress 程序压缩的档案:2.*.bz2 bzip2 程序压缩的档案:3.*.gz gzip 程序压缩的档案:4.*.tar tar 程序打包的数据,并没有压缩过 ...

  5. SQL逻辑读变成零

    使用缓存HINT 让逻辑读变成0. create table t as select * from dba_objects; insert into t select * from t; commit ...

  6. mysql 在insert 时防止出现主键冲突错误的方法

    在mysql中插入数据的时候常常因为主键存在而冲突报错,下面有两个解决方法: 1.在insert 语句中添加ignore 关键字,如:insert ignore into table (id,name ...

  7. 搜索功能demo

    代码如下: <html> <head> <meta http-equiv="X-UA-Compatible" content="IE=edg ...

  8. FF与IE对JavaScript和CSS的区别

    一.FF与IE对JavaScript的区别 1. document.formName.item("itemName") 问题 说明:IE下,可以使用document.formNam ...

  9. IE9的css hack

    以前写过<IE8的css hack>,ie9一出css hack也该更新,以前一直没关注,今天在内部参考群mxclion分享了IE9的css hack,拿出来也分享一下: select { ...

  10. lucas 定理学习

    大致意思就是求组合数C(n , m) % p的值, p为一个偶数 可以将组合数的n 和 m都理解为 p 进制的表示 n  = ak*p^k + a(k-1)*p^(k-1) + ... + a1*p ...