沐枫NOI 17. Volleyball
来源:互联网 发布:英雄传奇挂机软件 编辑:程序博客网 时间:2024/05/18 00:45
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 C++. It is preferred to use cin, cout streams or the %I64d specificator.
Examples
Input
4 41 31 2 31 4 12 4 12 3 52 77 21 27 7
Output
9
#include<iostream>#include<cstdio>#include<algorithm>#include<vector>#include<set>#include<queue>#include<cstring>using namespace std;const int INF=1<<29;int n,m;int start,end;int _map[1000+10][1000+10];int cost[1000+10]; int len[1000+10];int daijia[1000+10];int v[1000+10]={0};queue<int> que;void dfs(int f,int index,int sum){for(int i=1;i<=n;i++){if(_map[index][i]!=INF){if(sum>=_map[index][i]){if(cost[i]>cost[f]+daijia[f]){cost[i]=cost[f]+daijia[f];if(v[i]==0){ v[i]=1,que.push(i);} dfs(f,i,sum-_map[index][i]);}}}}}int main(){cin>>n>>m>>start>>end;for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) _map[i][j]=INF;for(int i=0;i<m;i++){int a,b,c;scanf("%d%d%d",&a,&b,&c);_map[a][b]=_map[b][a]=c;}for(int i=1;i<=n;i++) cost[i]=INF;for(int i=1;i<=n;i++){scanf("%d%d",&len[i],&daijia[i]);}cost[start]=0;v[start]=1;que.push(start);while(que.size()){int index=que.front();que.pop();dfs(index,index,len[index]);}if(cost[end]==INF) printf("-1");elseprintf("%d",cost[end]);return 0;}
- 沐枫NOI 17. Volleyball
- Volleyball
- 沐枫NOI 10. Leaders
- 沐枫NOI 14. Football
- 沐枫NOI 28. Queue
- NOI
- codeforces 96D Volleyball
- Codeforces 95C-Volleyball
- 沐枫NOI 21. Restoring Password
- 沐枫NOI 25. Binary Number
- 沐枫NOI 30. African Crossword
- 沐枫NOI 2839. 单词替换
- Codeforces 96D Volleyball spfa
- CF95C volleyball [最短路+想法]
- Codeforces 95C Volleyball 题解
- 沐枫NOI 6. Help Chef Gerasim 模拟
- 沐枫NOI 7. Help Far Away Kingdom
- 沐枫NOI 13. Lucky Numbers (easy)
- 各种数学和几何所用的代码即相关定理
- 5.3
- Spring 中的 Environment
- 时钟
- 去除两个 inline-block 之间的间距
- 沐枫NOI 17. Volleyball
- 计算两个时间戳之间相差的日时分秒
- 1N4148、1N4007、1N5819
- 大数据1-淘宝MapReduce作业特性分析(转)
- 反击爬虫,前端工程师的脑洞可以有多大?
- 在Linux上安装QNX Momentics IDE 4.7.0
- SQL语句
- 获取客户端IP地址
- 兔子产仔