Codeforces 95C Volleyball(最短路)
题目链接:http://codeforces.com/problemset/problem/95/C
2 seconds
256 megabytes
standard input
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.
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.
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.
4 4
1 3
1 2 3
1 4 1
2 4 1
2 3 5
2 7
7 2
1 2
7 7
9
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(最短路)的更多相关文章
- Codeforces Beta Round #77 (Div. 1 Only) C. Volleyball (最短路)
题目链接:http://codeforces.com/contest/95/problem/C 思路:首先dijkstra预处理出每个顶点到其他顶点的最短距离,然后如果该出租车到某个顶点的距离小于等于 ...
- 【codeforces 95C】Volleyball
[题目链接]:http://codeforces.com/problemset/problem/95/C [题意] 给你n个点,m条边; 每个点有一辆出租车; 可以到达离这个点距离不超过u的点,且在这 ...
- codeforces DIV2 D 最短路
http://codeforces.com/contest/716/problem/D 题目大意:给你一些边,有权值,权值为0的表示目前该边不存在,但是可以把0修改成另外一个权值.现在,我们重新建路, ...
- Codeforces 96D Volleyball(最短路径)
Petya loves volleyball very much. One day he was running late for a volleyball match. Petya hasn't b ...
- Dynamic Shortest Path CodeForces - 843D (动态最短路)
大意: n结点有向有权图, m个操作, 增加若干边的权重或询问源点为1的单源最短路. 本题一个特殊点在于每次只增加边权, 并且边权增加值很小, 询问量也很小. 我们可以用johnson的思想, 转化为 ...
- CodeForces 25C(Floyed 最短路)
F - Roads in Berland Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...
- World Tour CodeForces - 667D (bfs最短路)
大意: 有向图, 求找4个不同的点ABCD, 使得d(A,B)+d(D,C)+d(C,A)最大
- 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 ...
- CodeForces 689B【最短路】
题意: 给你一副图,给出的点两两之间的距离是abs(pos1-pos2),然后给你n个数是表示该pos到x的距离是1. 思路: 直接建边,跑spfa就好了.虽然说似乎题意说边很多,其实只要建一下相邻的 ...
随机推荐
- mysql中的范式
范式 范式:Normal Format,是一种离散数学中的知识,是为了解决数据的存储与优化的问题:保存数据的存储之后,凡是能够通过关系寻找出来的数据,坚决不再重复存储,终极目标是为了减少数据的冗余.范 ...
- Tensorflow模型保存与加载
在使用Tensorflow时,我们经常要将以训练好的模型保存到本地或者使用别人已训练好的模型,因此,作此笔记记录下来. TensorFlow通过tf.train.Saver类实现神经网络模型的保存和提 ...
- javascript中的继承-寄生组合式继承
前文说过,组合继承是javascript最常用的继承模式,不过,它也有自己的不足:组合继承无论在什么情况下,都会调用两次父类构造函数,一次是在创建子类原型的时候,另一次是在子类构造函数内部.子类最终会 ...
- vue组件化编程应用2
写几个小案例来理解vue的组件化编程思想,下面是一个demo. 效果图示: 需求: header部输入任务,进行添加,在list中显示; list中每个item项,鼠标移入时,背景变灰并显示delet ...
- 介绍一下 except 的作用和用法?
except: #捕获所有异常 except: <异常名>: #捕获指定异常 except:<异常名 1, 异常名 2> : 捕获异常 1 或者异常 2 except:< ...
- 分布式之redis(转发)
为什么写这篇文章? 博主的<分布式之消息队列复习精讲>得到了大家的好评,内心诚惶诚恐,想着再出一篇关于复习精讲的文章.但是还是要说明一下,复习精讲的文章偏面试准备,真正在开发过程中,还是脚 ...
- 使用SQL语法来查询Elasticsearch:Elasticsearch-SQL插件
简介 Elasticsearch-SQL是Elasticsearch的一个插件,它可以让我们通过类似SQL的方式对Elasticsearch中的数据进行查询.项目地址是:https://github. ...
- 华为云搭建windows+wordpress+xampp
1.如何将本地文件上传至华为云ECS云服务器(Windows系统) 1.1 在本地电脑上,快捷键“WIN+R"打开“运行”中输入“mstsc”,点击确定 1.2 在“远程桌面连接”框点击“ ...
- 深入理解React组件传值(组合和继承)
在文章之前,先把这句话读三遍 Props 和组合为你提供了清晰而安全地定制组件外观和行为的灵活方式.注意:组件可以接受任意 props,包括基本数据类型,React 元素以及函数. 来源于React中 ...
- jQuery进阶第三天(2019 10.12)
一.原生JS快捷的尺寸(属性)(注意这些属性的结果 不带PX单位) clientWidth/clientHeight =====> 获得元素content+padding的宽/高: offse ...