【AT2434】JOI 公園 (JOI Park) 最短路+贪心
题解
我的歪解
我首先想的是分治,我想二分肯定不行,因为它是没有单调性的。
我想了一下感觉它的大部分数据应该是有凸性的(例如\(y=x^2\)的函数图像),所以可以三分。
下面是我的三分代码(骗了不少分)
三分模板没过的我居然瞎歪歪了一个三分
歪解code:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<queue>
#define ll long long
#define R register
#define N 400005
#define INF 0x7fffffffffffLL
using namespace std;
template<typename T>inline void read(T &a){
char c=getchar();T x=0,f=1;
while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
a=f*x;
}
ll n,m,c,tot,h[N],vis[N],pd[N];
ll dist[N],sum,now_ans,now;
struct bian{
int u,v;
ll w;
}b[N];
struct node{
int nex,to;
ll dis;
}edge[N<<1];
inline void add(R int u,R int v,R ll w){
edge[++tot].nex=h[u];
edge[tot].to=v;
edge[tot].dis=w;
h[u]=tot;
}
inline void spfa(R int s){
for(R int i=1;i<=n;i++)dist[i]=INF;
queue<int> q;q.push(s);dist[s]=0;vis[s]=1;
while(!q.empty()){
R int x=q.front();q.pop();vis[x]=0;
for(R int i=h[x];i;i=edge[i].nex){
R int xx=edge[i].to;
if(dist[xx]>dist[x]+edge[i].dis){
dist[xx]=dist[x]+edge[i].dis;
if(!vis[xx]){
vis[xx]=1;
q.push(xx);
}
}
}
}
}
inline ll check(R ll mid){
ll tot=0;
for(R int i=1;i<=n;i++)pd[i]=0;
for(R int i=1;i<=n;i++)
if(dist[i]<=mid)pd[i]=1;
for(R int i=1;i<=m;i++)
if(pd[b[i].u]&&pd[b[i].v])
tot+=b[i].w;
return tot-mid*c;//这是你能节省的
}
int main(){
read(n);read(m);read(c);
for(R int i=1;i<=m;i++){
read(b[i].u);read(b[i].v);read(b[i].w);
add(b[i].u,b[i].v,b[i].w);add(b[i].v,b[i].u,b[i].w);sum+=b[i].w;
}
spfa(1);
R ll l=0,r=sum;
while(l<=r){
R ll tmp=(r-l)/3;
R ll mid1=l+tmp;
R ll mid2=r-tmp;
if(check(mid1)>check(mid2)) r=mid2-1;
else l=mid1+1;
}
ll tmp=check(l),temp=check(r);
if(tmp>temp)now=l,now_ans=tmp;
else now=r,now_ans=temp;
printf("%lld\n",sum-now_ans);
return 0;
}
当然了,三分本来就是一个非常好的骗分算法(也会是正解),有些题在加一些暴力,一定会有神奇的效果;
讲课老师说加上暴力这道题应该可以\(A\)掉,但懒惰的我并没有去实践,有兴趣的可以试一试;
正解
这其实是一道经典的最短路的一种题型。
先跑一遍\(SPFA\),处理出\(dist\)数组;
然后再利用\(dist\)数组处理出每一条边的\(maxdis\);
将\(maxdis\)数组从小到大排序(结构体排序);

看完图应该都懂了吧。
code:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<queue>
#define ll long long
#define R register
#define N 800005
#define int long long
#define INF 9999999999999999LL
using namespace std;
template<typename T>inline void read(T &a){
char c=getchar();T x=0,f=1;
while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
a=f*x;
}
ll n,m,c,tot,h[N],vis[N],pd[N],maxdis[N];
ll dist[N],sum,ans,maxsum;
struct bian{
int u,v,w;
}b[N];
struct node{
int nex,to,dis;
}edge[N<<1];
struct MAX{
int maxdis,id;
friend bool operator < (const MAX &a,const MAX &b){
return a.maxdis<b.maxdis;
}
}md[N];
inline void add(R int u,R int v,R int w){
edge[++tot].nex=h[u];
edge[tot].to=v;
edge[tot].dis=w;
h[u]=tot;
}
inline void spfa(R int s){
for(R int i=1;i<=n;i++)dist[i]=INF;
queue<int> q;q.push(s);dist[s]=0;vis[s]=1;
while(!q.empty()){
R int x=q.front();q.pop();vis[x]=0;
for(R int i=h[x];i;i=edge[i].nex){
R int xx=edge[i].to;
if(dist[xx]>dist[x]+edge[i].dis){
dist[xx]=dist[x]+edge[i].dis;
if(!vis[xx]){
vis[xx]=1;
q.push(xx);
}
}
}
}
}
signed main(){
read(n);read(m);read(c);
for(R int i=1;i<=m;i++){
read(b[i].u);read(b[i].v);read(b[i].w);
add(b[i].u,b[i].v,b[i].w);add(b[i].v,b[i].u,b[i].w);sum+=b[i].w;
}
spfa(1);
for(R int i=1;i<=m;i++)
md[i].maxdis=max(dist[b[i].u],dist[b[i].v]),md[i].id=i;
sort(md+1,md+1+m);
ans=sum;
for(R int i=1;i<=m;i++){
sum-=b[md[i].id].w;
ans=min(ans,1LL*md[i].maxdis*c+sum);
}
printf("%lld\n",ans);
return 0;
}
【AT2434】JOI 公園 (JOI Park) 最短路+贪心的更多相关文章
- 洛谷 AT2434 JOI 公園 (JOI Park) 题解
人生第一次AC黑题,我太感动了. 每日一题 day31 打卡 Analysis 先跑遍DJ,求出1到 i的最短路.得到每个点到 1号点的距离后,从小到大排序一遍,这时便可以枚举每个点到 1号点的距离修 ...
- Codeforces Round #303 (Div. 2) E. Paths and Trees 最短路+贪心
题目链接: 题目 E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes inputs ...
- HDU2363 最短路+贪心
Cycling Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- Codeforces 1076D Edge Deletion 【最短路+贪心】
<题目链接> 题目大意: n个点,m条边的无向图,现在需要删除一些边,使得剩下的边数不能超过K条.1点为起点,如果1到 i 点的最短距离与删除边之前的最短距离相同,则称 i 为 " ...
- 【CF1076D】Edge Deletion 最短路+贪心
题目大意:给定 N 个点 M 条边的无向简单联通图,留下最多 K 条边,求剩下的点里面从 1 号顶点到其余各点最短路大小等于原先最短路大小的点最多怎么构造. 题解:我们可以在第一次跑 dij 时直接采 ...
- Codeforces 545E. Paths and Trees[最短路+贪心]
[题目大意] 题目将从某点出发的所有最短路方案中,选择边权和最小的最短路方案,称为最短生成树. 题目要求一颗最短生成树,输出总边权和与选取边的编号.[题意分析] 比如下面的数据: 5 5 1 2 2 ...
- Forethought Future Cup - Elimination Round D 贡献 + 推公式 + 最短路 + 贪心
https://codeforces.com/contest/1146/problem/D 题意 有一只青蛙,一开始在0位置上,每次可以向前跳a,或者向后跳b,定义\(f(x)\)为青蛙在不跳出区间[ ...
- Codeforces Round #303 (Div. 2)(CF545) E Paths and Trees(最短路+贪心)
题意 求一个生成树,使得任意点到源点的最短路等于原图中的最短路.再让这个生成树边权和最小. http://codeforces.com/contest/545/problem/E 思路 先Dijkst ...
- [CSP-S模拟测试]:任务分配(最短路+贪心+DP)
题目传送门(内部题149) 输入格式 每个测试点第一行为四个正整数$n,b,s,m$,含义如题目所述. 接下来$m$行,每行三个非负整数$u,v,l$,表示从点$u$到点$v$有一条权值为$l$的有向 ...
随机推荐
- 【知识结构】最强Web认证知识体系
花了些时间总结了下Web认证,以及各种方式的利弊和使用,后续后继续更新.文章转载请注明出处:https://www.cnblogs.com/pengdai/p/9144843.html -----20 ...
- (转)CSS布局-负边距-margin
css中的负边距(negative margin)是布局中的一个常用技巧,只要运用得合理常常会有意想不到的效果.很多特殊的css布局方法都依赖于负边距,所以掌握它的用法对于前端的同学来说,那是必须的. ...
- 图的第k短路
[问题描述] 给你一个有向图,求从1到n的第k短路. [解法] SPFA+A*搜索. 1 A*算法 A*算法在人工智能中是一种典型的启发式搜索算法,启发中的估价是用估价函数表示的: h(n)=f(n) ...
- libevent源码深度剖析二
libevent源码深度剖析二 ——Reactor模式 张亮 前面讲到,整个libevent本身就是一个Reactor,因此本节将专门对Reactor模式进行必要的介绍,并列出libevnet中的几个 ...
- 基于Nginx实现集群原理
1)安装Nginx 2)配置多个Tomcat,并修改端口号(两个端口号不一样即可) 3)在Nginx的Nginx.conf添加如下配置:
- spring aop自动代理注解配置之一
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 二项分布 , 多项分布, 以及与之对应的beta分布和狄利克雷分布
1. 二项分布与beta分布对应 2. 多项分布与狄利克雷分布对应 3. 二项分布是什么?n次bernuli试验服从 二项分布 二项分布是N次重复bernuli试验结果的分布. bernuli实验是什 ...
- 解决selenium与firefox版本不兼容问题
Python环境下类比 个人使用 32位环境 Python 2.7.12 Selenium 2.53.6 Firefox 47.01 安装selenium可用pip选择对应版本,参考另一教程. 因为在 ...
- 使用EasyUI,关于日期格式的文本框按照正常方式获取不到值的问题
这是个小菜在实际工作中遇到的问题,相信很多EasyUI新手很可能也遇到这样的问题,因此小菜觉得有必要拿出来分享一下. 这个问题要从EasyUI的datebox组件说起,小菜用这个组件的时候,发现用$( ...
- MySQL性能调优与架构设计——第6章 MySQL Server 性能的相关因素
第6章 MySQL Server 性能的相关因素 前言 大部分人都一致认为一个数据库应用系统(这里的数据库应用系统概指所有使用数据库的系统)的性能瓶颈最容易出现在数据的操作方面,而数据库应用系统的大部 ...