题解

我的歪解

我首先想的是分治,我想二分肯定不行,因为它是没有单调性的。

我想了一下感觉它的大部分数据应该是有凸性的(例如\(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) 最短路+贪心的更多相关文章

  1. 洛谷 AT2434 JOI 公園 (JOI Park) 题解

    人生第一次AC黑题,我太感动了. 每日一题 day31 打卡 Analysis 先跑遍DJ,求出1到 i的最短路.得到每个点到 1号点的距离后,从小到大排序一遍,这时便可以枚举每个点到 1号点的距离修 ...

  2. 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 ...

  3. HDU2363 最短路+贪心

    Cycling Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  4. Codeforces 1076D Edge Deletion 【最短路+贪心】

    <题目链接> 题目大意: n个点,m条边的无向图,现在需要删除一些边,使得剩下的边数不能超过K条.1点为起点,如果1到 i 点的最短距离与删除边之前的最短距离相同,则称 i 为 " ...

  5. 【CF1076D】Edge Deletion 最短路+贪心

    题目大意:给定 N 个点 M 条边的无向简单联通图,留下最多 K 条边,求剩下的点里面从 1 号顶点到其余各点最短路大小等于原先最短路大小的点最多怎么构造. 题解:我们可以在第一次跑 dij 时直接采 ...

  6. Codeforces 545E. Paths and Trees[最短路+贪心]

    [题目大意] 题目将从某点出发的所有最短路方案中,选择边权和最小的最短路方案,称为最短生成树. 题目要求一颗最短生成树,输出总边权和与选取边的编号.[题意分析] 比如下面的数据: 5 5 1 2 2 ...

  7. Forethought Future Cup - Elimination Round D 贡献 + 推公式 + 最短路 + 贪心

    https://codeforces.com/contest/1146/problem/D 题意 有一只青蛙,一开始在0位置上,每次可以向前跳a,或者向后跳b,定义\(f(x)\)为青蛙在不跳出区间[ ...

  8. Codeforces Round #303 (Div. 2)(CF545) E Paths and Trees(最短路+贪心)

    题意 求一个生成树,使得任意点到源点的最短路等于原图中的最短路.再让这个生成树边权和最小. http://codeforces.com/contest/545/problem/E 思路 先Dijkst ...

  9. [CSP-S模拟测试]:任务分配(最短路+贪心+DP)

    题目传送门(内部题149) 输入格式 每个测试点第一行为四个正整数$n,b,s,m$,含义如题目所述. 接下来$m$行,每行三个非负整数$u,v,l$,表示从点$u$到点$v$有一条权值为$l$的有向 ...

随机推荐

  1. EasyGui

    EasyGui 在IDLE上运行EasyGui可能存在冲突 EasyGui是运行在Tkinter上并哟拥有自身的事件循环,而IDLE也是Tkinter写的一个应用程序并页拥有自身的事件循环.两者同时运 ...

  2. bash&nbsp;shell笔记7&nbsp;创建函数

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://twentyfour.blog.51cto.com/945260/525126 知 ...

  3. nand中间出现坏块,无法正常启动内…

    我板子的启动过程如下: ..showlogo.. Flash:   1 MB NAND:    SLC detected.256 MB In:      serial Out:     serial ...

  4. Xamarin官方示例代码无法部署,提示已跳过部署解决方法

    最近利用Visual Studio 2017学习Android开发.主要是通过Xamarin官方的文档进行的.官方的入门指导提供了很多的示例代码.但是下载之后,调试运行的时候,总是无法部署到虚拟机上. ...

  5. node.js中模块报错【window is not defined】的解决方法

    (function(window) { /* Keep source code the same */ // })(typeof window == "undefined" ? g ...

  6. 【bzoj1056】排名系统

    1056: [HAOI2008]排名系统 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2195  Solved: 623[Submit][Statu ...

  7. msql 计算连续签到天数

    刚刚写了一个签到计算天数的sql, 记录下来. 思路如下: 获取当前签到的最后时间(今天或昨天), 定义一个变量@i 对签到时间进行天数自减, 然后查询出当前记录签到时间是否与自减后的时间匹配.   ...

  8. 7. Reverse Integer 反转整数

    [抄题]: 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数).   样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 [暴力解法]: ...

  9. 690. Employee Importance员工权限重要性

    [抄题]: You are given a data structure of employee information, which includes the employee's unique i ...

  10. 简单Factory模式

    #pragma once #include "student.h" #include "Teacher.h" typedef enum _EPersonType ...