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 ...
随机推荐
- Struts、JSTL标签库的基本使用方法
一 使用Struts标签之前需要经过下面3个步骤的配置. 1.导入TLD文件. 2.在web.xml中注册标签库. 3.在页面中引入标签库. 下面详细介绍以上步骤. 1 导入TLD文件. TLD文件是 ...
- uva 1631
1631 Locker A password locker with N digits, each digit can be rotated to 0-9 circularly. You can ro ...
- EF 7 Code First
加载方式三种 1. Eager Loading 2. Lazy Loading 3.Explicit Loading 使用EF在与关系型数据库的交互中不可避免地需要加载数据,如何加载数据变得至关重要. ...
- linux下格式化硬盘与挂载硬盘
格式化: mkfs -t ext4 /dev/sdb 自动挂载: 编辑/etc/fstab文件 sudo nano /etc/fstab,如下图将设备/dev/sdb硬盘挂载到/home/solr/s ...
- NIO基础
通道和缓冲区 概述 通道 和 缓冲区 是 NIO 中的核心对象,几乎在每一个 I/O 操作中都要使用它们. 通道是对原 I/O 包中的流的模拟.到任何目的地(或来自任何地方)的所有数据都必须通过一个 ...
- 如何获取google可以访问的IP地址
由于某些原因,google的部分网站无法打开,导致我们的好些资源都无法找到,今天在网上看到一篇文件,教大家如何能找到可以访问的google. 假如我们需要访问的是:https://code.googl ...
- MATLAB图像处理函数汇总(二)
60.imnoise 功能:增加图像的渲染效果. 语法: J = imnoise(I,type) J = imnoise(I,type,parameters) 举例 I = imread('eight ...
- iframe中的jquery ui modal dialog 覆盖父窗口
在iframe中 使用jquery ui dialog,弹出后可以覆盖父窗体 ///iframe中的jquery ui modal dialog 覆盖父窗口 function openDialog() ...
- K-Anonymous Sequence(poj 3709)
http://poj.org/problem?id=3709 给定一个长度为n的非严格单调递增数列a1,...,an.每一次操作可以使数列中的任何一项的值减小1.现在要使数列中的每一项都满足其他项中至 ...
- 《day12---异常》
//91-面向对象-异常-异常的发生和简单应用. /* 异常: java运行时期发生的问题就是异常. Java中运行时的除了异常Exception含有错误Error. 异常:通常发生后可以有针对性的处 ...