bzoj 4152: [AMPPZ2014]The Captain
发布日期:2021-05-06 23:45:44 浏览次数:22 分类:精选文章

本文共 1283 字,大约阅读时间需要 4 分钟。

这题的话大概就是一个最短路。

一开始我想先按x排序,接着推过去,但是发现有点慢。。
因为我可能要扫很多地方
那我们不妨再把y排序,也推过去。。
但似乎问题没有解决
上面都是废话
最后我们发现一个点只与与他x,y坐标相邻的点可能有贡献。
于是就连边跑SPFA
但是似乎这题的人卡SPFA,所以要加一个堆优化
code:

#include
#include
#include
#include
#include
#define swap(x,y) {int tt=x;x=y;y=tt;}#define inf 0x3f3f3f3f using namespace std;const int N=200005;int n;struct qq{ int x,y,id;}s[N];bool cmp (qq a,qq b){ return a.x
,vector
>,greater
> > q;void SPFA (){ for (int u=1;u<=n;u++) f[u]=inf; memset(in,false,sizeof(in)); in[1]=true; q.push(make_pair(0,1)); f[1]=0; while (!q.empty()) { int x=q.top().second;q.pop(); for (int u=last[x];u!=-1;u=e[u].last) { int y=e[u].y; if (f[y]>f[x]+e[u].z) { f[y]=f[x]+e[u].z; if (in[y]==false) { in[y]=true; q.push(make_pair(f[y],y)); } } } in[x]=false; } printf("%d\n",f[n]);}int read(){ int x=0;char ch=getchar(); while (ch<'0'||ch>'9') ch=getchar(); while (ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();} return x;}int main(){ num=0;memset(last,-1,sizeof(last)); n=read(); for (int u=1;u<=n;u++) { s[u].x=read();s[u].y=read(); s[u].id=u; } sort(s+1,s+1+n,cmp); for (int u=2;u<=n;u++) init(s[u].id,s[u-1].id,s[u].x-s[u-1].x); sort(s+1,s+1+n,cmp1); for (int u=2;u<=n;u++) init(s[u].id,s[u-1].id,s[u].y-s[u-1].y); SPFA(); return 0;}
上一篇:bzoj 1455: 罗马游戏 左偏树入门
下一篇:bzoj3879: SvT 后缀自动机

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月11日 12时08分06秒