C. Journey
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Recently Irina arrived to one of the most famous cities of Berland — the Berlatov city. There are n showplaces in the city, numbered from to n, and some of them are connected by one-directional roads. The roads in Berlatov are designed in a way such that there are no cyclic routes between showplaces. Initially Irina stands at the showplace , and the endpoint of her journey is the showplace n. Naturally, Irina wants to visit as much showplaces as she can during her journey. However, Irina's stay in Berlatov is limited and she can't be there for more than T time units. Help Irina determine how many showplaces she may visit during her journey from showplace to showplace n within a time not exceeding T. It is guaranteed that there is at least one route from showplace to showplace n such that Irina will spend no more than T time units passing it. Input
The first line of the input contains three integers n, m and T ( ≤ n ≤ ,   ≤ m ≤ ,   ≤ T ≤ ) — the number of showplaces, the number of roads between them and the time of Irina's stay in Berlatov respectively. The next m lines describes roads in Berlatov. i-th of them contains integers ui, vi, ti ( ≤ ui, vi ≤ n, ui ≠ vi,  ≤ ti ≤ ), meaning that there is a road starting from showplace ui and leading to showplace vi, and Irina spends ti time units to pass it. It is guaranteed that the roads do not form cyclic routes. It is guaranteed, that there is at most one road between each pair of showplaces. Output
Print the single integer k ( ≤ k ≤ n) — the maximum number of showplaces that Irina can visit during her journey from showplace to showplace n within time not exceeding T, in the first line. Print k distinct integers in the second line — indices of showplaces that Irina will visit on her route, in the order of encountering them. If there are multiple answers, print any of them. Examples
input output input output input output

  一道图论题,DFS t了,BFS MLE了。看来最优解应该是DP没跑了。

  下面尝试定义转移方程: dp[i][j]表示在经过了i个城市之后到达编号为j的城市所消耗的最小时间。

  转移方程写得好,一切问题就变的简单了。反观这题,题中一共三个变量,经历城市数(希望最大化),消耗的时间(希望最小化),当前城市编号(希望到达n)。

  为了达到上述目的,我们只需要在dp[i][n]<=T中找最大的i就好了, 另外开辟一个parent[i][j]:表示经历了i 个点后从parent[i][j]到达了j点。

IF u can reach v
dp[i][v] = min(dp[i][v], dp[i - ][u] + w(u, v));

  最后就是转移方程的第一维度可以从1->n枚举,第二维我们可以通过遍历整个边集合,来找到每一个u->v来更新当前经历过i个点下的dp[i][v]。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector> using namespace std; const int Maxn = ; int cntE, head[Maxn];
int n, m, T; typedef struct Node{
int from, to, next, w;
}Edge;
Edge edges[Maxn]; void init()
{
memset(head, -, sizeof head);
cntE = ;
}
void addEdge(int u, int v, int w)
{
edges[cntE].from = u;
edges[cntE].to = v;
edges[cntE].w = w;
edges[cntE].next = head[u];
head[u] = cntE ++;
}
int parent[Maxn][Maxn], dp[Maxn][Maxn]; void printPath(int pos)
{
printf("%d\n", pos);
int id = n;
vector<int>ans;
ans.push_back(id);
for(int i = pos; i > ; i --){
ans.push_back(parent[i][id]);
id = parent[i][id];
}
printf("%d",ans[ans.size() - ]);
for(int i = ans.size() - ; i >= ; i --){
printf(" %d",ans[i]);
}cout<<endl;
}
void solveByDp()
{
for(int i = ; i < n + ; i ++){
for(int j = ; j < n + ; j ++){
dp[i][j] = T + ;
}
}
dp[][] = ;
//经历了i个点
int u, v, w, pos = ;
for(int i = ; i <= n; i ++){
//到达j点
for(int j = ; j < m; j ++){
u = edges[j].from, v = edges[j].to, w = edges[j].w;
if(dp[i - ][u] + w < dp[i][v]){
dp[i][v] = dp[i - ][u] + w;
parent[i][v] = u;
}
}
if(dp[i][n] <= T){
pos = i;
}
}
printPath(pos);
} int main()
{
init();
int u, v, w;
cin>>n>>m>>T;
for(int i = ; i < m; i ++){
scanf("%d%d%d", &u, &v, &w);
addEdge(u, v, w);
}
solveByDp();
return ;
}

  

最新文章

  1. 带有runat=&quot;server&quot; 的服务器控件通过 ClientID 获取Id
  2. JVM之运行时常量池(Runtime Constant Pool)
  3. 接触到得到新语言里面涉及到很多关于ECMscript相关知识,所以还是来总结一下吧
  4. [New Portal]Windows Azure Virtual Machine (23) 使用Storage Space,提高Virtual Machine磁盘的IOPS
  5. php取默认值以及类的继承
  6. Myeclipse10下载,安装,破解,插件,优化介绍
  7. redis 的使用 (基础, key操作, string类型操作)
  8. JavaScript之Array常用函数汇总
  9. [ERROR] Plugin &#39;InnoDB&#39; init function returned error
  10. Tomcat Manager用户配置详解
  11. 学习:java设计模式—工厂模式
  12. linux c/c++ IP字符串转换成可比较大小的数字
  13. 关于“javax.servlet.include.request_uri”属性值
  14. Eclipse下使用Fat Jar插件对源代码进行打包
  15. 关于webservice不支持方法重载的解决办法
  16. Oracle function注释
  17. python cookbook学习笔记 第一章 文本(2)
  18. node.js入门系列(一)--Node.js简介
  19. OpenCV4Android背景建模(MOG、MOG2)
  20. 浅析is和as两个关键词在类型转换时的使用

热门文章

  1. 51nod 1002 数塔取数问题【dp】
  2. grep,cut,wc,sort,diff,uniq,patch命令
  3. 31.IK分词器配置文件讲解以及自定义词库
  4. PAT 1091. Acute Stroke (bfs)
  5. vue中对象属性改变视图不更新问题
  6. 【DEBUG】 Visual Studio 2005 DEBUG集
  7. 一个oracle bug
  8. PHP array_count_values()
  9. SDUT OJ -2892 A
  10. selenium获取弹窗提示