题意很简单,不说了。

这题思路其实挺简单,把每个格编号为i,3*i走到3*j+1,3*i+1走到3*j+2,以此类推,注意一下边权是多少就好了.

然而一开始用分层图的普遍套路i+j*n*n编号调了好久发现不同层编号有冲突,mdzz....

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <algorithm>
  4 #include <cstring>
  5 #include <cmath>
  6 #include <queue>
  7 #include <map>
  8 #define ll long long
  9 #define out(a) printf("%d ",a)
 10 #define ln printf("\n")
 11 #define clear(a,b) memset(a,b,sizeof(a))
 12 const int N=1e5+2e4+50;
 13 const int MOD=1e9+7;
 14 using namespace std;
 15 int n,nf,t;
 16 int x_,y_,l,r,cnt=0;
 17 int x[150][150],a[150][150];
 18 int tot=0;
 19 int head[N],dis[N];
 20 bool vis[N];
 21 int ans=23333333;
 22 int dx[4]={1,-1,0,0};
 23 int dy[4]={0,0,1,-1};
 24 struct node
 25 {
 26     int to,nxt,cost;
 27 }edge[N<<1];
 28 struct dist
 29 {
 30     int h,id;
 31     bool operator<(const dist&a)const{
 32       return a.h<h;
 33     }
 34 };
 35 int read()
 36 {
 37     int s=0,t=1; char c;
 38     while (c<'0'||c>'9'){if (c=='-') t=-1; c=getchar();}
 39     while (c>='0'&&c<='9'){s=s*10+c-'0'; c=getchar();}
 40     return s*t;
 41 }
 42 ll readl()
 43 {
 44     ll s=0,t=1; char c;
 45     while (c<'0'||c>'9'){if (c=='-') t=-1; c=getchar();}
 46     while (c>='0'&&c<='9'){s=s*10+c-'0'; c=getchar();}
 47     return s*t;
 48 }
 49 void add(int x,int y,int z)
 50 {
 51     edge[++tot].to=y;
 52     edge[tot].cost=z;
 53     edge[tot].nxt=head[x];
 54     head[x]=tot;
 55 }
 56 priority_queue<dist>q;
 57 void Dijkstra(int s)
 58 {
 59     int x,y,z;
 60     clear(dis,127); clear(vis,false);
 61     dis[s]=0;
 62     dist p;
 63     p.h=0; p.id=s;
 64     q.push(p);
 65     while (!q.empty()){
 66       p=q.top(); q.pop();
 67       x=p.id;
 68       if (!vis[x]) {
 69           vis[x]=true;
 70           for (int i=head[x];i;i=edge[i].nxt){
 71             y=edge[i].to; z=edge[i].cost;
 72             if (dis[y]>dis[x]+z){
 73                 dis[y]=dis[x]+z;
 74                 p.h=dis[y]; p.id=y;
 75                 q.push(p);
 76             }
 77           }
 78       }
 79     }
 80     ans=min(min(dis[3*nf],dis[3*nf+1]),dis[3*nf+2]);
 81 }
 82 int main()
 83 {
 84     n=read(),t=read();
 85     for (int i=1;i<=n;i++)
 86       for (int j=1;j<=n;j++)
 87         x[i][j]=read(),a[i][j]=++cnt;
 88     nf=n*n;
 89     for (int i=1;i<=n;i++)
 90       for (int j=1;j<=n;j++){
 91           for (int k=0;k<4;k++){
 92             x_=i+dx[k]; y_=j+dy[k];
 93             if (x_>0&&x_<=n&&y_>0&&y_<=n) {
 94                 l=a[i][j]; r=a[x_][y_];
 95                 add(3*l,3*r+1,t); //add(3*r+1,3*l,t);
 96                 add(3*l+1,3*r+2,t); //add(3*r+2,3*l+1,t);
 97                 add(3*l+2,3*r,x[x_][y_]+t); //add(3*r,3*l+2,x[i][j]+t);
 98             }
 99          }
100      }
101      Dijkstra(3);
102     out(ans);
103     return 0;
104 }
105
106
107       

View Code

转载于:https://www.cnblogs.com/Kaleidoscope233/p/9570760.html

BZOJ 4992: [Usaco2017 Feb]Why Did the Cow Cross the Road相关推荐

  1. bzoj 4997: [Usaco2017 Feb]Why Did the Cow Cross the Road III(Pu1 2018.10.1)

    算法:dfs,连通块 难度:NOIP 题解:搜索出每个连通块,找到每个连通块里有几头牛.然后每个连通块内牛数量相乘,求和即为正解 不清楚luogu题解中的代码明明MLE了,却还能AC??? 代码如下: ...

  2. BZOJ 4997 [Usaco2017 Feb]Why Did the Cow Cross the Road III

    本来想做一道搜索 最后发现是一道并查集的水题 如果两个邻格之间没有路 就把他们合并就行了 #include <cstdio> #include <cstring> #inclu ...

  3. bzoj 4997: [Usaco2017 Feb]Why Did the Cow Cross the Road III

    题意: 给你一个n*n的地图 某对相邻的点中间有墙. 然后给你n个坐标,问你有多少对坐标不可以互相到达. 题解: 至今为止,提交记录就只有1个WA的-- 随便暴力做一下就好了.

  4. 【bzoj4992: [Usaco2017 Feb]Why Did the Cow Cross the Road】动规

    4992: [Usaco2017 Feb]Why Did the Cow Cross the Road Time Limit: 10 Sec   Memory Limit: 256 MB Submit ...

  5. [bzoj4994][Usaco2017 Feb]Why Did the Cow Cross the Road III_树状数组

    Why Did the Cow Cross the Road III bzoj-4994 Usaco-2017 Feb 题目大意:给定长度为$2N$的序列,$1~N$各处现过$2$次,$i$第一次出现 ...

  6. [BZOJ4994] [Usaco2017 Feb]Why Did the Cow Cross the Road III(树状数组)

    传送门 1.每个数的左右位置预处理出来,按照左端点排序,因为左端点是从小到大的,我们只需要知道每条线段包含了多少个前面线段的右端点即可,可以用树状数组 2.如果 ai < bj < bi, ...

  7. 【bzoj4994】[Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组

    题目描述 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 样例输入 4 3 2 4 4 1 3 2 1 样例输 ...

  8. BZOJ4997 [Usaco2017 Feb]Why Did the Cow Cross the Road III

    欢迎访问~原文出处--博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4997 题意概括 在n*n的区域里,每一个1*1的块都是一个格子. 有k头牛在里面. 有r个篱笆把格 ...

  9. bzoj4997 [Usaco2017 Feb]Why Did the Cow Cross the Road III(bfs)

    枚举每一个起点,bfs能访问到几头牛即可.复杂度O(n3)O(n^3) #include <cstdio> #include <cstring> #include <al ...

最新文章

  1. React Native开发环境搭建
  2. 找找 Spring Event 源码中各种设计模式的使用
  3. 为jupyter_notebook增加目录
  4. gre考试能用计算机么,新GRE考试必须知道的九大考场问题
  5. 数据预处理工具_数据预处理
  6. python for循环1 到10_python for循环(1)
  7. JQuery--使用autocomplete控件进行自己主动输入完毕(相当于模糊查询)
  8. python 字符类型
  9. 丙烯怎么做成流体丙烯_怎么选择聚丙烯酰胺的分子量
  10. 2008年全国计算机等级考试须知及参考资料
  11. 自定义JSON配置器
  12. 共模电感适用的频率_电感基础知识入门
  13. php while次数,PHP While 循环
  14. React中Video播放器的使用
  15. 计算机硬盘扇区修复,w7硬盘坏道修复详细教程
  16. 毕业设计-基于汇编语言的at89c52单片机可调数字钟的设计,基于AT89S52单片机数字钟的仿真设计(汇编语言程序)...
  17. Linux 常见问题
  18. 【更新】关于VMware虚拟机无法正常获取IP地址问题的解决方法及思路
  19. 如何破解华为家长防护
  20. DNS工作原理及过程讲解

热门文章

  1. iOS开发:对于AFNetworking HTTP转HTTPS请求证书问题
  2. bzoj1596[Usaco2008 Jan]电话网络*
  3. Qt之QFileSystemWatcher
  4. 葡萄城发布新版ActiveReports 9报表控件和报表服务器
  5. [leetcode]Palindrome Number @ Python
  6. 文章读后感--社会文明分析总结
  7. 眼睛-摄像 科技-文学
  8. 未能找到服务器的主机名,未能找到服务器的主机名
  9. PCL1.8.0/ Qt5.7.0开发环境配置
  10. qtableview删除选中行_如何批量删除PPT备注+如何修改模板信息