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;
}
CodeForces 721B Journey (DP)的更多相关文章
- CodeForces 839C - Journey | Codeforces Round #428 (Div. 2)
起初误以为到每个叶子的概率一样于是.... /* CodeForces 839C - Journey [ DFS,期望 ] | Codeforces Round #428 (Div. 2) */ #i ...
- Codeforces Round #374 (Div. 2) C. Journey DP
C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...
- 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 ...
- codeforces 721C C. Journey(dp)
题目链接: C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
- CodeForces 721C Journey(拓扑排序+DP)
<题目链接> 题目大意:一个DAG图有n个点,m条边,走过每条边都会花费一定的时间,问你在不超过T时间的条件下,从1到n点最多能够经过几个节点. 解题分析:对这个有向图,我们进行拓扑排序, ...
- CF721C. Journey[DP DAG]
C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard input ou ...
- CF #374 (Div. 2) C. Journey dp
1.CF #374 (Div. 2) C. Journey 2.总结:好题,这一道题,WA,MLE,TLE,RE,各种姿势都来了一遍.. 3.题意:有向无环图,找出第1个点到第n个点的一条路径 ...
- codeforces 682D(DP)
题目链接:http://codeforces.com/contest/682/problem/D 思路:dp[i][j][l][0]表示a串前i和b串前j利用a[i] == b[j]所得到的最长子序列 ...
- codeforces 666A (DP)
题目链接:http://codeforces.com/problemset/problem/666/A 思路:dp[i][0]表示第a[i-1]~a[i]组成的字符串是否可行,dp[i][1]表示第a ...
随机推荐
- linux 常见名词及命令(二)
pwd 用于显示当前的工作目录. cd 用于切换工作路径 cd - 切换到上一次的目录 cd ~ 切换到家目录 cd ~username 切换到其他用户的家目录 cd .. 切换到上级目录 ls 用于 ...
- Atom安装Markdown编辑器
1.安装插件 2.打开/关闭实时渲染: [Ctrl]+[Shift]+[M] 3.[增强]安装同步滚动插件(markdown-scroll-sync) 4.[增强]安装代码增强插件(language- ...
- JavaOne Online Hands-on Labs
http://www.oracle.com/technetwork/java/index-156938.html
- 【.Net Core 学习系列】-- EF Core 实践(Code First)
一.开发环境: VS2015, .Net Core 1.0.0-preview2-003156 二解决方案: 新建项目: File --> New --> Project --> ...
- sublime text 3(Build 3103)最新注冊码
原来注冊过的sublime text 3近期更新了.没想到原来的注冊码就失效了,只是我找到了最新的注冊码(Build 3103),与大家分享一下(第一个亲測可用). -– BEGIN LICENSE ...
- Selenium系列之--04 常见元素操作总结
一.Selenium总共有八种定位方法 By.id() 通过id定位 By.name() 通过name 定位 By.xpath() 通过xpath定位 By.className() 通过clas ...
- HDFS的体系架构
通过对Hadoop分布式计算平台最核心的分布式文件系统HDFS.MapReduce处理过程,以及数据仓库工具Hive和分布式数据库Hbase的介绍.基本涵盖了Hadoop分布式平台的全部技术核心. H ...
- git 删除目录
1. 查看本地已经被删除的文件 2. 删除 目录以及目录下的文件 [root@test01 h2_mopub_replace]# git rm ../test_code_driver -r 3. [r ...
- Flask采用Virtualenv+Supervisor+Nginx部署应用
Flask采用Virtualenv+Supervisor+Nginx部署应用 -- 首先是概念解释 WSGI服务器,负责我们的app与服务器的交互,常用的有Gunicorn Web服务器,是个HTTP ...
- 关于axis2.1.6与websphere7的包冲突问题的解决方式
1,复制axis2.1.6内的module目录内的全部文件到lib 并改动扩展名为.jar 2,删除module目录(可选,不删除也能够) 3,部署到was 4,设置was相应应用程序的类载入方案为父 ...