Gym - 100814G Galway Girl (最短路spfa+二分)
2017-08-09 20:17
399 查看
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.
题目大意:在一个含有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
;
}
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;
}
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
;
}
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;
}
相关文章推荐
- Gym - 101490E-最短路限制&二分&好题-Charles in Charge
- Gym - 101490E Charles in Charge【最短路+二分答案】
- poj 2391 网络流 二分 最短路 拆点
- GYM 101149 B.No Time for Dragons【二分+贪心】
- Gym - 101190E Expect to Wait(数形结合 , 二分, 查分)
- hdu 2962(二分答案+最短路)
- Gym - 101194D(二分
- hdu 2962(最短路+二分)
- Gym 100733H Designation in the Mafia(最短路)
- poj 3662 二分+最短路
- Gym - 100625J Jailbreak 最短路+搜索
- hdu 2962(最短路+二分)
- 【二分】【半平面交】Gym - 101309J - Jungle Outpost
- HDU3143Speedy Escape 最短路+二分+搜索
- 【最短路】BAPC2014 B Button Bashing (Codeforces GYM 100526)
- Gym 101020H Weekend 【枚举最短路 】
- bzoj 1614 Telephone Lines架设电话线 - 二分答案 - 最短路
- poj3662,二分 + 最短路,好题啊
- HDU 2962 Trucking 二分+最短路(带限制最短路)\\不满足递推关系,不能直接是spfa
- L2-001. 紧急救援 SPFA+记录路径,统计最短路