In Dhaka there are too many vehicles. So, the result is well known, yes, traffic jam. So, mostly people have to spend quite a time in the roads to go from one place to another.

Now, the students have finally found a solution to this problem. The idea is to make all the roads one way. That means a vehicle can go through the roads in one way only. And to make the number of vehicles low, each vehicle has to pay a toll to use a road. Now you want to go from a place s to another place t. And you have a total of p taka in your pocket. Now you want to find the path which contains the highest toll road, to go from s to t. Remember that you can't use more than p taka.

For the given picture, s = 1, t = 5 and p = 10. There are three paths from 1 to 5.

  1. Path 1: 1 - 2 - 5, total toll = 11 (> p)
  2. Path 2: 1 - 3 - 5, total toll = 9 (≤ p), 6 is the maximum toll
  3. Path 3: 1 - 4 - 5, total toll = 9 (≤ p), 5 is the maximum toll

So the maximum toll for a road of all of the paths having total toll not greater than p is 6.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case starts with five integers N (2 ≤ N ≤ 10000), M (1 ≤ M ≤ 50000), s (1 ≤ s ≤ N), t (1 ≤ t ≤ N) and p (1 ≤ p ≤ 106) where N means the number of junctions and M means the number of roads connecting the junctions. Then there will be M lines each containing three integers u v cu and v are junctions and there is a road from u to v (1 ≤ u, v ≤ N, u ≠ v) and c (0 ≤ c ≤ 105) is the toll needed for that road. There can be multiple roads between two junctions.

Output

For each case, print the case number and the desired result. If no such result is found, print"-1".

Sample Input

2

5 6 1 5 10

1 2 7

2 5 4

1 3 6

3 5 3

1 4 5

4 5 4

2 1 1 2 10

1 2 20

Sample Output

Case 1: 6

Case 2: -1

取反向边跑两次dij,枚举所有边找到最优解。

 #include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define LL long long
int first1[],first2[];
struct Edge
{
int u,v,w,next;
}e1[],e2[];
struct node
{
int u,w;
bool operator<(const node&chs)const{
return w>chs.w;
}
};
int tot1,tot2;
void add1(int u,int v,int w)
{
e1[tot1].u=u;
e1[tot1].v=v;
e1[tot1].w=w;
e1[tot1].next=first1[u];
first1[u]=tot1++;
}
void add2(int u,int v,int w)
{
e2[tot2].u=u;
e2[tot2].v=v;
e2[tot2].w=w;
e2[tot2].next=first2[u];
first2[u]=tot2++;
}
int d1[],d2[];
bool vis[];
void dij(int s,int d[],Edge e[],int first[])
{
priority_queue<node>q;
memset(d,inf,sizeof(int)*);
memset(vis,,sizeof(bool)*);
q.push(node{s,});
d[s]=;
while(!q.empty()){
int u=q.top().u;
q.pop();
if(vis[u]) continue;
vis[u]=;
for(int i=first[u];i+;i=e[i].next){
if(d[u]+e[i].w<d[e[i].v]){
d[e[i].v]=d[u]+e[i].w;
q.push(node{e[i].v,d[e[i].v]});
}
}
} }
int main()
{
int T,N,M,s,t,p;
int u,v,w;
int i,j,k;
int cas=;
cin>>T;
while(T--){cas++;
tot1=tot2=;
memset(first1,-,sizeof(first1));
memset(first2,-,sizeof(first2));
cin>>N>>M>>s>>t>>p;
while(M--){
scanf("%d%d%d",&u,&v,&w);
add1(u,v,w);
add2(v,u,w);
}
dij(s,d1,e1,first1);
dij(t,d2,e2,first2);
int ans=-;
for(i=;i<=N;++i){
for(j=first1[i];j+;j=e1[j].next){
if(d1[e1[j].u]+d2[e1[j].v]+e1[j].w<=p){
ans=max(ans,e1[j].w);
}
}
}
cout<<"Case "<<cas<<": ";
cout<<ans<<endl;
}
return ;
}

Light oj 1379 -- 最短路的更多相关文章

  1. Light OJ 1316 A Wedding Party 最短路+状态压缩DP

    题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...

  2. Light OJ 1114 Easily Readable 字典树

    题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...

  3. Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖

    题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...

  4. Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖

    标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...

  5. light oj 1007 Mathematically Hard (欧拉函数)

    题目地址:light oj 1007 第一发欧拉函数. 欧拉函数重要性质: 设a为N的质因数.若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N ...

  6. Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖

    题目来源:Light OJ 1406 Assassin`s Creed 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...

  7. Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩

    题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...

  8. Jan's light oj 01--二分搜索篇

    碰到的一般题型:1.准确值二分查找,或者三分查找(类似二次函数的模型). 2.与计算几何相结合答案精度要求比较高的二分查找,有时与圆有关系时需要用到反三角函数利用 角度解题. 3.不好直接求解的一类计 ...

  9. Light OJ 1272 Maximum Subset Sum 高斯消元 最大XOR值

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011686226/article/details/32337735 题目来源:problem=12 ...

随机推荐

  1. SQL Server 使用 Hierarchyid 操作层次结构数据

    层次结构数据定义为一组通过层次结构关系互相关联的数据项. 在层次结构关系中,一个数据项是另一个项的父级或子级. sql server2008开始内置的 hierarchyid 数据类型使存储和查询层次 ...

  2. js的同步异步

    由于js没有多线程,所以处理多任务的时候,可以用异步回调来解决.js中setTimeout.setInterval.ajax(jq中可以选择同步或异步)均会开启异步.遇到异步模块,会将其推入值任务队列 ...

  3. eclipse 编译的时候 自动把SDK需要放入libs里面的so文件给删除了

    解决方法: 右击Project,选Properties->Builders, 把CDT Builder 关掉. 这样就不会编译了.包括c++的代码也不会编译.. 治标不治本啊...以后c++代码 ...

  4. win7 eclipse设置Courier New字体

    win7系统 1.控制面板-->字体.找到Courier New 字体,右键->显示,这种字体就开始变亮了. 2.eclipse里设置: windows-->Preferences- ...

  5. 20145316许心远《Java学习笔记(第8版)》课程总结

    20145316许心远<Java学习笔记(第8版)>课程总结 每周读书笔记链接汇总 ▪ 第一周读书笔记 ▪ 第二周读书笔记 ▪ 第三周读书笔记 ▪ 第四周读书笔记 ▪ 第五周读书笔记 ▪ ...

  6. kivy sdl2 - ImportError: DLL load failed: 找不到指定的模块

    from kivy.app import App from kivy.uix.button import Button class TestApp(App): def build(self): ret ...

  7. 【c++ primer, 5e】函数匹配

    练习 6.49 候选函数:与所调用的函数的名字相同的函数的集合. 可行函数:给候选函数加上参数数量.参数类型的约束所得到的函数的集合. 6.50 a 3.4可行,二义匹配 b 2.4可行,2是最佳匹配 ...

  8. Swoole学习(三)Swoole之UDP服务器的创建

    环境:Centos6.4,PHP环境:PHP7 <?php //创建UCP服务器(UDP服务器相对于TCP服务器通信更可靠些) /** * $host 是swoole需要监听的ip,如果要监听本 ...

  9. Jquery6 DOM 节点操作

    学习要点: 1.创建节点 2.插入节点 3.包裹节点 4.节点操作 DOM 中有一个非常重要的功能,就是节点模型,也就是 DOM 中的“M”.页面中的元素结构就是通过这种节点模型来互相对应着的,通过这 ...

  10. 前端初级技能No.1 [切图]

    “切图”是指通过测量设计稿,从设计稿中提取图片等方式为页面开发提供支持的过程. 整个“切图”过程主要分为以下五个主要步骤: 分析设计图: 测量元素: 提取图片: 保存图片: 图片优化与合并: 1.分析 ...