Codeforces Beta Round #77 (Div. 1 Only) C. Volleyball (最短路)
题目链接: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 (最短路)的更多相关文章
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- 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++代码: ...
- 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 ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
- Codeforces Beta Round #75 (Div. 2 Only)
Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...
- Codeforces Beta Round #74 (Div. 2 Only)
Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...
随机推荐
- Python QRCODE
- strcpy vs memcpy
[本文连接] http://www.cnblogs.com/hellogiser/p/strcpy_vs_memcpy.html [分析] strcpy和memcpy都是标准C库函数,它们有下面的特点 ...
- C++ 获取vector容器最后一个元素
声明:vector<T> vec; 方法一: return vec.at(vec.size()-1); 方法二: return vec.back(); 方法三: return vec.e ...
- Unity全屏模糊
先上效果,左边模糊 其实用的是Unity Stard Effect里的资源,一个脚本一个shader //脚本代码 using UnityEngine; using System.Collection ...
- 初识Python(一)
Python简介 Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解 ...
- 数据结构-链表逆置(c++模板类实现)
链表结点类模板定义: template <class T> class SingleList; template <class T> class Node { private: ...
- js中apply方法的使用
js中apply方法的使用 1.对象的继承,一般的做法是复制:Object.extend prototype.js的实现方式是: Object.extend = function(destinat ...
- Android环境搭建中遇到的小问题
有一认识的同学做Android,结果他们搭建环境出现问题,最后卡在了一关,因为听说自己学过Java,所以就... 最后,自己试了一下,结果将遇到的解决问题记下来了:(看到小绿人后自己也被Android ...
- 【leetcode】Binary Tree Maximum Path Sum (medium)
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- 高校排名 加强版(codevs 2799)
题目描述 Description 大学排名现在已经非常流行.在网上搜索可查到关于中国大学排行的各个方面的消息. 我们知道,在一大学里通常都由许多不同的"系"(专业)组成,比如计算机 ...