题目链接: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. Kubernetes服务部署解决方案

    学习了K8S的基础知识,我们的目的就是解决我们服务的迁移,那么接下去通过几个案例来感受一下K8s部署带来的便捷与效率. 环境准备: 3个节点,然后我这边也安装了 Ingress. 部署wordpres ...

  2. Java8---函数式编程-示例

    // Java8函数式编程示例—(Predicate.Stream.Optional) https://blog.csdn.net/weixin_41950473/article/details/84 ...

  3. MySQL-快速入门(10)触发器

    1.什么是触发器 触发器是与表有关的命名数据库对象.触发器由事件来触发某个操作. 触发器是特殊的存储过程,触发器不需要call语句来调用,也不需要手工启动,只需要确定一个预定义的事件发生的时候,就会被 ...

  4. 一个简单的Vue.js组件开发示例

    //创建属于自己的vue组件库 (function(Vue, undefined) { Vue.component("my-component", { template: '< ...

  5. laravel5.5结合bootstrap上传插件fileinput 上传图片

    引入相关js <script src="{{ asset('bootstrap-fileinput/js/fileinput.js') }}"></script& ...

  6. 通过编写串口助手工具学习MFC过程——(四)添加ComboBox组合框

    通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...

  7. 攻防世界--when_did_you_born5

    测试文件:https://adworld.xctf.org.cn/media/task/attachments/24937e95ca4744818feebe82ab96902d 1.准备 root@l ...

  8. mybatis报错(三)报错Result Maps collection does not contain value for java.lang.Integer解决方法

    转自:https://blog.csdn.net/zengdeqing2012/article/details/50978682 1 [WARN ] 2016-03-25 13:03:23,955 - ...

  9. linux下iptables讲解

    iptables(netfilter网络过滤器) iptables是linux上特有的防火墙机制,功能非常强大.CentOS默认是没有iptables规则. iptables命令可用于配置Linux的 ...

  10. PAT Advanced 1035 Password (20 分)

    To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem ...