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> ...
随机推荐
- POJ 2352
---恢复内容开始--- http://poj.org/problem?id=2352 这是一个树状数组的题目,也是我第一次接触这类的题目,也正是因为之前的一道题,TLE了,超时太严重了,我看disc ...
- 如何限制一个类只在堆上分配和栈上分配(StackOnly HeapOnly)
[本文链接] http://www.cnblogs.com/hellogiser/p/stackonly-heaponly.html [题目] 如何限制一个类只在堆上分配和栈上分配? [代码] C+ ...
- JAVA thread0.interrupt()方法
interrupt()只是改变中断状态而已,interrupt()不会中断一个正在运行的线程.这一方法实际上完成的是,给受阻塞的线程抛出一个中断信号,这样受阻线程就得以退出阻塞的状态. 更确切的说,如 ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- SAP ALV OO 选择行打印
&---------------------------------------------------------------------* *& Report ZSDF001 * ...
- iOS9 UI Tests探索笔记
UI Tests是什么? UI Tests是一个自动测试UI与交互的Testing组件 UI Tests有什么用? 它可以通过编写代码.或者是记录开发者的操作过程并代码化,来实现自动点击某个按钮.视图 ...
- 【leetcode】Rotate List(middle)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- [Android Pro] AAR and JAR
svn status svn log --limit 3 > RELEASE_NOTE.txt cat RELEASE_NOTE.txt pwd project_name_prefix=&quo ...
- SQLServer视图
视图简介:通过定义 SELECT 语句以检索将在视图中显示的数据来创建视图.SELECT 语句引用的数据表称为视图的基表.在SQL Server 2005系统中,可以把视图分为3种类型,即标准视图,索 ...
- Android 在 manifest 文件里增加 versionCode,运行后版本并没有随之增加
现象:从 git 上拉下来的代码中 versionCode 是8,versionName 是1.0.7但运行后的版本仍然是1.0.6 原因:全文搜索1.0.6之后发现在 bin 目录下也有一个 man ...