题目链接【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 5532 Almost Sorted Array (最长非递减子序列)

    题目链接 Problem Description We are all familiar with sorting algorithms: quick sort, merge sort, heap s ...

  2. Python Dict用法

    Operation Result len(a) the number of items in a 得到字典中元素的个数 a[k] the item of a with key k 取得键K所对应的值 ...

  3. translate 与 相对、绝对定位

    垂直水平居中是日常前端开发当中一个常见的需求,在支持 CSS3 属性的现代浏览器当中,有一个利用 CSS3 属性的垂直水平居中方法: position absolute; :; :; :transla ...

  4. PHP 生成、识别二维码及安装相关扩展/工具

    2018-02-20 00:30:26  更新:推荐新扩展(极力推荐) 这篇文章里用的两个二维码扩展都有些问题和麻烦:phpqrcode(生成二维码)的源码有点小 bug: 而 php-zbarcod ...

  5. netstat-ll-grep-nohup-df-supervisord

    ============http://man.linuxde.net/=========== 0 vi / n是查找下一个,alt+n是上一个  u撤销上一步,回到上一步 1. 根据进程号(4974) ...

  6. 使用mongoose操作mongodb数据库

    1.如何启动mongodb数据库 参考地址:http://www.runoob.com/mongodb/mongodb-window-install.html 在数据库安装的地方,bin文件夹,输入 ...

  7. json的用法

    json格式 JSON格式:http://www.json.org/ python和JSON的关系请参考:http://docs.python.org/library/json.html JSON建构 ...

  8. github后端开发面试题大集合(一)

    作者:小海胆链接:https://www.nowcoder.com/discuss/3614?type=0&order=0&pos=5&page=0?from=wb来源:牛客网 ...

  9. OutLook中添加Exchange失败问题

    问题: 在邮件中添加账户后,打开outlook时报出错误:无法启动 Microsoft Outlook. 无法打开 Outlook 窗口. 无法打开此文件夹集合. 必须先使用当前的配置文件连接到 Mi ...

  10. 使用文本用户界面(NMTUI)进行网络配置

    NetworkManager 文本用户界面(TUI)工具 nmtui 可提供一个文本界面配置由 NetworkManager 控制的网络.该工具包含在 NetworkManager-tui 子软件包中 ...