1. 程式人生 > >Light oj 1379 -- 最短路

Light oj 1379 -- 最短路

def main pla its rem ive queue make using

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 c. u 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,枚舉所有邊找到最優解。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define inf 0x3f3f3f3f
 4 #define LL long long 
 5 int first1[10010],first2[10010];
 6 struct Edge
 7 {
 8     int u,v,w,next;
 9 }e1[50050],e2[50050];
10 struct node
11 {
12     int u,w;
13     bool operator<(const node&chs)const{
14         return w>chs.w;
15     }    
16 };
17 int tot1,tot2;
18 void add1(int u,int v,int w)
19 {
20     e1[tot1].u=u;
21     e1[tot1].v=v;
22     e1[tot1].w=w;
23     e1[tot1].next=first1[u];
24     first1[u]=tot1++;
25 }
26 void add2(int u,int v,int w)
27 {
28     e2[tot2].u=u;
29     e2[tot2].v=v;
30     e2[tot2].w=w;
31     e2[tot2].next=first2[u];
32     first2[u]=tot2++;
33 }
34 int d1[10010],d2[10010];
35 bool vis[10010];
36 void dij(int s,int d[],Edge e[],int first[])
37 {
38     priority_queue<node>q;
39     memset(d,inf,sizeof(int)*10003);
40     memset(vis,0,sizeof(bool)*10003);
41     q.push(node{s,0});
42     d[s]=0;
43     while(!q.empty()){
44         int u=q.top().u;
45         q.pop();
46         if(vis[u]) continue;
47         vis[u]=1;
48         for(int i=first[u];i+1;i=e[i].next){
49             if(d[u]+e[i].w<d[e[i].v]){
50                 d[e[i].v]=d[u]+e[i].w;
51                 q.push(node{e[i].v,d[e[i].v]});
52             }
53         }
54     }
55     
56 }
57 int main()
58 {
59     int T,N,M,s,t,p;
60     int u,v,w;
61     int i,j,k;
62     int cas=0;
63     cin>>T;
64     while(T--){cas++;
65         tot1=tot2=0;
66         memset(first1,-1,sizeof(first1));
67         memset(first2,-1,sizeof(first2));
68         cin>>N>>M>>s>>t>>p;
69         while(M--){
70             scanf("%d%d%d",&u,&v,&w);
71             add1(u,v,w);
72             add2(v,u,w);
73         }
74         dij(s,d1,e1,first1);
75         dij(t,d2,e2,first2);
76         int ans=-1;
77         for(i=1;i<=N;++i){
78             for(j=first1[i];j+1;j=e1[j].next){
79                 if(d1[e1[j].u]+d2[e1[j].v]+e1[j].w<=p){
80                     ans=max(ans,e1[j].w);
81                 }
82             }
83         }
84         cout<<"Case "<<cas<<": ";
85         cout<<ans<<endl;
86     }
87     return 0;
88 }

Light oj 1379 -- 最短路