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就好了.虽然说似乎题意说边很多,其实只要建一下相邻的 ...
随机推荐
- SparkStreaming DStream转换
1.无状态转换操作 (1)无状态转化操作就是把简单的RDD转化操作应用到每个批次上,也就是转换DStream中的每一个RDD. 部分无状态转化操作: (2)尽管这些函数韩起来像作用在整个流上一样,但事 ...
- catch that cow POJ 3278 搜索
catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...
- OneinStack定时同步备份数据库/网站至七牛云存储方法
无论我们用WEB面板,还是用一键脚本安装环境建站,只要一旦我们开始用VPS.服务器,最为关键的就是服务器中的数据.因为大部分VPS.服务器商家都是无管理型主机,任何的安装和维护都需要我们自行管理.即便 ...
- thinkphp5发送邮件(实例代码 非常适合新手)
第一步:在(https://pan.baidu.com/s/1Fq6lONHlft5D6jvOnNwtoA)下载 phpmailer.rar 解压 然后把文件放入 vendor目录下 第二步:在 ap ...
- 虚拟机的linux(CentOS)系统安装
一.安装虚拟机:VirtualBox 1.软件下载:https://www.virtualbox.org/ 2.一路点击下一步完成安装 二.安装linux:CentOS6.8 1.官方网站:ht ...
- PHP获取当前服务器详细信息
最近正在用PHP写一个企业级的CMS,后台需要用到PHP获取当前服务器的详细信息以及相关系统参数信息,整理了整理,现在贴这儿,以备后用. 获取系统类型及版本号: php_uname() (例:W ...
- css渐变色兼容性写法
background: -webkit-linear-gradient(left, #0f0f0f, #0c0c0c, #272727); /* Safari 5.1 - 6.0 */ backgro ...
- Markov Chain Monte Carlo Simulation using C# and MathNet
Math.Net Numerics has capability to conduct Markov Chair Monte Carlo simulations, yet the document i ...
- MongoDB的使用学习之(四)权限设置--用户名、密码、端口==
本文参照:http://hi.baidu.com/tianhuimin/item/590d96cfd7ac1509c610b26a 本人也是按照此文章操作的,但是有些不妥,红色文字就是我实践后,需要改 ...
- ubuntu下docker安装
首先来一个官网安装教程链接:https://docs.docker.com/install/linux/docker-ce/ubuntu/ 目前docker主要有docker-CE 与 docker- ...