Foki is the president of the Martian United States of Altanie, Altanie is a very large and strange country. Each citizen in it has a positive integer wisdom value calculated based on her/his age and educational level (of course Foki has the maximum value). Altanie has a big map for all its roads, this map has the following properties:
- There are N cities in Altanie, and the cities are numbered from 1 to N.
- Each road connects 2 different cities, and all roads are bidirectional.
- Each road requires a minimal wisdom value for the citizen to have the right to use it.
- Each road costs some amount of Martian money to use it.
- There is at most one road between each 2 cities.
Foki cares about all people of his country, so he is wondered about the minimum wisdom value that is needed to go from city 1 to city N with a total cost less than K, your job is to answer this question for him.
The first line of the input contains T the number of the test cases. The first line of each test contains 1 < N ≤ 105 the number of the cities in Altanie, 1 ≤ M ≤ 105, the number of roads connecting the N cities and 1 ≤ K ≤ 109 the total cost.
Each of the next M lines contain a description of one of the M roads, each road is described with 1 ≤ s1, s2 ≤ N the numbers of two cities the road connects,1 ≤ c ≤ 109 the cost you have to pay each time you use this road, 1 ≤ W ≤ 109the minimal amount of wisdom value needed to have the right to use the road.
For each test case print one line contains the answer of the following question: What is the minimum wisdom value a citizen should have to be able to go from city 1 to city N with cost less than K? if there is no solution, print -1.
2 5 6 3 1 2 1 1 1 4 1 1 1 3 1 1 2 5 2 1 2 4 1 1 3 5 1 5 5 6 2 1 2 1 1 1 4 1 1 1 3 1 1 2 5 2 1 2 4 1 1 3 5 1 5
5 -1
Warning: large Input/Output data, be careful with certain languages.
题目大意:在一个含有1到n个点的图中,每条边上都有一个花费和走这条路的所需具备的智慧值,要求找到一个满足花费低于k的最短路从1到N,求出最小的智慧值要求
解题思路:这道题你既要考虑最小花费不高于K且要求在这条路上的所需智慧值更低,一开始没有什么思路,后来想到的是每次二分一个最小的智慧值,然后每次用spfa检查这个智慧值下的最短路花费是否超过K
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long LL;
int n,m;
LL flag;
LL INF=1<<30;
struct point
{
LL c,w;
};
LL dis[100005];
bool vis[100005];
vector< pair<int,point> > tu[100005];
LL spfa(LL x)
{
int i,y;
point t;
pair<int,point> tt;
for(i=1;i<=n;i++)
{
dis[i]=INF;
vis[i]=0;
}
vis[1]=1;
dis[1]=0;
t.c=t.w=0;
queue< pair<int,point> > qua;
qua.push(make_pair(1,t));
while(!qua.empty())
{
tt=qua.front();
qua.pop();
y=tt.first;
for(i=0;i<tu[y].size();i++)
{
tt=tu[y][i];
LL cost=(tt.second).c;
if(dis[tt.first]>cost+dis[y]&&(tt.second).w<=x)
{
dis[tt.first]=cost+dis[y];
if(vis[tt.first]==0)
{
qua.push(tt);
vis[tt.first]=1;
}
}
}
vis[y]=0;
}
return dis[n];
}
bool check(LL x)
{
LL ans=spfa(x);
if(ans>=flag)
return false;
else
return true;
}
int main()
{
int T,i,x,y;
LL c,w,l,r,k;
cin>>T;
point a;
while(T--)
{
memset(tu,0,sizeof(tu));
cin>>n>>m>>flag;
for(i=1;i<=m;i++)
{
cin>>x>>y>>a.c>>a.w;
tu[x].push_back(make_pair(y,a));
tu[y].push_back(make_pair(x,a));
}
LL ans=spfa(INF);
if(ans>=flag)
{
cout<<-1<<endl;
continue;
}
l=-1,r=INF;
while(l<r-1)
{
k=(l+r)/2;
if(check(k))
r=k;
else
l=k;
}
cout<<r<<endl;
}
return 0;
}