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. java二维数组简单初步理解

    二维数组 二维数组本质上是以数组作为数组元素的数组,即“数组的数组”. int[][] arr = {{1, 2, 3}, {4, 5, 6}}; System.out.println(arr[0][ ...

  2. 实验一 Java开发环境的熟悉境的熟悉

    - 运行结果: (一)命令行下Java程序开发 (二)Eclipse下Java程序开发.调试 设置断点和单步运行 单步运行:Step Into(快捷捷F5)和Step Over(快捷捷F5) 使程序直 ...

  3. js 面试题

    1.用原生js,创建一个无序列表添加到body中,ul下包含5个li,每个li包含一个text类型元素,text元素内容可自定义: <script type="text/javascr ...

  4. 230. Kth Smallest Element in a BST ——迭代本质:a=xx1 while some_condition: a=xx2

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  5. JDE函数--GetUDC(B函数)

    GetUDC使用方式:

  6. 学习记录014-ssh批量分发

    一.ssh服务介绍 1.ssh安全的加密协议用于远程连接服务器 2.默认端口是22,安全协议版本ssh2,它能同时支持RSA和DSA秘钥,SSH1只支持RSA 3.服务端主要包含两个服务功能ssh远程 ...

  7. table表格

    表格是一种组织整理的数据的手段,在div布局还未流行是,也用来布局,一个表格包含了表格整体.表格头部.每个表格均有若干行,每行被分为若干单元格. 在HTML中表格使用table标签来定义,行由< ...

  8. HBase HMaster Architecture - HBase Master架构

    HBase architecture follows the traditional master slave model where you have a master which takes de ...

  9. js控制html文字提示语的出现和隐藏

    有时我们需要在点击html输入框的时候,旁边会出现提示语.在输入字符的时候,输入框下边会出现输入了多少字符的提示. 请看下面实例. <!DOCTYPE html> <html> ...

  10. sql插入多条数据的sql语句

    sql插入多条数据的sql语句 有三种方法:1.InSert Into <表名>(列名)Select <列名>From <源表名>如:INSERT INTO Ton ...