bzoj4992 [Usaco2017 Feb]Why Did the Cow Cross the Road(分层图最短路)
来源:互联网 发布:易语言赚钱源码 编辑:程序博客网 时间:2024/06/18 09:37
把每个点拆成三个点,分别表示走了%3余0步到i,记作3*i+0,走了%3余1步到i,记作3 *i+1,走了%3余2步到i,记作3 *i+2。每个点i可以向上下左右走一步到达j点,所以对于i,j我们建三条有向边,从 3*i走到3*j+1,边权为t,从3*i+1走到3*j+2,边权为t,从3*i+2走到3*j,边权为t+a[j] (第三步有附加值)。然后以3为源点跑一遍spfa即可。最后答案就是3*n*n,3*n*n+1,3*n*n+2中最小的一个。
主要就是个建图,很神奇。orz leoly
#include <bits/stdc++.h>using namespace std;#define pa pair<int,int>#define ll long long#define inf 0x3f3f3f3f#define N 105inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f;}int n,t,a[N][N],dx[]={0,0,1,-1},dy[]={1,-1,0,0},d[N*N*3],h[N*N*3],num=0;bool inq[N*N*3];struct edge{ int to,next,v;}data[12*N*N];inline void add1(int x,int y,int v){ data[++num].to=y;data[num].next=h[x];h[x]=num;data[num].v=v;}void spfa(int s){ queue<int>q; memset(d,0x3f,sizeof(d)); q.push(s);inq[s]=1;d[s]=0; while(!q.empty()){ int x=q.front();q.pop();inq[x]=0; for(int i=h[x];i;i=data[i].next){ int y=data[i].to; if(d[x]+data[i].v<d[y]){ d[y]=d[x]+data[i].v; if(!inq[y]) inq[y]=1,q.push(y); } } }}int main(){// freopen("a.in","r",stdin); n=read();t=read(); for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) a[i][j]=read(); for(int x=1;x<=n;++x) for(int y=1;y<=n;++y) for(int k=0;k<4;++k){ int xx=x+dx[k],yy=y+dy[k]; if(xx<1||xx>n||yy<1||yy>n) continue; int i=(x-1)*n+y,j=(xx-1)*n+yy; add1(3*i,3*j+1,t);add1(3*i+1,3*j+2,t);add1(3*i+2,3*j,t+a[xx][yy]); } spfa(3); printf("%d\n",min(min(d[n*n*3],d[n*n*3+1]),d[n*n*3+2])); return 0;}
阅读全文
0 0
- bzoj4992 [Usaco2017 Feb]Why Did the Cow Cross the Road(分层图最短路)
- 【bzoj4992: [Usaco2017 Feb]Why Did the Cow Cross the Road】动规
- 4992: [Usaco2017 Feb]Why Did the Cow Cross the Road
- bzoj4995: [Usaco2017 Feb]Why Did the Cow Cross the Road
- bzoj4989: [Usaco2017 Feb]Why Did the Cow Cross the Road
- Bzoj4989 [Usaco2017 Feb]Why Did the Cow Cross the Road
- [Usaco2017 Feb]Why Did the Cow Cross the Road
- [Usaco2017 Feb]Why Did the Cow Cross the Road II
- [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj 4993: [Usaco2017 Feb]Why Did the Cow Cross the Road II
- BZOJ 4993: [Usaco2017 Feb]Why Did the Cow Cross the Road II LCS
- BZOJ 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 树状数组优化LCS
- BZOJ 4989: [Usaco2017 Feb]Why Did the Cow Cross the Road 逆序对
- 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II
- bzoj4993 [Usaco2017 Feb]Why Did the Cow Cross the Road II
- bzoj4994 [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj4993: [Usaco2017 Feb]Why Did the Cow Cross the Road II
- Hdu 6000 Wash(贪心)
- jquery 根据数据库值设置radio的选中
- Python3 深浅拷贝
- 从子 Activity 获取返回结果
- Atom代码编辑软件炫酷效果插件activate-power-mode安装方法
- bzoj4992 [Usaco2017 Feb]Why Did the Cow Cross the Road(分层图最短路)
- jdbc链接mysql数据库
- 火星文字(PAT-1044)
- #1008约瑟夫问题
- 【scrapy ip代理池】如何解决ip被限制的问题
- 关于合格设计师的30条冷知识
- GitHub上整理的一些工具
- ipmitool配置管理网络
- SQL 查询当天,本月,本周的记录 ,日期计算