CodeForces 721B Journey (DP)

题意:给定一个有向图,你从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;
}
时间: 2024-12-28 06:43:40

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

Codeforces 13C Sequence --DP+离散化

题意:给出一个 n (1 <= n <= 5000)个数的序列 .每个操作可以把 n 个数中的某一个加1 或 减 1.问使这个序列变成非递减的操作数最少是多少 解法:定义dp[i][j]为将前i个数变为以j为结尾的非递减序列的最少操作次数. 则有: dp[i][j] = min(dp[i][j], min(dp[i][k]) + Cost(原来第i个位置上的数转换到j))  (1 <= k <= j) 即前i个数以j结尾的状态可以由前i-1个数以小于等于j的k结尾的状态转移过来,取

Codeforces 77C 树形dp + 贪心

题目链接:点击打开链接 题意: 给定n个点, 每个点的豆子数量 下面是一棵树 再给出起点 每走到一个点,就会把那个点的豆子吃掉一颗. 问:回到起点最多能吃掉多少颗豆子 思路:树形dp 对于当前节点u,先把子节点v都走一次. 然后再往返于(u,v) 之间,直到u点没有豆子或者v点没有豆子. dp[u] 表示u点的最大值.a[u] 是u点剩下的豆子数. #include <cstdio> #include <vector> #include <algorithm> #inc

Codeforces 57C Array dp暴力找规律

题目链接:点击打开链接 先是计算非递增的方案, 若非递增的方案数为x, 则非递减的方案数也是x 答案就是 2*x - n 只需求得x即可. 可以先写个n3的dp,然后发现规律是 C(n-1, 2*n-1) 然后套个逆元即可. #include<iostream> #include<cstdio> #include<vector> #include<string.h> using namespace std; #define ll long long #def

Codeforces 413D 2048(dp)

题目连接:Codeforces 413D 2048 题目大意:2048的游戏,两个相同的数x可以变成一个2*x,先给出n,表示在一个1*n的矩阵上面玩2048,规定每次向左移动,并且每次出现一个,给出序列n,表示出现的块的值,0表示既可以是2也可以是4,问说有多少种可能,使得游戏结束后的最大块的值大于等于2^k. 解题思路:dp[i][j][x]表示第i个位置,值为j,x表示先前有没有出现过大于2^k的数: 这种递增的情况可以直接表示为14(总和,以为后面的2,4如果变大,就肯定能和8想合在一起

Codeforces 455A Boredom (dp)

很裸的dp 状态转移方程 dp[i]=max(dp[i-1],dp[i-2]+dp[i]*i) #include<bits/stdc++.h> using namespace std; long long dp[100020]; int main() { int n,a; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a); dp[a]++; } for(int i=2;i&

Codeforces 176B 经典DP

非常好的一个题目,CF上的DP都比较经典 题意就是 给定一个串A,B,正好执行K次操作,每次操作可以把 A串从中间切开,并调换两部分的位置,问最后得到B串共有多少种不同的切法(只要中间有一次不同,即视为不同) 首先,题目的一个关键点一定要抓到,就是 ,不管怎么切 然后调换位置,其实串根本没变,你把串想成一个环,从某一点分成两部分并且交换位置,其实就是把串的起点变到了该点,这是很关键也是最机智的一点 然后,我们要发现规律,你纸上模拟也行,推理也行.. 我们发现:1.首先原串(即以0号字母开头的)个

Nanami&#39;s Digital Board CodeForces - 434B (棋盘dp)

大意: 给定01矩阵, m个操作, 操作1翻转一个点, 操作2求边界包含给定点的最大全1子矩阵 暴力枚举矩形高度, 双指针统计答案 #include <iostream> #include <algorithm> #include <math.h> #include <cstdio> #include <set> #include <map> #include <string> #include <vector>

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

C. Journey 题意: 在一个DAG(有向无环图)中,问从1 到 n 点,在时间限制K下,最多能游玩几个地点,把游玩的顺序顺便输出. 思路: 感觉dp,一维不够就加一维,我一开始有想到dp,但是只是一维的去推,推着感觉不正确.这次用dp[i][j],表示到j点已游玩i个地点的最少时间. DAG中一般要想到求拓扑序.即保证dp从左到右. 这题这个人直接两重循环求dp,不知道为啥也是对的 ,确实是有道理,因为dp[ i - 1]这一行的所有情况对于 i 行都是确定的,且i行一定是在i-1行存在

CodeForces 407B 简单dp

//CodeForces 407B 1 #include "iostream" 2 #include "cstdio" 3 #include "cstring" 4 #include "algorithm" 5 using namespace std; 6 const __int64 mod = 1e9 + 7; 7 __int64 dp[1010]; 8 int pos[1010], n; 9 10 int main() 1