【Educational Codeforces Round 38 (Rated for Div. 2)】 Problem A-D 题解
【比赛链接】
【题解】
Problem A Word Correction【字符串】
不用多说了吧,字符串的基本操作
Problem B Run for your prize【贪心】
我们可以将这个数轴一分为二,小于等于500000的由第一个人领,否则由第二个人领
Problem C Constructing tests【贪心】【数学】
首先我们发现 : N^2 - (N / M)^2 = x (N/M向下取整)
然后我们算出N的上下界,发现: sqrt(x+1)<=N<=sqrt(3/4*x) (sqrt是开根号的意思)
所以我们可以枚举N,算出M
Problem D Buy a ticket【最短路】
首先想到可以跑N遍最短路
但是很显然,这样会超时,那么我们该如何优化呢?
我们不妨先建一个原点,将原点与每个城市连边,权值为在这个城市开演唱会的价格ai,然后再将城市与城市之间连边,
但是每条路径上的权值要乘2,因为是往返的费用
我们发现,只要对这个图跑一遍最短路,就得出了答案。
注意N最大10^5,SPFA不能过,要用dijkstra+堆优化
代码 :
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAXN = * 1e5; LL i,N,M;
LL dist[MAXN+],a[MAXN+],b[MAXN+],c[MAXN+],w[MAXN+],vis[MAXN+];
vector< pair<LL,LL> > E[MAXN+]; template <typename T> inline void read(T &x) {
LL f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = x * + c - '';
x *= f;
} template <typename T> inline void write(T x) {
if (x < ) { putchar('-'); x = -x; }
if (x > ) write(x/);
putchar(x%+'');
} template <typename T> inline void writeln(T x) {
write(x);
puts("");
} inline void dijkstra(LL st) {
LL i,x,to,cost;
priority_queue< pair<LL,LL> > q;
for (i = ; i <= N; i++) dist[i] = LONG_LONG_MAX;
q.push(make_pair(,st));
dist[st] = ;
memset(vis,,sizeof(vis));
while (!q.empty()) {
x = q.top().second; q.pop();
if (vis[x]) continue;
vis[x] = ;
for (i = ; i < E[x].size(); i++) {
to = E[x][i].first;
cost = E[x][i].second;
if (dist[x] + cost < dist[to]) {
dist[to] = dist[x] + cost;
q.push(make_pair(-dist[to],to));
}
}
}
} int main() { read(N); read(M);
for (i = ; i <= M; i++) {
read(a[i]); read(b[i]); read(w[i]);
E[a[i]].push_back(make_pair(b[i],w[i]*));
E[b[i]].push_back(make_pair(a[i],w[i]*));
}
for (i = ; i <= N; i++) {
read(c[i]);
E[N+].push_back(make_pair(i,c[i]));
}
dijkstra(N+);
for (i = ; i <= N; i++) {
if (i == ) write(dist[i]);
else { putchar(' '); write(dist[i]); }
}
puts(""); return ;
}
【Educational Codeforces Round 38 (Rated for Div. 2)】 Problem A-D 题解的更多相关文章
- Educational Codeforces Round 38 (Rated for Div. 2) C
C. Constructing Tests time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Educational Codeforces Round 38 (Rated for Div. 2)
这场打了小号 A. Word Correction time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Educational Codeforces Round 76 (Rated for Div. 2)E(dp||贪心||题解写法)
题:https://codeforces.com/contest/1257/problem/E 题意:给定3个数组,可行操作:每个数都可以跳到另外俩个数组中去,实行多步操作后使三个数组拼接起来形成升序 ...
- Educational Codeforces Round 92 (Rated for Div. 2) B、C题解
TAT 第一场codeforces B. Array Walk #暴力 #贪心 题目链接 题意 有\(a1, a2, ..., an\) 个格子(每个格子有各自分数),最初为1号格(初始分数为\(a1 ...
- Educational Codeforces Round 116 (Rated for Div. 2), problem: (C) Banknotes
传送门 Problem - C - Codeforces 题目 题目重点内容手打翻译:(欢迎批评指正) 在柏林, 使用着n套不同的货币(banknotes).第i个货币面额为10ai 元,货币的第一种 ...
- Educational Codeforces Round 96 (Rated for Div. 2) E. String Reversal 题解(思维+逆序对)
题目链接 题目大意 给你一个长度为n的字符串,可以交换相邻两个元素,使得这个字符串翻转,求最少多少种次数改变 题目思路 如果要求数组排序所需要的冒泡次数,那其实就是逆序对 这个也差不多,但是如果是相同 ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
随机推荐
- Educational Codeforces Round 51 (Rated for Div. 2) The Shortest Statement
题目链接:The Shortest Statement 今天又在群里看到一个同学问$n$个$n$条边,怎么查询两点直接最短路.看来这种题还挺常见的. 为什么最终答案要从42个点的最短路(到$x,y$) ...
- python学习之-requests模块基础
安装版本:2.18 模块导入:import requests l 发送请求 发送GET请求: 获取GITHUB的公共时间线 r = requests.get(url='https://api.git ...
- T3138 栈练习2 codevs
http://codevs.cn/problem/3138/ 题目描述 Description 给定一个栈(初始为空,元素类型为整数,且小于等于100),只有两个操作:入栈和出栈.先给出这些操作,请输 ...
- Liunx 下Redis 的安装
一.Redis 的简介 Redis是一款开源的.高性能的键-值存储.它常被称作是一款数据结构服务器,它是一个key-value存储系统.和Memcache类似,Memecache只支持字符窜的数据类型 ...
- ssh 卡主
偶尔会遇到这样的现象 ssh 登录一台远程机器,显示下面的信息然后hang在那 Connecting to 192.168.137.102:22... Connection established. ...
- 制作ubuntu U盘安装盘
sudo dd if=ubuntu.iso of=/dev/sdb2 sudo syslinux /dev/sdb1
- 转:libev和libevent的设计差异
转:http://www.cnblogs.com/Lifehacker/p/whats_the_difference_between_libevent_and_libev_chinese.html [ ...
- 如何推断一个P2P平台是否靠谱?
推断一个站点,是否靠谱.是有规律可循的.P2P平台算是个新兴的电商类站点. 网上欺诈类的站点.不限于P2P,实在是太多了,真的有必要总结下最关键的几个靠谱指标. 最关键的2个 1.创始人和 ...
- HDU 2317 Nasty Hacks
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- html5 式程序员表白
html5 式程序员表白 王海庆 于 星期三, 04/06/2014 - 00:44 提交 今天是个好日子,2014年5月20日,表白的最佳时机,虽说孩子已经四岁.结婚已经五年.可是也不能够偷懒.于是 ...