C. Volleyball
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Petya loves volleyball very much. One day he was running late for a volleyball match. Petya hasn't bought his own car yet, that's why he had to take a taxi. The city has n junctions, some of which are connected by two-way roads. The length of each road is defined by some positive integer number of meters; the roads can have different lengths.
Initially each junction has exactly one taxi standing there. The taxi driver from the i-th junction agrees to drive Petya (perhaps through several intermediate junctions) to some other junction if the travel distance is not more than ti meters. Also, the cost of the ride doesn't depend on the distance and is equal to ci bourles. Taxis can't stop in the middle of a road. Each taxi can be used no more than once. Petya can catch taxi only in the junction, where it stands initially.
At the moment Petya is located on the junction x and the volleyball stadium is on the junction y. Determine the minimum amount of money Petya will need to drive to the stadium.
Input
The first line contains two integers n and m (1 ≤ n ≤ 1000, 0 ≤ m ≤ 1000). They are the number of junctions and roads in the city correspondingly. The junctions are numbered from 1 to n, inclusive. The next line contains two integers x and y(1 ≤ x, y ≤ n). They are the numbers of the initial and final junctions correspondingly. Next m lines contain the roads' description. Each road is described by a group of three integers ui, vi, wi (1 ≤ ui, vi ≤ n, 1 ≤ wi ≤ 109) — they are the numbers of the junctions connected by the road and the length of the road, correspondingly. The next n lines contain n pairs of integers ti and ci(1 ≤ ti, ci ≤ 109), which describe the taxi driver that waits at the i-th junction — the maximum distance he can drive and the drive's cost. The road can't connect the junction with itself, but between a pair of junctions there can be more than one road. All consecutive numbers in each line are separated by exactly one space character.
Output
If taxis can't drive Petya to the destination point, print "-1" (without the quotes). Otherwise, print the drive's minimum cost.
Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator.
Examples
input
Copy
4 4 1 3 1 2 3 1 4 1 2 4 1 2 3 5 2 7 7 2 1 2 7 7
output
Copy
9
Note
An optimal way — ride from the junction 1 to 2 (via junction 4), then from 2 to 3. It costs 7+2=9 bourles.
一、原题地址
二、大致题意
现在有n个城市,m条路。每条路包含起点xi终点yi和路段长度wi。在每个城市有一辆出租车,第i辆车最远能开ti的距离,所需的打车费用固定为ci(出租车不能在路的中途停下)。
现在给出一个起点s和终点en,求从起点到达终点所需的最小费用。
三、大致思路
首先将路的距离作为权值建边,在图上每个点跑堆优化过的迪杰斯特拉。这样就能得到每个点到所有点的最短路径,这里的复杂度是n*n*logn。因为n最大为1000所以这个复杂度是可以接受的。
然后再看每个司机能送你到达的所有点,重新建图,此时把打车费用作为权值。再从S跑迪杰斯特拉。
四、代码
#include <iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
typedef long long LL;
const LL INF=0x3f3f3f3f3f3f3f3f;
int S,E;
typedef struct
{
int v,next;
LL cost;
}Edge;
typedef struct
{
int v;
LL cost;
}Node;
bool operator <(const Node &a,const Node &b)
{
return a.cost>b.cost;
}
const int MAXN=50005,MAXM=2005;
struct Car
{
LL di,co;
}car[MAXN];
int m,n;
int head[2][MAXN];
LL dis[MAXN];
Edge e[2][2005005];
bool vis[MAXN];
int ind;
void add(int from,int to,LL cost,int ty)
{
e[ty][ind].next=head[ty][from];
e[ty][ind].v=to;
e[ty][ind].cost=cost;
head[ty][from]=ind++;
}
void Dijkstra(int start,int ty)
{
for(int i=1;i<=n;i++)dis[i]=INF,vis[i]=false;
Node t;
priority_queue<Node>q;
t.cost=0;
dis[start]=0;
t.v=start;
q.push(t);
while(!q.empty())
{
t=q.top();
q.pop();
if(vis[t.v])continue;
vis[t.v]=true;
for(int i=head[ty][t.v];i!=-1;i=e[ty][i].next)
{
if(!vis[e[ty][i].v]&&dis[e[ty][i].v]>dis[t.v]+e[ty][i].cost)
{
if((ty==1&&dis[t.v]+e[ty][i].cost<=car[start].di)||ty==0)
{
Node temp;
temp.v=e[ty][i].v;
temp.cost=e[ty][i].cost+t.cost;
dis[e[ty][i].v]=dis[t.v]+e[ty][i].cost;
q.push(temp);
}
}
}
}
if(ty&1)
{
for(int i=1;i<=n;i++)
{
if(car[start].di>=dis[i])
{
if(i==start)continue;
//printf("%d -> %d %d\n",start,i,car[start].co);
add(start,i,car[start].co,0);
}
}
}
else
{
if(dis[E]!=INF)printf("%lld\n",dis[E]);
else printf("-1\n");
}
}
int main()
{
scanf("%d %d",&n,&m);
scanf("%d %d",&S,&E);
for(int i=1;i<=MAXN-1;i++)
{
head[0][i]=head[1][i]=-1;
}
ind=0;
for(int i=1;i<=m;i++)
{
int u,v;
LL w;
scanf("%d %d %lld",&u,&v,&w);
add(u,v,w,1);
add(v,u,w,1);
}
for(int i=1;i<=n;i++)
{
scanf("%lld %lld",&car[i].di,&car[i].co);
Dijkstra(i,1);
}
Dijkstra(S,0);
return 0;
}