题意:

      给你一个图,让你送起点走到终点,至少经过k条边,问你最短路径是多少....


思路:

      把每个点拆成50点,记为dis[i][j] (i 1---50 ,j 1---n);代表走到第j个点做过i条边时的最短距离,因为做多五十条边,如果走的过程中,边数大于50直接等于50,因为大于50的时候就没有必要走"回头路"了...然后跑完spfa后在dis[i][t](i =  k---50)中取一个最小的输出来,就行了...


#include<stdio.h>
#include<string.h>
#include<queue> #define N_node 5000 + 100
#define N_edge 200000 + 1000
#define inf 100000000

using namespace
std; typedef struct
{
int
to ,next ,cost;
}
STAR; typedef struct
{
int
x ,t;
}
NODE; int s_x[55][N_node] ,n ,m ,s ,t;
int
mark[55][N_node];
int
list[N_node] ,tot;
NODE xin ,tou;
STAR E[N_edge]; void add(int a ,int b ,int c)
{

E[++tot].to = b;
E[tot].cost = c;
E[tot].next = list[a];
list[a] = tot;
} void
SPFA()
{
for(int
i = 0 ;i <= 52 ;i ++)
for(int
j = 1 ;j <= n ;j ++)
s_x[i][j] = inf;
// printf("%d %d\n" ,s_x[1][3] ,s_x[1][2]);
s_x[0][s] = 0;
xin.x = s;
xin.t = 0;
queue<NODE>q;
q.push(xin);
memset(mark ,0 ,sizeof(mark));
mark[0][s] = 1;
while(!
q.empty())
{

tou = q.front();
q.pop();
      mark[tou.t][tou.x] = 0;
for(int
k = list[tou.x] ;k ;k = E[k].next)
{

xin.x = E[k].to;
xin.t = tou.t + 1;
if(
xin.t > 50) xin.t = 50;
//printf("%d %d %d %d\n" ,s_x[xin.t][xin.x] ,s_x[tou.t][tou.x] + E[k].cost ,xin.t ,xin.x);
if(s_x[xin.t][xin.x] > s_x[tou.t][tou.x] + E[k].cost)
{

s_x[xin.t][xin.x] = s_x[tou.t][tou.x] + E[k].cost; if(!mark[xin.t][xin.x])
{

mark[xin.t][xin.x] = 1;
q.push(xin);
}
}
}
}
} int main ()
{
int
m ,a ,b ,c ,k ,i;
while(~
scanf("%d %d" ,&n ,&m))
{

memset(list ,0 ,sizeof(list));
tot = 1;
for(
i = 1 ;i <= m ;i ++)
{

scanf("%d %d %d" ,&a ,&b ,&c);
add(a ,b ,c);
add(b ,a ,c);
}
scanf("%d %d %d" ,&s ,&t ,&k);
SPFA();
int
ans = inf;
k = (k + 9)/10;
for(
i = k ;i <= 50 ;i ++)
if(
ans > s_x[i][t])
ans = s_x[i][t];
if(
ans == inf) ans = -1;
printf("%d\n" ,ans);
}
return
0;
}

hdu4396 多状态spfa的更多相关文章

  1. Travelling(spfa+状态压缩dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 Travelling Time Limit: 6000/3000 MS (Java/Others ...

  2. Victor and World(spfa+状态压缩dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5418 Victor and World Time Limit: 4000/2000 MS (Java/ ...

  3. HDU 4085 Peach Blossom Spring 斯坦纳树 状态压缩DP+SPFA

    状态压缩dp+spfa解斯坦纳树 枚举子树的形态 dp[i][j] = min(dp[i][j], dp[i][k]+dp[i][l]) 当中k和l是对j的一个划分 依照边进行松弛 dp[i][j]  ...

  4. [luogu P3786]萃香抱西瓜 [spfa][状态压缩]

    题目背景 伊吹萃香(Ibuki Suika)正在魔法之森漫步,突然,许多西瓜(Suika)从四周飞来,划出了绚丽的轨迹.虽然阵势有点恐怖,但她还是决定抱走一些西瓜. 题目描述 萃香所处的环境被简化为一 ...

  5. BZOJ2763 [JLOI2011]飞行路线(SPFA + DP)

    题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=2763 Description Alice和Bob现在要乘飞机旅行,他们选择了一家 ...

  6. bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)

    数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...

  7. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  8. 【BZOJ-1097】旅游景点atr SPFA + 状压DP

    1097: [POI2007]旅游景点atr Time Limit: 30 Sec  Memory Limit: 357 MBSubmit: 1531  Solved: 352[Submit][Sta ...

  9. ACM/ICPC 之 最短路-Floyd+SPFA(BFS)+DP(ZOJ1232)

    这是一道非常好的题目,融合了很多知识点. ZOJ1232-Adventrue of Super Mario 这一题折磨我挺长时间的,不过最后做出来非常开心啊,哇咔咔咔 题意就不累述了,注释有写,难点在 ...

随机推荐

  1. CVE-2017-12615 -Tomcat-任意写入文件

    漏洞分析参考 https://www.freebuf.com/vuls/148283.html 漏洞描述: 当 Tomcat运行在Windows操作系统时,且启用了HTTP PUT请求方法(例如,将 ...

  2. 爬虫必知必会(5)_scrapy框架_基础

    一.移动端数据的爬取 基于某一款抓包工具,fiddler,青花瓷,miteproxy fillder进行一个基本的配置:tools->options->connection->all ...

  3. webpack4.x 从零开始配置vue 项目(一)基础搭建项目

    序 现在依旧记得第一次看到webpack3.x 版本配置时候的状态  刚开始看到这些真的是一脸懵.希望这篇文章能帮到刚开始入门的同学. webpack 是什么? webpack是一个模块化打包工具,w ...

  4. vim下在插件emmet

    试了很多种方法,结果都没有用,最后找到了完美的解决方法! 1.方式1 1.1下载emmet并解压 1.2 cd /home/debian8/Downloads/emmet-vim-master/plu ...

  5. MySQL语法基础

    一.通用语法 1.MySQL数据库的SQL语句不区分大小写 2.可以用/**/完成注释 3.常用数据类型 类型 描述 int 整型 double 浮点型 varchar 字符串型 date 日期类型, ...

  6. Spring源码之ApplicationContext

    ​ 本文是针对Srping的ClassPathXMLApplicationContext来进行源码解析,在本篇博客中将不会讲述spring Xml解析注册代码,因为ApplicationContext ...

  7. ethtool - 命令

    ethtool 导览:     1. 如何查看 Linux 中可用的网卡接口     2. 如何查看 Linux 中网卡信息     3. 如何查看网卡驱动版本以及硬件版本     4. 如何查看网络 ...

  8. PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) 凌宸1642

    PAT (Advanced Level) Practice 1006 Sign In and Sign Out (25 分) 凌宸1642 题目描述: At the beginning of ever ...

  9. Kubernetes,kubectl常用命令详解

    kubectl概述 祭出一张图,转载至 kubernetes-handbook/kubectl命令概述 ,可以对命令族有个整体的概念. 环境准备 允许master节点部署pod,使用命令如下: kub ...

  10. 【oracle学习笔记02】Oracle Architecture —— Process Structure

    Oracle中有三类进程: 1 User Process 2 Server Process Server Process is a program that directly interacts wi ...