您的位置:首页 > 其它

Codeforces Gym 100814 G It is all about wisdom 二分+最短路

2017-07-28 12:33 1236 查看
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.

一个无向图,没有重边。所有路有一个花费,有一个最小智慧值,经过某条路径需要本人的智慧值大于等于所有路径的值智慧。问花费不超过k时,从1到n的最小智慧值是多少。

时限11s,放的这么开应该想到二分答案
4000


二分最大智慧值,每次用spfa寻找有没有这样一条路径。

#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <bitset>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
const int maxn=100005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
int head[maxn],dist[maxn];
int num,k,n,m;
bool inque[maxn];

struct Edge {
int from,to,pre,dist,wis;
};
Edge edge[maxn*2];

void addedge (int from,int to,int dist,int wis) {
edge[num]=(Edge){from,to,head[from],dist,wis};
head[from]=num++;
edge[num]=(Edge){to,from,head[to],dist,wis};
head[to]=num++;
}

bool find(int n,int maxd,int mwis) {
queue<int> q;
q.push(1);
meminf(dist);
mem0(inque);
dist[1]=0;inque[1]=1;
while (!q.empty()) {
int now=q.front();
q.pop();
inque[now]=0;
for (int i=head[now];i!=-1;i=edge[i].pre) {
int to=edge[i].to;
if (dist[now]+edge[i].dist<dist[to]&&dist[now]+edge[i].dist<k&&edge[i].wis<=mwis) {
dist[to]=dist[now]+edge[i].dist;
if (to==n) return true;
if (!inque[to]) {
inque[to]=1;
q.push(to);
}
}
}
}
return false;
}

int solve(int n) {
int l,r,mid,ans=-1;
l=1;r=1e9;
while (l<=r) {
mid=(l+r)/2;
if (find(n,k,mid)) {
ans=mid;
r=mid-1;
} else l=mid+1;
}
return ans;
}

int main() {
int cas;
scanf("%d",&cas);
while (cas--) {
num=0;
scanf("%d%d%d",&n,&m,&k);
int i,j,x,y,w,d;
memset(head,-1,sizeof(head));
for (i=1;i<=m;i++) {
scanf("%d%d%d%d",&x,&y,&d,&w);
addedge(x,y,d,w);
}
int ans=solve(n);
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: