Gym - 100814G Galway Girl (最短路spfa+二分)

25 篇文章 4 订阅
15 篇文章 0 订阅
G. It is all about wisdom
time limit per test
11.0 s
memory limit per test
1024 MB
input
standard input
output
standard output

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.

Input

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.

Output

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.

Examples
input
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
output
5
-1
Note

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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值