UVAlive 6756 Increasing Shortest Path
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的更多相关文章
- hdu-----(2807)The Shortest Path(矩阵+Floyd)
The Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- zoj 2760 How Many Shortest Path 最大流
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...
- The Shortest Path in Nya Graph
Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- Shortest Path(思维,dfs)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
- Shortest Path
Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- (中等) 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 ...
- 【ZOJ2760】How Many Shortest Path
How Many Shortest Path 标签: 网络流 描述 Given a weighted directed graph, we define the shortest path as th ...
- [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 ...
随机推荐
- unity2017 光照与渲染(一)
光照&渲染(基于unity2017.2.0) Custom Skybox 天空盒 最丰富的环境光 a. TextureShape 改成 Cube. b. 把图片直接丢给天空,就会自动生成材质. ...
- 字符串format函数使用
#format拼接字符串,format()内的参数必须为可迭代的对象p1="i am {2},age {1},{0}".format("seven",18,'a ...
- c++函数参数或返回值为函数指针
C++中函数指针的形式为:返回值类型 + 参数类型,函数没有值类型,但是却可以声明函数的指针,因为函数是可寻址的,存放在内存中的代码段,可以从指针访问. 函数指针可以声明为: void (*pF)(v ...
- vue2.0 之 nextTick
Vue.nextTick 将回调延迟到下次 DOM 更新循环之后执行.在修改数据之后立即使用它,然后等待 DOM 更新. 它跟实例方法vm.$nextTick() 一样,不同的是 this 自动绑定到 ...
- java并发学习第五章--线程中的锁
一.公平锁与非公平锁 线程所谓的公平,就是指的是线程是否按照锁的申请顺序来获取锁,如果是遵守顺序来获取,这就是个公平锁,反之为非公平锁. 非公平锁的优点在于吞吐量大,但是由于其不是遵循申请锁的顺序来获 ...
- 现在的编辑器不能复制粘贴word中的文本
我司需要做一个需求,就是使用富文本编辑器时,不要以上传附件的形式上传图片,而是以复制粘贴的形式上传图片. 在网上找了一下,有一个插件支持这个功能. WordPaster 安装方式如下: 直接使用Wor ...
- Java继承基础版
继承是软件开发中实现代码复用的有效手段,如果一个类A继承了类B那么类B中的public.protected及默认修饰符修饰的实例成员或静态成员将被类A继承,也可以说类B的成员就是类A的成员而类A在此基 ...
- [CSP-S模拟测试]:数论(数学)
题目传送门(内部题11) 输入格式 第一行,三个整数$T,K,M$,分别代表数据组数.良好标准和整数范围.接下来$T$行,每行一个整数$n_i$,代表一个询问. 输出格式 输出$T$行,在第$i$行对 ...
- python 使用xlsxwriter的方法属性
http://xlsxwriter.readthedocs.io/format.html
- 最长上升子序列(LIS)动态规划
最长上升子序列 给你n个整数 A1 A2 ········· An 找出在这个数组里面的最长上升的子序列.例如给你(1,7,3,5,9,4,8),他的上升子序列有(1,7) (3,4,8)等等之类的, ...