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
output
Note
Warning: large Input/Output data, be careful with certain languages.
一个无向图,没有重边。所有路有一个花费,有一个最小智慧值,经过某条路径需要本人的智慧值大于等于所有路径的值智慧。问花费不超过k时,从1到n的最小智慧值是多少。
时限11s,放的这么开应该想到二分答案
4000
。
二分最大智慧值,每次用spfa寻找有没有这样一条路径。
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; }
相关文章推荐
- Gym - 100814G It is all about wisdom
- It is All About The Data
- Codeforces Gym 100500F Problem F. Door Lock 二分
- Codeforces Gym 101608 G WiFi Password(尺取/二分+ST表 )
- Codeforces Gym 101505D Orchard Division(离散化+树状数组+扫描线+二分)
- CodeForces Gym 100989G Mission in Amman (B) 二分答案
- It is all about Using Text & Fonts in Flash
- codeforces Gym 100814 A、B、F、I
- Codeforces Round #185 (Div. 2) A. Whose sentence is it? 水题
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
- Codeforces Gym 100425A Luggage Distribution 二分 数学
- Codeforces Gym - 101234A Hacker Cups and Balls [二分+线段树]
- [二分答案 2-SAT验证 前缀后缀优化建图 线段树优化建图] Codeforces gym 100159 Facebook Hacker Cup 2012 I. Unfriending
- 【Educational Codeforces Round 2B】【map or 二分查找】Queries about less or equal elements b[]中的每个数比a[]中多少数大
- 【二分】NEERC15 L Landscape Improved (Codeforces GYM 100851)
- Codeforces Gym 221682 R Straight Shot(二分)
- Codeforces Gym 100741G Yet Another Median Task 二分乱搞
- Codeforces Gym 100825 C. KenKen You Do It? (暴搜 + 剪枝)
- 【二分】NEERC15 L Landscape Improved(2015-2016 ACM-ICPC)(Codeforces GYM 100851)
- Educational Codeforces Round 2B. Queries about less or equal elements(二分查找)