spfa 反向建边 两次求最短路 Toll Management

 Toll Management

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 43  Solved: 16
[Submit][Status][Web Board]

Description

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
对于每个边u->v,如果u到start的最短距离+v到end的最短距离+w(u,v)<= P 的话则该边就是满足条件的边
View Code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;
const int maxn=10010;
const int maxe=50010;
const int INT_MAX=100000000;
struct node
{
int s,to,w,next;
}e[2][maxe];
int tot[2],dest1[maxn],dest2[maxn],vis1[maxn],vis2[maxn],n,m,start,end,P;
int head[2][maxn];
void addedge(int u,int v,int w,int id)
{
e[id][tot[id]].s=u,e[id][tot[id]].to=v,e[id][tot[id]].w=w,e[id][tot[id]].next=head[id][u],head[id][u]=tot[id]++;
}
void spfa()
{
queue<int> q;
q.push(start);
for(int i=1;i<=n;i++)
dest1[i]=INT_MAX;
dest1[start]=0;
memset(vis1,0,sizeof(vis1));
while(!q.empty())
{
int cur=q.front();
q.pop();
vis1[cur]=0;
// cout<<head[0][cur]<<endl;
for(int i=head[0][cur];i!=-1;i=e[0][i].next)
{
int v=e[0][i].to;
// cout<<cur<<" "<<v<<endl;
if(dest1[v]>dest1[cur]+e[0][i].w)
{
dest1[v]=dest1[cur]+e[0][i].w;
if(!vis1[v]) {vis1[v]=1;q.push(v);}
}
}
}
for(int i=1;i<=n;i++)
dest2[i]=INT_MAX;
dest2[end]=0;
memset(vis2,0,sizeof(vis2));
q.push(end);
while(!q.empty())
{
int cur=q.front();
q.pop();
vis2[cur]=0;
for(int i=head[1][cur];i!=-1;i=e[1][i].next)
{
int v=e[1][i].to;
if(dest2[v]>dest2[cur]+e[1][i].w)
{
dest2[v]=dest2[cur]+e[1][i].w;
if(!vis2[v]) {vis2[v]=1;q.push(v);}
}
}
}
// for(int i=1;i<=n;i++)
// cout<<dest1[i]<<" "<<dest2[i]<<endl;
}
void solve()
{
int ans=-1;
for(int i=0;i<tot[0];i++)
{
int s=e[0][i].s,t=e[0][i].to,w=e[0][i].w;
if(dest1[s]+dest2[t]+w<=P&&ans<w) ans=w;
}
printf("%d\n",ans);
}
int main()
{
int ca,a,b,c,cas=1;
cin>>ca;
while(ca--)
{
cin>>n>>m>>start>>end>>P;
tot[0]=tot[1]=0;
memset(head,-1,sizeof(head));
while(m--)
{
scanf("%d %d %d",&a,&b,&c);
addedge(a,b,c,0);
addedge(b,a,c,1);
}
// cout<<tot[0]<<" "<<tot[1]<<endl;
spfa();
printf("Case %d: ",cas++);
solve();
}
return 0;
}


 

posted on 2012-04-07 15:54  Goal  阅读(255)  评论(0编辑  收藏  举报

导航