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> ...
随机推荐
- ASP.NET页面传值不使用QueryString
ASP.NET页面传值不使用QueryString Asp.net中的页面传值方法: 1 Url传值 特点:主要优点是实现起来非常简单,然而它的缺点是传递的值是会显示在浏览器的地址 ...
- ios xib 中的 size class
需要阅读UITraitCollection的说明文档,先截图如下: 今天说说xib中的size class的简单设置,先看图 一共有9个小块,水平方向代表width,垂直方向代表height. 对于w ...
- php优化
2015年12月14日 21:20:29 之前写过两篇文章: xdebug配置 xdebug trace 结果分析 第二篇里边有两个很耗时间和内存的线条: 第一个斜坡: 从mysql里读取数据后, 用 ...
- 15. javacript高级程序设计-Canvas绘图
1. Canvas绘图 HTML5的<canvas>元素提供了一组JavaScript API,让我们可以动态的创建图形和图像.图形是在一个特定的上下文中创建的,而上下文对象目前有两种. ...
- js简单分页,可用
//翻页调用 var pageSize = 1; var counts = 1; var current_page = 1; var rows,total; search(); //查询所有 func ...
- 【leetcode】Maximum Gap(hard)★
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 【python】f.write()写入中文出错解决办法
一个出错的例子 #coding:utf-8 s = u'中文' f = open("test.txt","w") f.write(s) f.close() 原因 ...
- 如何获取Iframe的页面控件的值
有时候我们在页面需要使用iframe的方法来引用另一个页面,虽然个人建议不要使用iframe哈,但是有些时候是没得法了才使用的咯,那么在使用的过程中也会遇到某一些问题,我将自己遇到的问题记录下来方便以 ...
- asp.net 防止按钮重复提交
1.将按钮属性设置如下: <asp:Button ID="btConfirm" runat="server" Text="Confirm&quo ...
- 中文和unicode互转
public class Test { public static void main(String[] args) { String uname="欧阳红"; for (int ...