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

思路:首先dijkstra预处理出每个顶点到其他顶点的最短距离,然后如果该出租车到某个顶点的距离小于等于最短距离,就连边,费用为一开始出租车所需的费用,建好图之后再求一次最短路即可。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std; const int MAX_N = (1000 + 100);
const long long inf = 1LL << 60;
int N, M, st, ed, vis[MAX_N][MAX_N];
long long dist[MAX_N][MAX_N], cost[MAX_N];
/*
struct cmp {
bool operator () (const pair<long long, int> &p, const pair<long long, int> &q) const {
return p.first > q.first;
}
};*/
vector<int > from[MAX_N];
vector<pair<int, int> > g[MAX_N]; void Dijkstra(int s)
{
priority_queue<pair<long long, int>, vector<pair<long long, int> >, less<pair<long long, int> > > pq;
pq.push(make_pair(-(dist[s][s] = 0), s));
while (!pq.empty()) {
pair<long long, int > p = pq.top();
pq.pop();
if (vis[s][p.second]) continue;
vis[s][p.second] = 1;
for (vector<pair<int, int > >::const_iterator it = g[p.second].begin(); it != g[p.second].end(); ++it) {
if (-(p.first) + it->second < dist[s][it->first]) {
dist[s][it->first] = -(p.first) + it->second;
if (!vis[s][it->first]) {
pq.push(make_pair(-(dist[s][it->first]), it->first));
}
}
}
} } long long dp[MAX_N];
int mark[MAX_N];
long long getDp()
{
FOR(i, 1, N) dp[i] = inf, mark[i] = 0;
dp[st] = 0;
queue<int > que;
que.push(st);
while (!que.empty()) {
int u = que.front();
que.pop();
mark[u] = 0;
REP(i, 0, (int)from[u].size()) {
int v = from[u][i];
if (dp[u] + cost[u] < dp[v]) {
dp[v] = dp[u] + cost[u];
if (!mark[v]) { mark[v] = 1; que.push(v); }
}
}
}
if (dp[ed] >= inf) return -1;
return dp[ed];
} int main()
{
cin >> N >> M >> st >> ed;
FOR(i, 1, M) {
int u, v, w; cin >> u >> v >> w;
g[u].push_back(make_pair(v, w));
g[v].push_back(make_pair(u, w));
}
FOR(i, 1, N) {
FOR(j, i, N) dist[j][i] = dist[i][j] = inf, vis[j][i] = vis[i][j] = 0;
}
FOR(i, 1, N) Dijkstra(i);
FOR(i, 1, N) {
int t, c; cin >> t >> cost[i];
FOR(j, 1, N) if (j != i && dist[i][j] <= t) {
from[i].push_back(j);
}
}
cout << getDp() << endl;
return 0;
}

Codeforces Beta Round #77 (Div. 1 Only) C. Volleyball (最短路)的更多相关文章

  1. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  2. codeforces水题100道 第二十三题 Codeforces Beta Round #77 (Div. 2 Only) A. Football (strings)

    题目链接:http://www.codeforces.com/problemset/problem/96/A题意:判断一个0-1字符串中出现的最长的0字串或者1字串的长度是否大于等于7.C++代码: ...

  3. Codeforces Beta Round #77 (Div. 2 Only) A. Football【字符串/判断是否存在连续7个0或7个1】

    A. Football time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  4. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  5. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  6. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  7. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

  8. Codeforces Beta Round #75 (Div. 2 Only)

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...

  9. Codeforces Beta Round #74 (Div. 2 Only)

    Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...

随机推荐

  1. 金额大小写转换和input失去焦点触发事件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. Effective C++ -----条款21:必须返回对象时,别妄想返回其reference

    绝不要返回pointer或reference指向一个local stack对象,或返回reference指向一个heap-allocated对象,或返回pointer或reference指向一个loc ...

  3. 【leetcode】Binary Tree Postorder Traversal (hard) ☆

    二叉树的后序遍历 用标记右子树vector的方法 vector<int> postorderTraversal(TreeNode *root) { vector<int> an ...

  4. 【leetcode】Word Search II(hard)★

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  5. 如何把一个excel工作薄中N个工作表复制到另一个工作薄中

    一般遇到标题这样的情况,许多人可能会一个一个的复制粘贴,其实完全不必那么麻烦. 你可以按以下步骤来操作: 第一步:打开所有要操作的excel工作薄\n 第二步:按住Shift键,选择所有要复制的工作表 ...

  6. mongodb3.2配置文件yaml格式 详解

    mongodb3.x版本后就是要yaml语法格式的配置文件,下面是yaml配置文件格式如下:官方yaml配置文件选项参考:https://docs.mongodb.org/manual/ ... #c ...

  7. Javascript 封装方法

    基本封装方法 请看下面的例子: var Person = function(name,age){ this.name = name; this.age = age || "未填写" ...

  8. NYOJ题目124中位数

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAssAAAJUCAIAAABsWvwaAAAgAElEQVR4nO3dPXLjuraG4TsJ5xqIYw

  9. NYOJ题目768移位密码

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAtIAAAJqCAIAAACJkTDlAAAgAElEQVR4nO3du3Ljvpa34b4J574Qx7

  10. Linux系统入门学习:在curl中设置自定义的HTTP头

    http://www.linuxidc.com/Linux/2015-02/114220.htm