题意:给定一个有向图,你从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. isinstance()和issubclass()

    内置函数中有个两个函数经常用到 isinstance()                    对象 是否是 类 的一个对象 from collections import Iterable prin ...

  2. CodeForces 595A Vitaly and Night

    水题. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> u ...

  3. asterisk 通道变量

    ${ACCOUNTCODE}: 用户计费帐号 sip.conf 里的 account=XXXX ${ANSWEREDTIME}: 通话时长(秒) ${BLINDTRANSFER}: 通道是否为转接类型 ...

  4. CSS 空中飘动的云动画

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  5. Oracle冷备和热备脚本

    Oracle冷备和热备脚本 冷备脚本: set feedback off set heading off set verify  off set trimspool off set echo off ...

  6. 【scrapy】Item及Spider

    Items Item objects are simple containers used to collect the scraped data.They provide a dictionary- ...

  7. 使用WIN32汇编语言实现一个基本windows窗体的过程分析

    一个常规的windows窗体一般都是一些一样的构造.你假设想要更改一些个性化的设置,你能够在这个一般的模板伤添砖加瓦.构造自己比較喜欢的类型.下边就分析一下一般的windows窗体的一般模板. 一. ...

  8. Linux-github 搭建静态博客

    1.在Github上创建一个新的Repository 到你的github上 https://github.com去create a new repository命名为 github.myblog 2. ...

  9. bug集合及其解决方法

    点击查看原文 1. java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 27 ...

  10. JS 省市区三级联动

    JS 省市区三级联动: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...