BZOJ 2834: 回家的路 分层图最短路
来源:互联网 发布:淘宝运营团队靠谱吗 编辑:程序博客网 时间:2024/05/17 09:41
2834: 回家的路
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 125 Solved: 70
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
2 1
1 2
1 1 2 2
1 2
1 1 2 2
Sample Output
5
HINT
N<=20000,M<=100000
不会。。。翻题解
哇,分层图
感觉思路还是蛮精巧的:
把图拆成横纵两层
#include<cmath>#include<ctime>#include<cstdio>#include<cstring>#include<cstdlib>#include<complex>#include<iostream>#include<algorithm>#include<iomanip>#include<vector>#include<string>#include<bitset>#include<queue>#include<map>#include<set>using namespace std;inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch<='9'&&ch>='0'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}return x*f;}const int inf=0X7f7f7f7f;const int N=200100;const int M=5001000;int n,m,ecnt,last[N];struct EDGE{int to,nt,val;}e[M];inline void add(int u,int v,int val){e[++ecnt]=(EDGE){v,last[u],val};last[u]=ecnt;}struct node{int x,y,id;}a[N];inline bool cmp1(node x,node y){return x.x==y.x?x.y<y.y:x.x<y.x;}inline bool cmp2(node x,node y){return x.y==y.y?x.x<y.x:x.y<y.y;}int q[N],dis[N],s,t;bool inq[N];void spfa(){memset(dis,0X7f,sizeof(dis));dis[s]=0;int head=0,tail=1;q[0]=s;inq[s]=1;while(head!=tail){int u=q[head++];head%=N;inq[u]=0;for(int i=last[u];i;i=e[i].nt)if(dis[e[i].to]>dis[u]+e[i].val){dis[e[i].to]=dis[u]+e[i].val;if(!inq[e[i].to]){q[tail++]=e[i].to;inq[e[i].to]=1;tail%=N;}}}if(dis[t]>=inf)dis[t]=-1;}int main(){n=read();m=read();int x,y;s=m+1;t=m+2;for(int i=1;i<=m+2;i++){a[i].x=read();a[i].y=read();a[i].id=i;}sort(a+1,a+m+3,cmp1);for(int i=1;i<=m+2;i++)if(a[i].x==a[i+1].x)add(a[i].id,a[i+1].id,2*(a[i+1].y-a[i].y)),add(a[i+1].id,a[i].id,2*(a[i+1].y-a[i].y));sort(a+1,a+m+3,cmp2);for(int i=1;i<=m+2;i++)if(a[i].y==a[i+1].y)add(a[i].id+m+2,a[i+1].id+m+2,2*(a[i+1].x-a[i].x)),add(a[i+1].id+m+2,a[i].id+m+2,2*(a[i+1].x-a[i].x));for(int i=1;i<=m;i++)add(i,i+m+2,1),add(i+m+2,i,1);for(int i=m+1;i<=m+2;i++)add(i,i+m+2,0),add(i+m+2,i,0);spfa();printf("%d\n",dis[t]);return 0;}/*2 11 21 1 2 25*/
阅读全文
0 0
- BZOJ 2834(回家的路-分层图最短路)
- BZOJ 2834: 回家的路 分层图最短路
- 【bzoj2834】回家的路 最短路
- 【bzoj2834】【回家的路】【最短路】
- BZOJ 2763 JLOI 2011 飞行路线 分层图+最短路
- BZOJ 2763: [JLOI2011]飞行路线(分层图最短路)
- bzoj 2763: [JLOI2011]飞行路线 分层图最短路
- BZOJ 2763: [JLOI2011]飞行路线 分层图最短路
- BZOJ 4152: [AMPPZ2014]The Captain 分层图最短路
- bzoj 2763 飞行路线(分层图最短路)
- bzoj 2662: [BeiJing wc2012]冻结 分层图最短路
- hdu3499(分层图最短路)
- 分层图最短路 bzoj2763
- BZOJ-1922 大陆争霸 多限制、分层图最短路 (堆+dijkstra)
- [bfs 分层图 字典序 最短路] BZOJ 2644 Pku3967 Ideal Path
- BZOJ 1671: [Usaco2005 Dec]Knights of Ni 骑士 分层图最短路
- BZOJ 1579: [Usaco2009 Feb]Revamping Trails 道路升级 分层图最短路
- [codevs2070]爱情之路 分层图最短路
- JQuery获取浏览器窗口的高度和宽度
- 笑谈java并发编程六之CountDownLatch
- IC设计基础系列之低功耗篇3:数字IC)低功耗设计入门(三)——系统与架构级低功耗设计
- spring RMI demo运行报错-nested exception is java.rmi.ConnectException: Connection refused to host: local
- Directshow 智能连接的步骤
- BZOJ 2834: 回家的路 分层图最短路
- Google Earth与ArcGIS数据交互
- Android系统源代码的下载与编译
- 老域名对网站SEO优化的重要性
- SQL SERVER 无法修改表
- Eclipse常用快捷键
- 操作系统概念(高等教育出版社,第七版)复习——第五章:CPU调度
- 安卓开发最简单的按键监听事件
- TFTP 服务简单分析--uboot2009.08