题目链接:http://codeforces.com/problemset/problem/95/C

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 uiviwi (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
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
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个点,你需要从x点到y点。如果点a到点b的路径小于ta ,那么从a到b的费用就为ca。求最小费用

题解:

  由于只有1000个点,所以Dij 跑n个点 ,时间复杂为 n*E*logE。你就可以得到dis[i][j](点 i 到点 j 的距离)

  我们建第二幅图。如果dis[i][j] < t[i] 。 这样的话从 i 到 j 花费 ci 就可以到达。

  在跑一边Dij 求最小费用。

 

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define eps 0.0000000001
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
const int maxn = +;
struct qnode {
int v;
LL c;
qnode(int _v=,int _c=):v(_v),c(_c) {}
bool operator <(const qnode &r)const {
return c>r.c;
}
};
struct Edge {
int v;
LL cost;
Edge(int _v=,int _cost=):v(_v),cost(_cost) {}
};
vector<Edge>E[maxn];
bool vis[maxn];
LL dist[maxn];
void Dijkstra(int n,int start) { //点的编号从1开始
memset(vis,false,sizeof(vis));
for(int i=; i<=n; i++)dist[i]=INF;
priority_queue<qnode>que;
while(!que.empty())que.pop();
dist[start]=;
que.push(qnode(start,));
qnode tmp;
while(!que.empty()) {
tmp=que.top();
que.pop();
int u=tmp.v;
if(vis[u])continue;
vis[u]=true;
for(int i=; i<E[u].size(); i++) {
int v=E[tmp.v][i].v;
LL cost=E[u][i].cost;
if(!vis[v]&&dist[v]>dist[u]+cost) {
dist[v]=dist[u]+cost;
que.push(qnode(v,dist[v]));
}
}
} }
void addedge(int u,int v,LL w) {
E[u].push_back(Edge(v,w));
}
LL t[maxn], c[maxn];
LL dis[maxn][maxn];
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio();
cin.tie();
int n, m, x, y, u, v;
LL w;
scanf("%d%d%d%d", &n, &m, &x, &y);
for(int i = ; i<m; i++) {
scanf("%d%d%lld", &u, &v, &w);
addedge(u, v, w);
addedge(v, u, w);
}
for(int i = ;i<=n;i++) scanf("%lld%lld", &t[i], &c[i]);
for(int i = ;i<=n;i++){
Dijkstra(n, i);
for(int j = ;j<=n;j++)
dis[i][j] = dist[j];
}
for(int i = ;i<=n;i++) E[i].clear();
for(int i = ;i<=n;i++){
for(int j = ;j<=n;j++){
if(i!=j&&dis[i][j] <= t[i]){
addedge(i, j, c[i]);
}
}
}
Dijkstra(n, x);
if(dist[y]!=INF)
printf("%lld\n", dist[y]);
else printf("-1\n");
return ;
}

Codeforces 95C Volleyball(最短路)的更多相关文章

  1. Codeforces Beta Round #77 (Div. 1 Only) C. Volleyball (最短路)

    题目链接:http://codeforces.com/contest/95/problem/C 思路:首先dijkstra预处理出每个顶点到其他顶点的最短距离,然后如果该出租车到某个顶点的距离小于等于 ...

  2. 【codeforces 95C】Volleyball

    [题目链接]:http://codeforces.com/problemset/problem/95/C [题意] 给你n个点,m条边; 每个点有一辆出租车; 可以到达离这个点距离不超过u的点,且在这 ...

  3. codeforces DIV2 D 最短路

    http://codeforces.com/contest/716/problem/D 题目大意:给你一些边,有权值,权值为0的表示目前该边不存在,但是可以把0修改成另外一个权值.现在,我们重新建路, ...

  4. Codeforces 96D Volleyball(最短路径)

    Petya loves volleyball very much. One day he was running late for a volleyball match. Petya hasn't b ...

  5. Dynamic Shortest Path CodeForces - 843D (动态最短路)

    大意: n结点有向有权图, m个操作, 增加若干边的权重或询问源点为1的单源最短路. 本题一个特殊点在于每次只增加边权, 并且边权增加值很小, 询问量也很小. 我们可以用johnson的思想, 转化为 ...

  6. CodeForces 25C(Floyed 最短路)

    F - Roads in Berland Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I6 ...

  7. World Tour CodeForces - 667D (bfs最短路)

    大意: 有向图, 求找4个不同的点ABCD, 使得d(A,B)+d(D,C)+d(C,A)最大

  8. Bakery CodeForces - 707B (最短路的思路题)

    Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. The ...

  9. CodeForces 689B【最短路】

    题意: 给你一副图,给出的点两两之间的距离是abs(pos1-pos2),然后给你n个数是表示该pos到x的距离是1. 思路: 直接建边,跑spfa就好了.虽然说似乎题意说边很多,其实只要建一下相邻的 ...

随机推荐

  1. php 操作Redis发送短信

    循环查询redis队列里面的数据 然后提交数据后将反馈信息再写入另一个 redis list里面 代码 <?php /** * System Name: sent message * User: ...

  2. http请求响应丢包问题

    在与合作方联调某个明细数据接口的时候发现 1.当请求条数为4,content-length<1500时,数据可以正确返回. 2.当请求条数为5,content-length>1500时,无 ...

  3. spring boot 枚举使用的坑

    java 枚举的功能挺多,但是坑更多,使用的时候要注意.如下面这个枚举. @Getter @AllArgsConstructor public enum EnumExpenseType impleme ...

  4. Linux基础命令四

    iptables iptables -F:关闭防火墙 crontab -l查看定时任务 crontab -e :编辑定时任务 log日志相关: ls  /var/log:查看日志 du -sh  /v ...

  5. XPath语法以及谓语的结合使用

    /* XPath 术语 节点(Node) 在 XPath 中,有七种类型的节点:元素.属性.文本.命名空间.处理指令.注释以及文档(根)节点.XML 文档是被作为节点树来对待的.树的根被称为文档节点或 ...

  6. AFNetworking2.0源码解析<二>

    本篇我们继续来看看AFNetworking的下一个模块 — AFURLRequestSerialization.   AFURLRequestSerialization用于帮助构建NSURLReque ...

  7. 继续死磕python

    一.数据运算 算术运算 比较运算 赋值运算 逻辑运算 成员运算 身份运算 位运算 其中左右移运算是逻辑左右移即缺失位补0,而算数右移缺失补符号位(注意逻辑运算都是补码运算即都取补码再运算,然后结果也是 ...

  8. 解密Qt安装目录的结构

    http://c.biancheng.net/view/3866.html 了解 Qt 安装目录的结构虽然不是编程必须的,但是它能练就我们的内功,让我们对 Qt 的编程环境了如指掌.Windows 和 ...

  9. linux c下的c文件 h文件 o文件 so文件 a文件 可执行文件 gcc使用

    linux下c语言工程: c文件:主要每个模块的原代码都在c文件中. h文件:每个c文件都跟着一个h文件,h文件的作用是放着c文件中函数的声明,结构体的定义,宏的定义等. o文件:目标文件.每个文件经 ...

  10. Python字符串(str)方法调用

    # str# n = 'pianYU'# v = n.capitalize() # 将字符串的首字母大写# print(v)## n = 'pianYI'# v1 = n.isupper() # 判断 ...