题目链接【https://www.oj.swust.edu.cn/problem/show/2605】

题意:给出包含N(N <= 5000)个点M条边的有向图,然后求1 - N在满足距离小于T的情况下,最多走多少个点。

题解:dp[i][j]表示邹大鹏i点,经过了j个点的最短路。用pre维护一下路径即可。

#include<bits/stdc++.h>
using namespace std;
typedef pair<int, int> Pair;
const int INF = 1e9 + 15;
const int maxn = 5050;
int N, M, T;
struct Edge
{
int to, next, len;
Edge() {}
Edge(int to, int next, int len): to(to), next(next), len(len) {}
} E[maxn*maxn];
int head[maxn], tot;
void initEdge()
{
for(int i = 0; i <= N; i++) head[i] = -1;
tot = 0;
}
void addEdge(int u, int v, int len)
{
E[tot] = Edge(v, head[u], len);
head[u] = tot++;
}
int dp[maxn][maxn], pre[maxn][maxn], in[maxn][maxn];
void Spfa()
{
queue<Pair>que;
dp[1][1] = 0;
que.push(make_pair(1, 1));
in[1][1] = 1;
while(!que.empty())
{
int u = que.front().first;
int num = que.front().second;
que.pop();
in[u][num] = 0;
for(int k = head[u]; ~k; k = E[k].next)
{
int v = E[k].to;
if(dp[v][num + 1] > dp[u][num] + E[k].len)
{
dp[v][num + 1] = dp[u][num] + E[k].len;
pre[v][num + 1] = u;
if(!in[v][num + 1])
{
que.push(make_pair(v, num + 1));
in[v][num + 1] = 1;
}
}
}
}
}
int main ()
{
while(~scanf("%d %d %d", &N, &M, &T))
{
initEdge();
for(int i = 1; i <= M; i++)
{
int u, v, len;
scanf("%d %d %d", &u, &v, &len);
addEdge(u, v, len);
}
for(int i = 1; i <= N; i++)
for(int j = 1; j <= N; j++)
dp[i][j] = INF, pre[i][j] = in[i][j] = 0;
Spfa();
int ans = N;
for(; ans >= 1; ans--)
if(dp[N][ans] <= T) break;
vector<int>vt;
int u = N, num = ans;
while(pre[u][num])
{
int v = pre[u][num];
vt.push_back(v);
u = v;
num--;
}
printf("%d\n%d\n", dp[N][ans], ans);
int sz = vt.size() - 1;
for(int i = sz; i >= 0; i--)
printf("%d ", vt[i]);
printf("%d\n", N);
}
return 0;
}

  

Power OJ 2605 SPFA+dp思想的更多相关文章

  1. HDU-3499:Flight(SPFA+dp)

    Recently, Shua Shua had a big quarrel with his GF. He is so upset that he decides to take a trip to ...

  2. 【BZOJ1003】1003: [ZJOI2006]物流运输trans SPFA+DP

    Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...

  3. HDU 3499 Flight spfa+dp

    Flight Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 65535/65535K (Java/Other) Total Subm ...

  4. BZOJ 1003 [ZJOI2006]物流运输trans SPFA+DP

    题意:链接 方法:SPFA+DP 解析:挺好的题目.因为数据范围较小所以用这样的方式能够搞,只是也是挺不好想的. 我们定义cost(i,j)表示从第i天走到第j天运用同一种方式的最小花费,然后因为数据 ...

  5. 到底什么是dp思想(内含大量经典例题,附带详细解析)

    期末了,通过写博客的方式复习一下dp,把自己理解的dp思想通过样例全部说出来 说说我所理解的dp思想 dp一般用于解决多阶段决策问题,即每个阶段都要做一个决策,全部的决策是一个决策序列,要你求一个 最 ...

  6. hdu 3030 Increasing Speed Limits (离散化+树状数组+DP思想)

    Increasing Speed Limits Time Limit: 2000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  7. DP思想在斐波那契数列递归求解中的应用

    斐波那契数列:1, 1, 2, 3, 5, 8, 13,...,即 f(n) = f(n-1) + f(n-2). 求第n个数的值. 方法一:迭代 public static int iterativ ...

  8. power oj 2480 放积木[二进制状压DP]

    题目链接[https://www.oj.swust.edu.cn/problem/show/2480] 题意:中文题目. 题解:二进制状态转移+坏点判断. #include<cstdio> ...

  9. power oj 1557种树[二进制状压DP]

    题目链接[https://www.oj.swust.edu.cn/problem/show/1557] 题意:中文题目. 题解:用0,1表示某个位置是否种了树,先算出同一行的有效状态的总数,即开两个1 ...

随机推荐

  1. HDU 2087 剪花布条 (KMP 不允许重叠的匹配)

    题目链接 Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Inp ...

  2. 2016.5.15——leetcode:Number of 1 Bits ,

    leetcode:Number of 1 Bits 代码均测试通过! 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n ...

  3. JS获取元素内容属性以及修改

    1.通过document对象

  4. Ubuntu使用apt-get upgrade升级时出错

    今天在按照常规的sudo apt-get update更新软件列表后,再使用sudo apt-get upgrade升级软件时,出现了以下的错误: 正在设置 linux-image-extra-4.4 ...

  5. c++ 类的构造顺序

    在单继承的情况下,父类构造先于子类,子类析构先于父类,例: class A { public: A() { cout << "A" << endl; } ~ ...

  6. Python_oldboy_自动化运维之路_全栈考试(五)

    1.执行 Python 脚本的两种方式 [root@localhost tmp]# cat a.py #!/usr/bin/python # -*- coding: UTF-8 -*- print & ...

  7. 使用html+css+js实现3D相册

    使用html+css+js实现3D相册,快来上传的照片吧 效果图: 代码如下,复制即可用: <!DOCTYPE html> <html lang="en"> ...

  8. 如何阻止点击scrollviewer里面的单位内容时,自动滚动

    <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="FocusVisualStyle ...

  9. 生成Insert语句的存储过程

    ) drop procedure [dbo].[spGenInsertSQL] GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO )) as b ...

  10. IdentityServer4揭秘---登录

    IdentityServer4默认提供了的登录地址是Account/Index 同意页面是Consent/Index 这里我们可以通过IdentittyServer4的用户交互自定义配置设置 在Con ...