We all love short and direct problems, it is easier to write, read and understand the problem statement.
Here is one of these problems. \Life is too short to make a story", said Ahmed Aly.
You are given a weighted directed graph of N nodes (the nodes are numbered from 1 to N), where
the weights of the edges are distinct and positive. For each graph, you are also given a list of queries
to answer.
Each query will be represented by 3 integers A B C, which means you need to nd the shortest
path (the path with minimum sum of weights of its edges) which goes from node A to node B and uses
at most C edges, such that the weights of the edges in that path are in increasing order along the path,
which means the weight of each edge in that path should be greater than the weight of the edge before
it (unless it is the rst edge in the path).
Your task is to write a program which answers these queries.
Input
Your program will be tested on one or more test cases. The rst line of the input will be a single
integer T, the number of test cases (1 T 100). Followed by the test cases, the rst line of each
test case contains 3 integers separated by a single space N M Q (2 N 150), (0 M 3; 000) and
(1 Q 1; 000) representing the number of nodes, the number of edges and the number of queries,
respectively. Followed by M lines, each line contains 3 integers separated by a single space X Y Z
(1 X; Y N) (1 Z 3; 000) which represent an edge going from the node X to the node Y with
cost Z (X and Y will be different). Followed by Q lines, each line contains 3 integers separated by a
single space A B C (1 A;B N) (0 C M) which represent a query as described above (A and
B will be different).
Note that there might multiple edges between the same pair of nodes.
Output
For each test case, print a single line for each query which contains a single integer, the minimum sum
of weights for a path between the given pair of nodes which satises the given constraints, or `-1' if
there is no valid path between the given nodes which satises the given constraints. The output must
not contain empty lines between the cases.
Sample Input
1
8 9 3
1 2 1
2 3 2
3 4 3
4 5 12
5 8 7
1 6 8
6 4 9
1 7 5
7 4 4

1 4 2
1 4 3
1 4 1

Sample Output
17
6
-1

30秒 ,100个点,3000条边,1000次询问。

从x到y用的边数不超过b条而且每次走过的边的权值都要递增所需要的权值和。

先对边进行排序。

然后用dp[i][j][k] 表示 i -> j 用的边数等于 k 条 所需要的最少权值。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=;
const int M=;
const int inf = 1e9; struct node
{
int x,y,w;
bool operator < (const node &a)const{
return w<a.w;
}
}e[N]; int dp[M][M][M];
int n,m,q;
void update(int &a,int b){
if(a==-||b<a)a=b;
}
void run()
{
int x,y,b,w;
scanf("%d%d%d",&n,&m,&q);
memset(dp,-,sizeof(dp));
for(int i=;i<=n;++i)
dp[i][i][]=; for(int i=;i<m;++i)
{
scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].w);
}
sort(e,e+m);
for(int i=;i<m;++i){
for(int j=;j<=n;++j){
for(int k=;k<n;++k){
if( dp[j][e[i].x][ k- ] != -){
update(dp[j][e[i].y][k],dp[j][e[i].x][k-]+e[i].w);
}
}
}
} for(int i=;i<q;++i){
scanf("%d%d%d",&x,&y,&b);
if(b >= n) b=n-;
int ans = -;
for(int j=;j<=b;++j){
if( dp[x][y][j] != -) update(ans,dp[x][y][j]);
}
printf("%d\n",ans);
}
}
int main()
{
int _;
scanf("%d",&_);
while(_--)run();
return ;
}

UVAlive 6756 Increasing Shortest Path的更多相关文章

  1. hdu-----(2807)The Shortest Path(矩阵+Floyd)

    The Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  2. zoj 2760 How Many Shortest Path 最大流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...

  3. The Shortest Path in Nya Graph

    Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...

  4. hdu 3631 Shortest Path(Floyd)

    题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...

  5. Shortest Path(思维,dfs)

    Shortest Path  Accepts: 40  Submissions: 610  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: ...

  6. Shortest Path

    Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  7. (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

  8. 【ZOJ2760】How Many Shortest Path

    How Many Shortest Path 标签: 网络流 描述 Given a weighted directed graph, we define the shortest path as th ...

  9. [Swift]LeetCode847. 访问所有节点的最短路径 | Shortest Path Visiting All Nodes

    An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...

随机推荐

  1. jvm监控和诊断工具

    大牛写的Java的OOM Killer:https://www.jianshu.com/p/4645254be259 强烈推荐 总的参考链接:https://cloud.tencent.com/dev ...

  2. AOP技术介绍--(引言)

    软件设计因为引入面向对象思想而逐渐变得丰富起来.“一切皆为对象”的精义,使得程序世界所要处理的逻辑简化,开发者可以用一组对象以及这些对象之间的关系将软件系统形象地表示出来.而从对象的定义,进而到模块, ...

  3. 学习旧岛小程序 (1) flex 布局

    css : view 相当于 div 块级元素 display 默认设置 block display:inline 设置后 设置宽度高度是无效的 要设置宽度高度 又要设置为行内元素 我们设置: (1) ...

  4. 动态全屏弹窗特效 Morphing Modal Window

    动态变形弹窗特效 Morphing Modal Window 弹出窗体是网页常用的一个交互设计,在这个注重交互动画体验的时代,网页弹窗也可以来点新鲜的点子,比如今天分享的CSS 变形Modal Win ...

  5. 前端每日实战:93# 视频演示如何用纯 CSS 创作一根闪电连接线

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/RBjdzZ 可交互视频 此视频是可 ...

  6. K个串

    题目链接 传送门 题解 看完题目后可以立刻想到:先算出最大值, 然后把最大值剔除掉,再找此时的最大值也就是次大值.这样重复\(k\)边即可找到第\(k\)大值. 于是我们只需要考虑找最大值了 我们可以 ...

  7. rest认证组件,权限组件,频率组件,url注册器,响应器组件,分页器组件

    1.认证组件 1.1 认证组件利用token来实现认证 1.2 token认证的大概流程 用户登录===>获取用户名和密码===>查询用户表 如果用户存在,生成token,否则返回错误信息 ...

  8. 030:spaceless和autoescape 标签

    1.spaceless 标签: spaceless 标签:移除html标签中的空白字符.包括空格.tab键.换行等.示例代码如下: {% spaceless %} <p> <a hr ...

  9. Idea配置注释

    Idea配置注释 方法注释 点击+号 选择2 template Group 自己随便填个有意义的name(如图的mn就是我填写的) 点击你上步填写的name (我的是mn),然后点击+选择1 Live ...

  10. webdriver显式和隐式等待、强制等待

    implicitly_wait() 方法是隐式等待,用来设置超时,一般把implicitly_wait()方法调用在加载测试地址后,等待所测试的应用程序加载WebDriverWait() 是显式等待, ...