题意:给定一个有向图,你从1出发到n,走尽可能多的点,并且使总权值不大于t。

析:在比赛时,竟然看成有向图了,就想了好久,感觉dp,但是不会啊。。。如果是有向图就好做多了,枚举边,然后打印就好,dp[i][j] 表示,

经过 i 个结点,并且在 j的最小时间。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
//#include <tr1/unordered_map>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5000 + 5;
const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int dp[maxn][maxn], p[maxn][maxn];
int G[maxn][3];
stack<int> stacks; int main(){
int t;
while(scanf("%d %d %d", &n, &m, &t) == 3){
int x;
for(int i = 0; i < m*3; ++i){
scanf("%d", &x);
G[i/3][i%3] = x;
} memset(dp, INF, sizeof dp);
dp[1][1] = 0;
memset(p, 0, sizeof p);
int ans = 0;
for(int i = 2; i <= n; ++i)
for(int j = 0; j < m; ++j){
int pre = G[j][0], last = G[j][1], w = G[j][2];
if(dp[i-1][pre] + w <= t && dp[i][last] > dp[i-1][pre] + w){
dp[i][last] = dp[i-1][pre] + w;
ans = last == n ? Max(ans, i) : ans;
p[i][last] = pre;
}
} printf("%d\n", ans);
for(int i = n; ans; i = p[ans--][i]) stacks.push(i);
printf("%d", stacks.top()); stacks.pop();
while(!stacks.empty()) printf(" %d", stacks.top()), stacks.pop();
printf("\n");
}
return 0;
}

CodeForces 721B Journey (DP)的更多相关文章

  1. CodeForces 839C - Journey | Codeforces Round #428 (Div. 2)

    起初误以为到每个叶子的概率一样于是.... /* CodeForces 839C - Journey [ DFS,期望 ] | Codeforces Round #428 (Div. 2) */ #i ...

  2. Codeforces Round #374 (Div. 2) C. Journey DP

    C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...

  3. Codeforces Round #374 (Div. 2) C. Journey —— DP

    题目链接:http://codeforces.com/contest/721/problem/C C. Journey time limit per test 3 seconds memory lim ...

  4. codeforces 721C C. Journey(dp)

    题目链接: C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...

  5. CodeForces 721C Journey(拓扑排序+DP)

    <题目链接> 题目大意:一个DAG图有n个点,m条边,走过每条边都会花费一定的时间,问你在不超过T时间的条件下,从1到n点最多能够经过几个节点. 解题分析:对这个有向图,我们进行拓扑排序, ...

  6. CF721C. Journey[DP DAG]

    C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard input ou ...

  7. CF #374 (Div. 2) C. Journey dp

    1.CF #374 (Div. 2)    C.  Journey 2.总结:好题,这一道题,WA,MLE,TLE,RE,各种姿势都来了一遍.. 3.题意:有向无环图,找出第1个点到第n个点的一条路径 ...

  8. codeforces 682D(DP)

    题目链接:http://codeforces.com/contest/682/problem/D 思路:dp[i][j][l][0]表示a串前i和b串前j利用a[i] == b[j]所得到的最长子序列 ...

  9. codeforces 666A (DP)

    题目链接:http://codeforces.com/problemset/problem/666/A 思路:dp[i][0]表示第a[i-1]~a[i]组成的字符串是否可行,dp[i][1]表示第a ...

随机推荐

  1. vi/vim复制粘贴命令

    1. 选定文本块.使用v进入可视模式,移动光标键选定内容. 2.复制的命令是y,即yank(提起) ,常用的命令如下:     y      在使用v模式选定了某一块的时候,复制选定块到缓冲区用:   ...

  2. Android基本动画

    Android基本动画 Android Animations动画使用详解 一.动画类型 Android的animation由四种类型组成:alpha.scale.translate.rotate XM ...

  3. MySQL基础架构

    前段时间订阅了<Mysql实战45讲>(从原理到实战),新的一年为自己充充电.对于这部分内容,我所知道的只来源于我大学里学习的课程<数据库原理>,在大学里学习的只是简单的查询, ...

  4. CD-----UVa624(01背包+输出路径)

      CD  You have a long drive by car ahead. You have a tape recorder, but unfortunately your best musi ...

  5. The Bottom of a Graph

                                    poj——The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K ...

  6. Spring Cloud(8):Sleuth和Zipkin的使用

    场景: 某大型电商网站基于微服务架构,服务模块有几十个. 某天,测试人员报告该网站响应速度过慢.排除了网络问题之后,发现很难进一步去排除故障. 那么:如何对微服务的链路进行监控呢? Sleuth: 一 ...

  7. Java 函数式接口 lambda

    import java.io.Serializable; import java.util.ArrayList; import java.util.List; public class Demo1 { ...

  8. how to read openstack code: action extension

    之前我们看过了core plugin, service plugin 还有resource extension. resource extension的作用是定义新的资源.而我们说过还有两种exten ...

  9. system表空间用满解决

      分类: Oracle 早上看到alert日志报说system表空间快满了(oracle版本是11gR2):   如果system表空间不是自动扩展,空间用满甚至会出现数据库无法登陆.使用任何用户登 ...

  10. JAVA学习(一):Java介绍及其平台、开发环境的配置与搭建

    Java介绍及其平台.开发环境的配置与搭建 1.Java的介绍 Java是一种面向对象的编程语言,具有跨平台.可移植.分布式.简单.可扩展等诸多特性.Java能够进行桌面应用.Web应用.分布式系统及 ...